Skip to content
Permalink
Browse files

Enhancement Add a path option for the schema data, so a full tree is …

…not required for this data
  • Loading branch information
Christopher Joe Damian Mooyman
Christopher Joe authored and Damian Mooyman committed Aug 10, 2017
1 parent 506ae52 commit 9dc11eff4362f91c56ad63cf042cf4936f8ae7d3
Showing with 63 additions and 1 deletion.
  1. +38 −1 src/Forms/TreeDropdownField.php
  2. +25 −0 tests/php/Forms/TreeDropdownFieldTest.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 @@ public function castedCopy($classOrCopy)
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 @@ public function getEmptyString()
);
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);

0 comments on commit 9dc11ef

Please sign in to comment.
You can’t perform that action at this time.