From a61612ec4b64131ecc9812190af95e46e179fd8d Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 6 Jul 2020 14:17:08 +1200 Subject: [PATCH 1/3] BUG Resolve issue with react select values not being saved Fixes #150 --- src/TagField.php | 9 ++++++++- tests/TagFieldTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/TagField.php b/src/TagField.php index 34a5113..5adde7c 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -6,7 +6,6 @@ use Exception; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; -use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\MultiSelectField; use SilverStripe\Forms\Validator; @@ -319,6 +318,14 @@ class TagField extends MultiSelectField return parent::setValue($value); } + // Safely map php / react-select values to flat list + $values = []; + foreach ($value as $item) { + if ($item) { + $values[] = $item['Value'] ?? $item; + } + } + return parent::setValue(array_filter($value)); } diff --git a/tests/TagFieldTest.php b/tests/TagFieldTest.php index 8429e4a..3db5092 100755 --- a/tests/TagFieldTest.php +++ b/tests/TagFieldTest.php @@ -131,6 +131,30 @@ class TagFieldTest extends SapphireTest $record ); } + + public function testSavesReactTags() + { + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); + $record->write(); + + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); + $field->setValue([ + [ + 'Title' => 'Tag1', + 'Value' => 'Tag1', + ], + [ + 'Title' => 'Tag2', + 'Value' => 'Tag2', + ], + ]); + $field->saveInto($record); + + $this->compareExpectedAndActualTags( + ['Tag1', 'Tag2'], + $record + ); + } /** * Ensure that {@see TagField::saveInto} respects existing tags From 2790d7504afb041a0683f0a79dfdf28ea24c3302 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 6 Jul 2020 14:33:09 +1200 Subject: [PATCH 2/3] =?UTF-8?q?PHP=205=20compatibility=20=F0=9F=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/TagField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TagField.php b/src/TagField.php index a0647f4..4a639db 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -322,7 +322,7 @@ class TagField extends MultiSelectField $values = []; foreach ($value as $item) { if ($item) { - $values[] = $item['Value'] ?? $item; + $values[] = isset($item['Value']) ? $item['Value'] : $item; } } From 748ef1a8f098fdde729fe5032a924bcefc4427ad Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 6 Jul 2020 14:36:17 +1200 Subject: [PATCH 3/3] Actually save the values --- src/TagField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TagField.php b/src/TagField.php index 4a639db..316a947 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -326,7 +326,7 @@ class TagField extends MultiSelectField } } - return parent::setValue(array_filter($value)); + return parent::setValue($values); } /**