diff --git a/_config/elemental.yml b/_config/elemental.yml index 1f7d4de..388093f 100644 --- a/_config/elemental.yml +++ b/_config/elemental.yml @@ -1,6 +1,6 @@ --- name: elemental-list --- -DNADesign/Elemental/Models/BaseElement: +DNADesign\Elemental\Models\BaseElement: extensions: - - DNADesign/ElementalList/Extensions/BaseElementExtension + - DNADesign\ElementalList\Extension\BaseElementCMSEditLinkExtension diff --git a/src/Extension/BaseElementCMSEditLinkExtension.php b/src/Extension/BaseElementCMSEditLinkExtension.php new file mode 100644 index 0000000..d968592 --- /dev/null +++ b/src/Extension/BaseElementCMSEditLinkExtension.php @@ -0,0 +1,61 @@ +owner; + + $relationName = $owner->getAreaRelationName(); + $page = $owner->getPage(true); + + if (!$page) { + return; + } + + if ($page instanceof ElementList) { + // nested bock - we need to get edit link of parent block + $link = Controller::join_links( + $page->CMSEditLink(), + 'ItemEditForm/field/' . $page->getOwnedAreaRelationName() . '/item/', + $owner->ID + ); + + // remove edit link from parent CMS link + $link = preg_replace('/\/item\/([\d]+)\/edit/', '/item/$1', $link); + } else { + // block is directly under a non-block object - we have reached the top of nesting chain + $link = Controller::join_links( + singleton(CMSPageEditController::class)->Link('EditForm'), + $page->ID, + 'field/' . $relationName . '/item/', + $owner->ID + ); + } + + $link = Controller::join_links( + $link, + 'edit' + ); + } +} diff --git a/src/Model/ElementList.php b/src/Model/ElementList.php index 7ded0a5..68b8980 100644 --- a/src/Model/ElementList.php +++ b/src/Model/ElementList.php @@ -52,4 +52,22 @@ class ElementList extends BaseElement return DBField::create_field('HTMLText', $summary . ' Contains ' . $count . ' ' . $suffix . ''); } + + /** + * Retrieve a elemental area relation name which this element owns + * + * @return string + */ + public function getOwnedAreaRelationName() + { + $has_one = $this->config()->get('has_one'); + + foreach ($has_one as $relationName => $relationClass) { + if ($relationClass === ElementalArea::class && $relationName !== 'Parent') { + return $relationName; + } + } + + return 'Elements'; + } }