Skip to content

Commit

Permalink
fix broken taglist in relation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Dec 5, 2024
1 parent b5ccea7 commit b78a938
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions modules/backend/formwidgets/TagList.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,28 @@ protected function hydrateRelationSaveValue($names): ?array
$names = [$names];
}

list($model, $attribute) = $this->resolveModelAttribute($this->formField->fieldName);
$relation = $model->{$attribute}();
$relationModel = $this->getRelationModel();
$existingTags = $relationModel
->whereIn($this->nameFrom, $names)
->lists($this->nameFrom, $relationModel->getKeyName())
;

$existingTags = $relation->lists($this->nameFrom, $relationModel->getKeyName());

$newTags = $this->customTags ? array_diff($names, $existingTags) : [];
$deletedTags = $this->customTags ? array_diff($existingTags, $names) : [];

foreach ($newTags as $newTag) {
$newModel = new $relationModel;
$newModel->{$this->nameFrom} = $newTag;
$newModel->save();
$newModel = $relation->create([$this->nameFrom => $newTag]);
$existingTags[$newModel->getKey()] = $newTag;
}

if ($deletedTags) {
$deletedKeys = array_keys($deletedTags);
$relation->whereIn($relationModel->getKeyName(), $deletedKeys)->delete();
foreach ($deletedTags as $id) {
unset($existingTags[$id]);
}
}

return array_keys($existingTags);
}

Expand Down

0 comments on commit b78a938

Please sign in to comment.