Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions components/MenuManagerBundle/src/bundle/Entity/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,17 @@ public function __toString()

public function assignPositions(): void
{
$rootItems = $this->getItemsByParent();
/** @var MenuItem[] $childrens */
$childrens = $this->items->filter(function (MenuItem $item) {
return null === $item->getParent();
})->getValues();

usort($childrens, function (MenuItem $itemA, MenuItem $itemB) {
return $itemA->getPosition() <=> $itemB->getPosition();
});

$position = 0;
foreach ($rootItems as $item) {
foreach ($childrens as $item) {
$item->setPosition($position);
$item->assignPositions();
++$position;
Expand Down
8 changes: 7 additions & 1 deletion components/MenuManagerBundle/src/bundle/Entity/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ public function update(array $properties): void

public function assignPositions(): void
{
$childrens = $this->getChildrens();
/** @var MenuItem[] $childrens */
$childrens = $this->getChildrens()->getValues();

usort($childrens, function (MenuItem $itemA, MenuItem $itemB) {
return $itemA->getPosition() <=> $itemB->getPosition();
});

$position = 0;
foreach ($childrens as $child) {
$child->setPosition($position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function fromHash($hash): ?MenuItem
$currentParent = $menuItem->getParent();
if ($currentParent && (!isset($hash['parentId']) || null === $hash['parentId'])) {
$currentParent->removeChildren($menuItem);
} elseif ($hash['parentId'] && (!$currentParent || $currentParent->getId() !== $hash['parentId'])) {
} elseif ($hash['parentId'] && (!$currentParent || $currentParent->getId() !== (int) $hash['parentId'])) {
$parent = $menuItemRepo->find($hash['parentId']);
if ($parent) {
$parent->addChildren($menuItem);
Expand Down