From ba23774772cc9003c2157da6b971b1dfd89f08f3 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Tue, 8 Oct 2024 08:04:33 +0000 Subject: [PATCH] Multiple fixes to Tag list form widget --- modules/backend/classes/FormField.php | 4 +- modules/backend/formwidgets/TagList.php | 46 +++++++++++++------ .../formwidgets/taglist/partials/_taglist.php | 19 ++------ 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 1f63e72ec8..b592bd1ef0 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -19,9 +19,9 @@ class FormField { /** - * @var int Value returned when the form field should not contribute any save data. + * @var string Value returned when the form field should not contribute any save data. */ - const NO_SAVE_DATA = -1; + const NO_SAVE_DATA = "\0"; /** * @var string A special character in yaml config files to indicate a field higher in hierarchy diff --git a/modules/backend/formwidgets/TagList.php b/modules/backend/formwidgets/TagList.php index 7159fbb161..c9a115367f 100644 --- a/modules/backend/formwidgets/TagList.php +++ b/modules/backend/formwidgets/TagList.php @@ -21,7 +21,7 @@ class TagList extends FormWidgetBase // /** - * @var string Tag separator: space, comma. + * @var string Tag separator: space, comma, semicolon. */ public $separator = 'comma'; @@ -123,23 +123,22 @@ public function getSaveValue($value) * Returns an array suitable for saving against a relation (array of keys). * This method also creates non-existent tags. */ - protected function hydrateRelationSaveValue($names): ?array + protected function hydrateRelationSaveValue($items): ?array { - if (!$names) { - return $names; + if (empty($items)) { + return []; } - if (!is_array($names)) { - $names = [$names]; + if (!is_array($items)) { + $items = [$items]; } $relationModel = $this->getRelationModel(); $existingTags = $relationModel - ->whereIn($this->nameFrom, $names) - ->lists($this->nameFrom, $relationModel->getKeyName()) - ; + ->whereIn(($this->useKey) ? $relationModel->getKeyName() : $this->nameFrom, $items) + ->lists($this->nameFrom, $relationModel->getKeyName()); - $newTags = $this->customTags ? array_diff($names, $existingTags) : []; + $newTags = $this->customTags ? array_diff($items, $existingTags) : []; foreach ($newTags as $newTag) { $newModel = new $relationModel; @@ -159,7 +158,24 @@ public function getLoadValue() $value = parent::getLoadValue(); if ($this->mode === static::MODE_RELATION) { - return $this->getRelationObject()->lists($this->nameFrom); + // Load default if necessary + if (!$this->model->exists) { + $default = $this->formField->getDefaultFromData($this->data ?: $this->model); + + if (!is_null($default)) { + return ($this->useKey) + ? $this->getRelationModel()->whereIn($this->getRelationModel()->getKeyName(), $default)->lists($this->getRelationModel()->getKeyName()) + : $this->getRelationModel()->whereIn($this->nameFrom, $default)->lists($this->nameFrom); + } + } + + if (empty($value)) { + return []; + } + + return $this->getRelationModel()->whereIn($this->getRelationModel()->getKeyName(), $value)->lists(($this->useKey) + ? $this->getRelationModel()->getKeyName() + : $this->nameFrom); } return $this->mode === static::MODE_STRING @@ -183,7 +199,7 @@ public function getFieldOptions() // by joining its pivot table. Remove all joins from the query. $query->getQuery()->getQuery()->joins = []; - return $query->lists($this->nameFrom); + return $query->lists($this->nameFrom, ($this->useKey) ? $this->getRelationModel()->getKeyName() : null); }); } @@ -214,10 +230,12 @@ protected function getCustomSeparators() protected function getSeparatorCharacter() { switch (strtolower($this->separator)) { - case 'comma': - return ','; + case 'semicolon': + return ';'; case 'space': return ' '; + default: + return ','; } } } diff --git a/modules/backend/formwidgets/taglist/partials/_taglist.php b/modules/backend/formwidgets/taglist/partials/_taglist.php index 9d3e0080bb..ae19bf400c 100644 --- a/modules/backend/formwidgets/taglist/partials/_taglist.php +++ b/modules/backend/formwidgets/taglist/partials/_taglist.php @@ -1,6 +1,6 @@ $option) { @@ -19,20 +19,11 @@
  • - value)): ?> - - - - - - +