Skip to content

Commit 68b038d

Browse files
committed
fix: issue with menu item position and parent switch
1 parent bcbf88a commit 68b038d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

components/MenuManagerBundle/src/bundle/Entity/Menu.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,17 @@ public function __toString()
181181

182182
public function assignPositions(): void
183183
{
184-
$rootItems = $this->getItemsByParent();
184+
/** @var MenuItem[] $childrens */
185+
$childrens = $this->items->filter(function (MenuItem $item) {
186+
return null === $item->getParent();
187+
})->getValues();
188+
189+
usort($childrens, function (MenuItem $a, MenuItem $b) {
190+
return $a->getPosition() <=> $b->getPosition();
191+
});
192+
185193
$position = 0;
186-
foreach ($rootItems as $item) {
194+
foreach ($childrens as $item) {
187195
$item->setPosition($position);
188196
$item->assignPositions();
189197
++$position;

components/MenuManagerBundle/src/bundle/Entity/MenuItem.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,13 @@ public function update(array $properties): void
315315

316316
public function assignPositions(): void
317317
{
318-
$childrens = $this->getChildrens();
318+
/** @var MenuItem[] $childrens */
319+
$childrens = $this->getChildrens()->getValues();
320+
321+
usort($childrens, function (MenuItem $a, MenuItem $b) {
322+
return $a->getPosition() <=> $b->getPosition();
323+
});
324+
319325
$position = 0;
320326
foreach ($childrens as $child) {
321327
$child->setPosition($position);

components/MenuManagerBundle/src/lib/MenuItem/Type/DefaultMenuItemType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function fromHash($hash): ?MenuItem
8888
$currentParent = $menuItem->getParent();
8989
if ($currentParent && (!isset($hash['parentId']) || null === $hash['parentId'])) {
9090
$currentParent->removeChildren($menuItem);
91-
} elseif ($hash['parentId'] && (!$currentParent || $currentParent->getId() !== $hash['parentId'])) {
91+
} elseif ($hash['parentId'] && (!$currentParent || $currentParent->getId() !== (int) $hash['parentId'])) {
9292
$parent = $menuItemRepo->find($hash['parentId']);
9393
if ($parent) {
9494
$parent->addChildren($menuItem);

0 commit comments

Comments
 (0)