From 8ec9c1fc203b15240220891c82612b47459617ea Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 12 Jan 2018 10:55:15 +1300 Subject: [PATCH] CMSEditLink() now reflects the nested path of an element. --- _config/elemental.yml | 4 +- .../BaseElementCMSEditLinkExtension.php | 61 +++++++++++++++++++ src/Model/ElementList.php | 18 ++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Extension/BaseElementCMSEditLinkExtension.php 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'; + } }