diff --git a/_config/extensions.yml b/_config/extensions.yml index 0e058ac..1e3766b 100644 --- a/_config/extensions.yml +++ b/_config/extensions.yml @@ -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" diff --git a/src/Extension/VirtualElementContentControllerExtension.php b/src/Extension/VirtualElementContentControllerExtension.php new file mode 100644 index 0000000..0cae4e8 --- /dev/null +++ b/src/Extension/VirtualElementContentControllerExtension.php @@ -0,0 +1,53 @@ +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); + } +}