diff --git a/src/Extensions/ElementalContentControllerExtension.php b/src/Extensions/ElementalContentControllerExtension.php index 243aa2b4..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,27 +25,55 @@ 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 = $elementOwner->$elementalAreaRelation()->Elements() - ->filter('ID', $id) - ->First(); + $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(iterable $elementalAreaRelations, DataObject $owner, $id): ?DataObject + { + foreach ($elementalAreaRelations as $elementalAreaRelation) { + $elements = $owner->$elementalAreaRelation()->Elements(); + $found = $elements->filter('ID', $id)->First(); + + if ($found) { + return $found; + } + + /** @var BaseElement $element */ + foreach ($elements as $element) { + if (!$element->hasExtension(ElementalAreasExtension::class)) { + continue; + } + + /** @var BaseElement&ElementalAreasExtension $element */ + $found = $this->findElement($element->getElementalRelations(), $element, $id); + + if ($found) { + return $found; + } + } + } + + return null; + } } 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); }