Merge pull request #165 from open-sausages/pulls/fix-list-values

FIX Preselect based on SS_List values
This commit is contained in:
Sam Minnée 2020-08-12 13:32:50 +12:00 committed by GitHub
commit c50f461a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 6 deletions

View File

@ -1,4 +1,4 @@
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>

View File

@ -274,11 +274,13 @@ class TagField extends MultiSelectField
}
// Convert an array of values into a datalist of options
if (is_array($values) && !empty($values)) {
$values = DataList::create($dataClass)
->filter($this->getTitleField(), $values);
} else {
$values = ArrayList::create();
if (!$values instanceof SS_List) {
if (is_array($values) && !empty($values)) {
$values = DataList::create($dataClass)
->filter($this->getTitleField(), $values);
} else {
$values = ArrayList::create();
}
}
// Prep a function to parse a dataobject into an option

View File

@ -360,6 +360,24 @@ class TagFieldTest extends SapphireTest
$this->assertEquals('Name', $readOnlyField->getTitleField());
}
public function testItDisplaysWithSelectedValuesFromDataList()
{
$source = TagFieldTestBlogTag::get();
$selectedTag = $source->First();
$unselectedTag = $source->Last();
$value = $source->filter('ID', $selectedTag->ID); // arbitrary subset
$field = new TagField('TestField', null, $source, $value);
// Not the cleanest way to assert this, but getOptions() is protected
$schema = $field->getSchemaDataDefaults();
$this->assertTrue(
$this->getFromOptionsByTitle($schema['options'], $selectedTag->Title)['Selected']
);
$this->assertFalse(
$this->getFromOptionsByTitle($schema['options'], $unselectedTag->Title)['Selected']
);
}
public function testGetSchemaDataDefaults()
{
$form = new Form(null, 'Form', new FieldList(), new FieldList());
@ -403,4 +421,20 @@ class TagFieldTest extends SapphireTest
$attributes = $field->getAttributes();
$this->assertNotEmpty($attributes['data-schema']);
}
/**
* @param array $options
* @param string $title
* @return array|null
*/
protected function getFromOptionsByTitle(array $options, $title)
{
foreach ($options as $option) {
if ($option['Title'] == $title) {
return $option;
}
}
return null;
}
}