Skip to content

Commit

Permalink
feat: add handling for virtualized ElementController (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 17, 2023
1 parent 76f32d4 commit 467885d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
9 changes: 7 additions & 2 deletions _config/extensions.yml
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 src/Extension/VirtualElementContentControllerExtension.php
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);
}
}

0 comments on commit 467885d

Please sign in to comment.