From 76a31b39258fdccb89928abf0a595f09ea08f016 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 7 Dec 2024 09:54:04 -0500 Subject: [PATCH] make it work for relations using pivot table as well --- modules/backend/formwidgets/TagList.php | 23 +++++++++++++++++++---- modules/backend/routes.php | 7 +------ modules/backend/widgets/Form.php | 6 +++--- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/modules/backend/formwidgets/TagList.php b/modules/backend/formwidgets/TagList.php index 6506e5907a..d9907e083d 100644 --- a/modules/backend/formwidgets/TagList.php +++ b/modules/backend/formwidgets/TagList.php @@ -4,6 +4,8 @@ use Backend\Classes\FormWidgetBase; use Illuminate\Database\Eloquent\Relations\Relation as RelationBase; +use Winter\Storm\Database\Relations\BelongsToMany; +use Winter\Storm\Database\Relations\MorphToMany; /** * Tag List Form Widget @@ -132,19 +134,32 @@ protected function hydrateRelationSaveValue($names): ?array $relation = $this->getRelationObject(); $relationModel = $this->getRelationModel(); - $existingTags = $relation->lists($this->nameFrom, $relationModel->getKeyName()); + $keyName = $relationModel->getKeyName(); + $pivot = in_array(get_class($relation), [BelongsToMany::class, MorphToMany::class]); + + if ($pivot) { + $existingTags = $relationModel->whereIn($this->nameFrom, $names)->lists($this->nameFrom, $keyName); + } else { + $existingTags = $relation->lists($this->nameFrom, $keyName); + } $newTags = $this->customTags ? array_diff($names, $existingTags) : []; $deletedTags = $this->customTags ? array_diff($existingTags, $names) : []; foreach ($newTags as $newTag) { - $newModel = $relation->create([$this->nameFrom => $newTag]); + if ($pivot) { + $newModel = new $relationModel; + $newModel->{$this->nameFrom} = $newTag; + $newModel->save(); + } else { + $newModel = $relation->create([$this->nameFrom => $newTag]); + } $existingTags[$newModel->getKey()] = $newTag; } - if ($deletedTags) { + if (!$pivot && $deletedTags) { $deletedKeys = array_keys($deletedTags); - $relation->whereIn($relationModel->getKeyName(), $deletedKeys)->delete(); + $relation->whereIn($keyName, $deletedKeys)->delete(); foreach ($deletedTags as $id) { unset($existingTags[$id]); } diff --git a/modules/backend/routes.php b/modules/backend/routes.php index 7ca92974df..f4e670054d 100644 --- a/modules/backend/routes.php +++ b/modules/backend/routes.php @@ -20,7 +20,7 @@ Event::fire('backend.beforeRoute'); /* - * Other pages + * Route everything to `Backend\Classes\BackendController`, which itself acts as a router for the Backend. */ Route::group([ 'middleware' => ['web'], @@ -29,11 +29,6 @@ Route::any('{slug?}', 'Backend\Classes\BackendController@run')->where('slug', '(.*)?'); }); - /* - * Entry point - */ - Route::any(Config::get('cms.backendUri', 'backend'), 'Backend\Classes\BackendController@run')->middleware('web'); - /** * @event backend.route * Fires after backend routes have been added diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 387b12eb67..7cbb8cb242 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -1228,10 +1228,10 @@ public function getSaveData(): array $fieldValue = $this->dataArrayGet($result, $parts, FormField::NO_SAVE_DATA); if ($fieldValue === FormField::NO_SAVE_DATA) { if ($widget->getConfig('type') === 'taglist' && $widget->getConfig('mode') === 'relation') { - // let the TagList remove its relation items - $widget->getSaveValue(array()); + $fieldValue = []; + } else { + continue; } - continue; } // Exclude fields where the widget returns NO_SAVE_DATA