diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index dc6a13788..23cfda353 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -195,7 +195,13 @@ class TreeDropdownField extends FormField * @var array */ protected $searchExpanded = []; - + + /** + * Show full path for selected options, only applies for single select + * @var bool + */ + protected $showSelectedPath = false; + /** * CAVEAT: for search to work properly $labelField must be a database field, * or you need to setSearchFunction. @@ -829,15 +835,28 @@ class TreeDropdownField extends FormField public function getSchemaStateDefaults() { $data = parent::getSchemaStateDefaults(); + /** @var Hierarchy|DataObject $record */ $record = $this->Value() ? $this->objectForKey($this->Value()) : null; // Ensure cache is keyed by last modified date of the underlying list $data['data']['cacheKey'] = DataList::create($this->getSourceObject())->max('LastEdited'); + $data['data']['showSelectedPath'] = $this->getShowSelectedPath(); if ($record) { + $titlePath = ''; + + if ($this->getShowSelectedPath()) { + $ancestors = $record->getAncestors(true)->reverse(); + + foreach ($ancestors as $parent) { + $title = $parent->obj($this->getTitleField())->getValue(); + $titlePath .= $title .'/'; + } + } $data['data']['valueObject'] = [ 'id' => $record->obj($this->getKeyField())->getValue(), 'title' => $record->obj($this->getTitleField())->getValue(), 'treetitle' => $record->obj($this->getLabelField())->getSchemaValue(), + 'titlePath' => $titlePath, ]; } @@ -908,4 +927,22 @@ class TreeDropdownField extends FormField ); return $emptyString; } + + /** + * @return bool + */ + public function getShowSelectedPath() + { + return $this->showSelectedPath; + } + + /** + * @param bool $showSelectedPath + * @return TreeDropdownField + */ + public function setShowSelectedPath($showSelectedPath) + { + $this->showSelectedPath = $showSelectedPath; + return $this; + } } diff --git a/tests/php/Forms/TreeDropdownFieldTest.php b/tests/php/Forms/TreeDropdownFieldTest.php index d370e226a..bc49151cb 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.php +++ b/tests/php/Forms/TreeDropdownFieldTest.php @@ -15,6 +15,31 @@ class TreeDropdownFieldTest extends SapphireTest protected static $fixture_file = 'TreeDropdownFieldTest.yml'; + public function testSchemaStateDefaults() + { + $field = new TreeDropdownField('TestTree', 'Test tree', Folder::class); + $folder = $this->objFromFixture(Folder::class, 'folder1-subfolder1'); + + $schema = $field->getSchemaStateDefaults(); + $this->assertFalse(isset($schema['data']['valueObject'])); + + $field->setValue($folder->ID); + + $schema = $field->getSchemaStateDefaults(); + $this->assertEquals($folder->ID, $schema['data']['valueObject']['id']); + $this->assertTrue(isset($schema['data']['valueObject'])); + $this->assertFalse($schema['data']['showSelectedPath']); + $this->assertEquals('', $schema['data']['valueObject']['titlePath']); + + $field->setShowSelectedPath(true); + $schema = $field->getSchemaStateDefaults(); + $this->assertTrue($schema['data']['showSelectedPath']); + $this->assertEquals( + 'FileTest-folder1/FileTest-folder1-subfolder1/', + $schema['data']['valueObject']['titlePath'] + ); + } + public function testTreeSearchJson() { $field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);