-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add handling for virtualized ElementController (#39)
- Loading branch information
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
--- | ||
Name: elementalvirtual | ||
After: 'elemental' | ||
After: "elemental" | ||
--- | ||
DNADesign\Elemental\Models\BaseElement: | ||
extensions: | ||
- DNADesign\ElementalVirtual\Extensions\BaseElementExtension | ||
SilverStripe\Admin\LeftAndMain: | ||
extra_requirements_css: | ||
- 'dnadesign/silverstripe-elemental-virtual:css/elemental-admin.css' | ||
- "dnadesign/silverstripe-elemental-virtual:css/elemental-admin.css" | ||
SilverStripe\CMS\Controllers\ContentController: | ||
extensions: | ||
- DNADesign\ElementalVirtual\Extensions\VirtualElementalContentControllerExtension | ||
url_handlers: | ||
"element/$ID!": "handleElement" |
53 changes: 53 additions & 0 deletions
53
src/Extension/VirtualElementContentControllerExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace DNADesign\ElementalVirtual\Extensions; | ||
|
||
use DNADesign\Elemental\Models\BaseElement; | ||
use SilverStripe\Core\Extension; | ||
|
||
class VirtualElementalContentControllerExtension extends Extension | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private static $allowed_actions = [ | ||
'handleElement' | ||
]; | ||
|
||
public function handleElement() | ||
{ | ||
$id = $this->owner->getRequest()->param('ID'); | ||
|
||
if (!$id) { | ||
return $this->owner->httpError(400, 'no element ID provided'); | ||
} | ||
|
||
$element = BaseElement::get()->filter('ID', $id)->First(); | ||
$page = $this->owner->data(); | ||
|
||
if ($element && $element->canView()) { | ||
$useElement = clone $element; | ||
|
||
// modify the element to appear on the correct 'Page' so that | ||
// any breadcrumbs and titles are correct. | ||
$elementalAreaRelations = $this->owner->getElementalRelations(); | ||
$id = null; | ||
|
||
foreach ($elementalAreaRelations as $elementalAreaRelation) { | ||
$id = $page->$elementalAreaRelation()->ID; | ||
|
||
if ($id) { | ||
break; | ||
} | ||
} | ||
|
||
$useElement->ParentID = $id; | ||
$useElement->setAreaRelationNameCache($elementalAreaRelation); | ||
$controller = $useElement->getController(); | ||
|
||
return $controller; | ||
} | ||
|
||
return $this->owner->httpError(404); | ||
} | ||
} |