From 0af064530ea2c139f7fd81a503b141ad6d94d30b Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Thu, 21 Sep 2023 11:38:56 +1200 Subject: [PATCH 1/4] FIX Element::getPage returning the wrong page --- src/Models/BaseElement.php | 13 ++++++++++--- tests/BaseElementTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index a3f89c22..81eaf858 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -627,21 +627,28 @@ public function getSimpleClassName() /** * Despite the name of the method, getPage can return any type of DataObject + * * @return null|DataObject * @throws \Psr\Container\NotFoundExceptionInterface * @throws \SilverStripe\ORM\ValidationException */ public function getPage() { - // Allow for repeated calls to be cached if (isset($this->cacheData['page'])) { - return $this->cacheData['page']; + if (isset($this->cacheData['parent_id']) && $this->cacheData['parent_id'] === $this->ParentID) { + return $this->cacheData['page']; + } } $class = DataObject::getSchema()->hasOneComponent($this, 'Parent'); $area = ($this->ParentID) ? DataObject::get_by_id($class, $this->ParentID) : null; + if ($area instanceof ElementalArea && $area->exists()) { - $this->cacheData['page'] = $area->getOwnerPage(); + $page = $area->getOwnerPage(); + + $this->cacheData['page'] = $page; + $this->cacheData['parent_id'] = $this->ParentID; + return $this->cacheData['page']; } diff --git a/tests/BaseElementTest.php b/tests/BaseElementTest.php index 872bce3e..06ca9612 100644 --- a/tests/BaseElementTest.php +++ b/tests/BaseElementTest.php @@ -503,4 +503,16 @@ public function testPreviewLink(string $class, string $elementIdentifier, ?strin $this->assertEmpty(rtrim($previewLink ?? '', '/')); } } + + public function testGetPage() + { + $element = $this->objFromFixture(ElementContent::class, 'content1'); + + $this->assertStringContainsString($element->getPage()->Title, 'Test Elemental'); + + $newArea = $this->objFromFixture(ElementalArea::class, 'area52'); + $element->ParentID = $newArea->ID; + + $this->assertStringContainsString($element->getPage()->Title, 'Page with one elements'); + } } From 6e3180c0b438fa32d185f1e6f66e737a8b4d2138 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Wed, 11 Oct 2023 18:24:31 +1300 Subject: [PATCH 2/4] FIX Apply multiupload auto-detection for upload fields (#1108) UploadField checks hasone components based on the field name to see if it can be a multiUpload field or not. We need to trigger that before changing the name of the field. --- src/Forms/EditFormFactory.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Forms/EditFormFactory.php b/src/Forms/EditFormFactory.php index a3c46eee..c8579af7 100644 --- a/src/Forms/EditFormFactory.php +++ b/src/Forms/EditFormFactory.php @@ -2,6 +2,7 @@ namespace DNADesign\Elemental\Forms; +use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Control\RequestHandler; use SilverStripe\Core\Config\Configurable; use SilverStripe\Forms\DefaultFormFactory; @@ -66,6 +67,10 @@ protected function namespaceFields(FieldList $fields, array $context) $elementID = $context['Record']->ID; foreach ($fields->dataFields() as $field) { + if ($field instanceof UploadField) { + // Apply audo-detection of multi-upload before changing the name. + $field->setIsMultiUpload($field->getIsMultiUpload()); + } $namespacedName = sprintf(self::FIELD_NAMESPACE_TEMPLATE ?? '', $elementID, $field->getName()); $field->setName($namespacedName); } From c20588923bd62e6ad85963e1240ac33031475ecf Mon Sep 17 00:00:00 2001 From: sukhwinder-somar Date: Wed, 11 Oct 2023 00:20:11 +1300 Subject: [PATCH 3/4] fix form submission, when form block is inside another block --- .../ElementalContentControllerExtension.php | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Extensions/ElementalContentControllerExtension.php b/src/Extensions/ElementalContentControllerExtension.php index 243aa2b4..90adae2a 100644 --- a/src/Extensions/ElementalContentControllerExtension.php +++ b/src/Extensions/ElementalContentControllerExtension.php @@ -35,9 +35,7 @@ public function handleElement() } foreach ($elementalAreaRelations as $elementalAreaRelation) { - $element = $elementOwner->$elementalAreaRelation()->Elements() - ->filter('ID', $id) - ->First(); + $element = $this->findElement($elementOwner->{$elementalAreaRelation}()->Elements(), $id); if ($element) { return $element->getController(); @@ -47,4 +45,35 @@ public function handleElement() user_error('Element $id not found for this page', E_USER_ERROR); return false; } + + private function findElement($elements, $id) + { + $element = $elements->filter('ID', $id)->First(); + + if ($element) { + return $element; + } + + foreach ($elements as $el) { + if (!$el->hasMethod('Elements')) { + continue; + } + + $subElementAreaRelations = $el->getElementalRelations(); + + if (!$subElementAreaRelations) { + continue; + } + + foreach ($subElementAreaRelations as $subElementalAreaRelation) { + $element = $this->findElement($el->{$subElementalAreaRelation}()->Elements(), $id); + + if ($element) { + return $element; + } + } + } + + return null; + } } From a2edb5e8cab0a8638cb013eeb227c79ebee79429 Mon Sep 17 00:00:00 2001 From: sukhwinder-somar Date: Thu, 12 Oct 2023 11:41:47 +1300 Subject: [PATCH 4/4] update PR with new supplied by reviewer --- .../ElementalContentControllerExtension.php | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Extensions/ElementalContentControllerExtension.php b/src/Extensions/ElementalContentControllerExtension.php index 90adae2a..cd642195 100644 --- a/src/Extensions/ElementalContentControllerExtension.php +++ b/src/Extensions/ElementalContentControllerExtension.php @@ -2,9 +2,10 @@ namespace DNADesign\Elemental\Extensions; +use SilverStripe\Core\Extension; +use SilverStripe\ORM\DataObject; use DNADesign\Elemental\Models\ElementalArea; use DNADesign\Elemental\Extensions\ElementalAreasExtension; -use SilverStripe\Core\Extension; class ElementalContentControllerExtension extends Extension { @@ -24,52 +25,51 @@ public function handleElement() return false; } - /** @var SiteTree $elementOwner */ + /** @var DataObject $elementOwner */ $elementOwner = $this->owner->data(); - $elementalAreaRelations = $this->owner->getElementalRelations(); + if (!$elementOwner->hasExtension(ElementalAreasExtension::class)) { + user_error(get_class($elementOwner) . ' doesnt have the ElementalAreasExtension applied', E_USER_ERROR); + return false; + } + $elementalAreaRelations = $elementOwner->getElementalRelations(); if (!$elementalAreaRelations) { - user_error(get_class($this->owner) . ' has no ElementalArea relationships', E_USER_ERROR); + user_error(get_class($elementOwner) . ' has no ElementalArea relationships', E_USER_ERROR); return false; } - foreach ($elementalAreaRelations as $elementalAreaRelation) { - $element = $this->findElement($elementOwner->{$elementalAreaRelation}()->Elements(), $id); + $element = $this->findElement($elementalAreaRelations, $elementOwner, $id); - if ($element) { - return $element->getController(); - } + if ($element) { + return $element->getController(); } user_error('Element $id not found for this page', E_USER_ERROR); return false; } - private function findElement($elements, $id) + private function findElement(iterable $elementalAreaRelations, DataObject $owner, $id): ?DataObject { - $element = $elements->filter('ID', $id)->First(); - - if ($element) { - return $element; - } + foreach ($elementalAreaRelations as $elementalAreaRelation) { + $elements = $owner->$elementalAreaRelation()->Elements(); + $found = $elements->filter('ID', $id)->First(); - foreach ($elements as $el) { - if (!$el->hasMethod('Elements')) { - continue; + if ($found) { + return $found; } - $subElementAreaRelations = $el->getElementalRelations(); - - if (!$subElementAreaRelations) { - continue; - } + /** @var BaseElement $element */ + foreach ($elements as $element) { + if (!$element->hasExtension(ElementalAreasExtension::class)) { + continue; + } - foreach ($subElementAreaRelations as $subElementalAreaRelation) { - $element = $this->findElement($el->{$subElementalAreaRelation}()->Elements(), $id); + /** @var BaseElement&ElementalAreasExtension $element */ + $found = $this->findElement($element->getElementalRelations(), $element, $id); - if ($element) { - return $element; + if ($found) { + return $found; } } }