Skip to content

Commit a78035d

Browse files
committed
Issue krayin#1654 fixed.
1 parent e1ec37c commit a78035d

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

packages/Webkul/Core/src/Menu.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ public function getItems(?string $area = null, string $key = ''): Collection
8080
$this->prepareMenuItems();
8181
}
8282

83-
$items = collect($this->items)->sortBy(fn ($item) => $item->getPosition());
84-
85-
return $items;
83+
return collect($this->removeUnauthorizedMenuItem())
84+
->sortBy(fn ($item) => $item->getPosition());
8685
}
8786

8887
/**
@@ -185,4 +184,30 @@ private function findMatchingItem($items, $currentKey): ?MenuItem
185184

186185
return null;
187186
}
187+
188+
/**
189+
* Remove unauthorized menu item.
190+
*/
191+
private function removeUnauthorizedMenuItem(): array
192+
{
193+
return collect($this->items)->map(function ($item) {
194+
$this->removeChildrenUnauthorizedMenuItem($item);
195+
196+
return $item;
197+
})->toArray();
198+
}
199+
200+
/**
201+
* Remove unauthorized menuItem's children. This will handle all levels.
202+
*/
203+
private function removeChildrenUnauthorizedMenuItem(MenuItem &$menuItem): void
204+
{
205+
if ($menuItem->haveChildren()) {
206+
$firstChildrenItem = $menuItem->getChildren()->first();
207+
208+
$menuItem->setRoute($firstChildrenItem->getRoute());
209+
210+
$this->removeChildrenUnauthorizedMenuItem($firstChildrenItem);
211+
}
212+
}
188213
}

0 commit comments

Comments
 (0)