From 5cadd770a09e36d883e6c98d065084ff54a88db5 Mon Sep 17 00:00:00 2001 From: Tin Date: Thu, 10 Apr 2025 15:20:58 +0200 Subject: [PATCH] Fix: Fix a bug where Saving entry would move it to the last place in the parent hierarchy --- .../CP/Collections/EntriesController.php | 8 +++++--- src/Revisions/Revisable.php | 8 +++++--- src/Structures/Page.php | 5 +++++ src/Structures/Tree.php | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Http/Controllers/CP/Collections/EntriesController.php b/src/Http/Controllers/CP/Collections/EntriesController.php index 53180eb1b4..b429adede9 100644 --- a/src/Http/Controllers/CP/Collections/EntriesController.php +++ b/src/Http/Controllers/CP/Collections/EntriesController.php @@ -244,9 +244,11 @@ public function update(Request $request, $collection, $entry) $parent = null; } - $tree - ->move($entry->id(), $parent) - ->save(); + if(!$tree->isAlreadyInParent($entry->id(), $parent)){ + $tree + ->move($entry->id(), $parent) + ->save(); + } }); $entry->remove('parent'); diff --git a/src/Revisions/Revisable.php b/src/Revisions/Revisable.php index 2c1dfd0efc..0ff38ad832 100644 --- a/src/Revisions/Revisable.php +++ b/src/Revisions/Revisable.php @@ -94,9 +94,11 @@ public function publishWorkingCopy($options = []) $parent = null; } - $tree - ->move($this->id(), $parent) - ->save(); + if($tree->isAlreadyInParent($item->id(), $parent)){ + $tree + ->move($this->id(), $parent) + ->save(); + } } $item diff --git a/src/Structures/Page.php b/src/Structures/Page.php index afb23c50ac..c85f10c2a6 100644 --- a/src/Structures/Page.php +++ b/src/Structures/Page.php @@ -284,6 +284,11 @@ public function setChildren(array $children): self return $this; } + public function getChildren():array + { + return $this->children; + } + public function setPageData(array $data): self { $this->data = $data; diff --git a/src/Structures/Tree.php b/src/Structures/Tree.php index 1caff9d5d4..3116caaa45 100644 --- a/src/Structures/Tree.php +++ b/src/Structures/Tree.php @@ -394,4 +394,18 @@ public function getCurrentDirtyStateAttributes(): array 'tree' => $this->tree, ]; } + + public function isAlreadyInParent($entryId, $parentId = null): bool + { + if ($parentId) { + $parentPage = $this->find($parentId); + if (! $parentPage) { + return false; + } + $childrenIds = Arr::pluck($parentPage->getChildren(), $this->idKey()); + } else { + $childrenIds = collect($this->tree)->pluck($this->idKey())->all(); + } + return in_array($entryId, $childrenIds); + } }