Skip to content

Commit

Permalink
make it work for relations using pivot table as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Dec 7, 2024
1 parent 68c3af8 commit 76a31b3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
23 changes: 19 additions & 4 deletions modules/backend/formwidgets/TagList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
}
Expand Down
7 changes: 1 addition & 6 deletions modules/backend/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions modules/backend/widgets/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76a31b3

Please sign in to comment.