Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix form submission, when form block is inside another block #1107

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions src/Extensions/ElementalContentControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}