FIX Format value to string for readonly transformation

Co-authored-by: Werner Huber <w.huber@byro13.de>
This commit is contained in:
Guy Sartorelli 2023-04-17 13:45:22 +12:00
parent 90f02b5602
commit 158bc006a5
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
2 changed files with 25 additions and 14 deletions

View File

@ -412,4 +412,11 @@ class StringTagField extends DropdownField
return $this;
}
public function performReadonlyTransformation()
{
$field = parent::performReadonlyTransformation();
$field->setValue(implode(', ', $this->Value()));
return $field;
}
}

View File

@ -171,22 +171,26 @@ class StringTagFieldTest extends SapphireTest
$this->assertNotEmpty($attributes['data-schema']);
}
/**
* Ensure a source of tags matches the given string tag names
*
* @param array $expected
* @param DataList $actualSource
*/
protected function compareTagLists(array $expected, DataList $actualSource)
public function testPerformReadonlyTransformation()
{
$actual = array_keys($actualSource->map('ID', 'Title')->toArray());
sort($expected);
sort($actual);
$field = new StringTagField('Tags');
$field->setSource(['Test1', 'Test2', 'Test3']);
$this->assertEquals(
$expected,
$actual
);
// Ensure a single value can be rendered
$field->setValue(['Test2']);
$field_readonly = $field->performReadonlyTransformation();
$this->assertEquals('Test2', $field_readonly->Value());
// Ensure multiple valid values are rendered
$field->setValue(['Test1', 'Test2']);
$field_readonly = $field->performReadonlyTransformation();
$this->assertEquals('Test1, Test2', $field_readonly->Value());
// Ensure an value not in the source array is still rendered
// (because e.g. in history view it must have been a valid value when it was set)
$field->setValue(['Test', 'Test1']);
$field_readonly = $field->performReadonlyTransformation();
$this->assertEquals('Test, Test1', $field_readonly->Value());
}
/**