mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #7285 from open-sausages/pulls/4.0/schema-ing-our-path-to-victory
Enhancement Add a path option for the schema data, …
This commit is contained in:
commit
a39325f1fc
@ -195,7 +195,13 @@ class TreeDropdownField extends FormField
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $searchExpanded = [];
|
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,
|
* CAVEAT: for search to work properly $labelField must be a database field,
|
||||||
* or you need to setSearchFunction.
|
* or you need to setSearchFunction.
|
||||||
@ -829,15 +835,28 @@ class TreeDropdownField extends FormField
|
|||||||
public function getSchemaStateDefaults()
|
public function getSchemaStateDefaults()
|
||||||
{
|
{
|
||||||
$data = parent::getSchemaStateDefaults();
|
$data = parent::getSchemaStateDefaults();
|
||||||
|
/** @var Hierarchy|DataObject $record */
|
||||||
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
|
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
|
||||||
|
|
||||||
// Ensure cache is keyed by last modified date of the underlying list
|
// Ensure cache is keyed by last modified date of the underlying list
|
||||||
$data['data']['cacheKey'] = DataList::create($this->getSourceObject())->max('LastEdited');
|
$data['data']['cacheKey'] = DataList::create($this->getSourceObject())->max('LastEdited');
|
||||||
|
$data['data']['showSelectedPath'] = $this->getShowSelectedPath();
|
||||||
if ($record) {
|
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'] = [
|
$data['data']['valueObject'] = [
|
||||||
'id' => $record->obj($this->getKeyField())->getValue(),
|
'id' => $record->obj($this->getKeyField())->getValue(),
|
||||||
'title' => $record->obj($this->getTitleField())->getValue(),
|
'title' => $record->obj($this->getTitleField())->getValue(),
|
||||||
'treetitle' => $record->obj($this->getLabelField())->getSchemaValue(),
|
'treetitle' => $record->obj($this->getLabelField())->getSchemaValue(),
|
||||||
|
'titlePath' => $titlePath,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -908,4 +927,22 @@ class TreeDropdownField extends FormField
|
|||||||
);
|
);
|
||||||
return $emptyString;
|
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';
|
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()
|
public function testTreeSearchJson()
|
||||||
{
|
{
|
||||||
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
$field = new TreeDropdownField('TestTree', 'Test tree', Folder::class);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user