diff --git a/src/Models/ElementalArea.php b/src/Models/ElementalArea.php index c5ee3657..efbc3391 100644 --- a/src/Models/ElementalArea.php +++ b/src/Models/ElementalArea.php @@ -12,6 +12,7 @@ use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\HasManyList; +use SilverStripe\ORM\UnsavedRelationList; use SilverStripe\Versioned\Versioned; /** @@ -108,6 +109,11 @@ public function Breadcrumbs() */ public function ElementControllers() { + // Don't try and process unsaved lists + if ($this->Elements() instanceof UnsavedRelationList) { + return ArrayList::create(); + } + $controllers = ArrayList::create(); $items = $this->Elements()->filterByCallback(function (BaseElement $item) { return $item->canView(); diff --git a/tests/ElementalAreaTest.php b/tests/ElementalAreaTest.php index 584b3caf..0d980895 100644 --- a/tests/ElementalAreaTest.php +++ b/tests/ElementalAreaTest.php @@ -4,10 +4,12 @@ use DNADesign\Elemental\Extensions\ElementalPageExtension; use DNADesign\Elemental\Models\ElementalArea; +use DNADesign\Elemental\Models\ElementContent; use DNADesign\Elemental\Tests\Src\TestElement; use DNADesign\Elemental\Tests\Src\TestPage; use Page; use SilverStripe\Dev\SapphireTest; +use SilverStripe\ORM\ArrayList; use SilverStripe\Versioned\Versioned; class ElementalAreaTest extends SapphireTest @@ -116,4 +118,18 @@ public function testDuplicate() $this->assertCount(2, $duplicatedAreaIds); $this->assertNotEquals($areaIds, $duplicatedAreaIds); } + + public function testUnsavedRelationListOfElementsReturnsEmptyArrayList() + { + $area = new ElementalArea(); + + $element = new ElementContent(); + $element->HTML = 'Test'; + + $area->Elements()->add($element); + + $result = $area->ElementControllers(); + $this->assertInstanceOf(ArrayList::class, $result); + $this->assertEmpty($result); + } }