mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Enhancement Add a path option for the schema data, so a full tree is not required for this data
This commit is contained in:
parent
506ae52e62
commit
9dc11eff43
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user