From efc9f45962fda2e6a6e3f8c0bf8daa24f17efa43 Mon Sep 17 00:00:00 2001 From: adunn49 Date: Tue, 1 Oct 2024 16:30:54 +1300 Subject: [PATCH] ENH Refactor the way selected options are returned for read only tag fields. --- src/ReadonlyTagField.php | 3 ++- tests/TagFieldTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ReadonlyTagField.php b/src/ReadonlyTagField.php index 505a2ad..2148d55 100644 --- a/src/ReadonlyTagField.php +++ b/src/ReadonlyTagField.php @@ -27,7 +27,8 @@ class ReadonlyTagField extends TagField { $options = array(); - foreach ($this->getOptions()->filter('Selected', true) as $option) { + // only get the selected options + foreach ($this->getOptions(true) as $option) { $options[] = $option->Title; } diff --git a/tests/TagFieldTest.php b/tests/TagFieldTest.php index 4ebfc8d..faf985f 100755 --- a/tests/TagFieldTest.php +++ b/tests/TagFieldTest.php @@ -408,6 +408,22 @@ class TagFieldTest extends SapphireTest $field->setTitleField('Name'); $readOnlyField = $field->performReadonlyTransformation(); $this->assertEquals('Name', $readOnlyField->getTitleField()); + + // Also check Field options + $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); + $field->setTitleField('Title'); + $field->setValue(['Tag1']); + + // When not read only (and not lazy-loading) all source options are returned + $htmlText = $field->Field(); + $this->assertStringContainsString('Tag1', $htmlText); + $this->assertStringContainsString('222', $htmlText); + + // When read only mode, only selected options are returned + $readOnlyField = $field->performReadonlyTransformation(); + $htmlText = $readOnlyField->Field(); + $this->assertStringContainsString('Tag1', $htmlText); + $this->assertStringNotContainsString('222', $htmlText); } public function testItDisplaysWithSelectedValuesFromDataList()