From a1a834192940c01144c310bae2fc663d59fdb954 Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Thu, 5 Oct 2017 14:58:27 +1300 Subject: [PATCH] Fix refactor TreeMultiselectField to be clearable if nothing is selected --- src/Forms/TreeMultiselectField.php | 47 +++++++++++++++--------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Forms/TreeMultiselectField.php b/src/Forms/TreeMultiselectField.php index 4f6c3cc5c..2c2888b1e 100644 --- a/src/Forms/TreeMultiselectField.php +++ b/src/Forms/TreeMultiselectField.php @@ -215,38 +215,37 @@ class TreeMultiselectField extends TreeDropdownField */ public function saveInto(DataObjectInterface $record) { + $items = []; + $fieldName = $this->name; + $saveDest = $record->$fieldName(); + + if (!$saveDest) { + $recordClass = get_class($record); + user_error( + "TreeMultiselectField::saveInto() Field '$fieldName' not found on" + . " {$recordClass}.{$record->ID}", + E_USER_ERROR + ); + } + // Detect whether this field has actually been updated if ($this->value !== 'unchanged') { - $items = []; - - $fieldName = $this->name; - $saveDest = $record->$fieldName(); - if (!$saveDest) { - $recordClass = get_class($record); - user_error( - "TreeMultiselectField::saveInto() Field '$fieldName' not found on" - . " {$recordClass}.{$record->ID}", - E_USER_ERROR - ); - } - if (is_array($this->value)) { $items = $this->value; } elseif ($this->value) { $items = preg_split("/ *, */", trim($this->value)); } - - // Allows you to modify the items on your object before save - $funcName = "onChange$fieldName"; - if ($record->hasMethod($funcName)) { - $result = $record->$funcName($items); - if (!$result) { - return; - } - } - - $saveDest->setByIDList($items); } + + // Allows you to modify the items on your object before save + $funcName = "onChange$fieldName"; + if ($record->hasMethod($funcName)) { + $result = $record->$funcName($items); + if (!$result) { + return; + } + } + $saveDest->setByIDList($items); } /**