From 7deb0848ca10c5b285645dc530c1675d2cb415f8 Mon Sep 17 00:00:00 2001 From: Gilles Felix Date: Fri, 26 Jan 2024 16:30:46 +0100 Subject: [PATCH] Fix use of buildTree --- src/Helpers/MenuHelper.php | 21 +++++++++++++-------- src/LaravelNovaMenuService.php | 8 ++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Helpers/MenuHelper.php b/src/Helpers/MenuHelper.php index 8ba9c71..bdda645 100644 --- a/src/Helpers/MenuHelper.php +++ b/src/Helpers/MenuHelper.php @@ -104,13 +104,7 @@ public static function displayMenu(Menu|string $slug_or_menu, string $view = nul } $tree = Cache::rememberForever($menu->getTreeCacheName(), static function () use ($menu) { - $items = MenuItem::scoped(['menu_id' => $menu->id]) - ->withDepth() - ->defaultOrder() - ->get() - ->toTree(); - - return app()->get('laravel-nova-menu')->buildTree($items); + return app()->get('laravel-nova-menu')->buildTree($menu); }); return (string) view($view ?? 'laravel-nova-menu::front/menu', [ @@ -119,7 +113,18 @@ public static function displayMenu(Menu|string $slug_or_menu, string $view = nul ]); } - public static function getTree(Collection $items): array + public static function buildTree(Menu $menu): array + { + $items = MenuItem::scoped(['menu_id' => $menu->id]) + ->withDepth() + ->defaultOrder() + ->get() + ->toTree(); + + return static::getTree($items); + } + + protected static function getTree(Collection $items): array { $tree = []; foreach ($items as $menuItem) { diff --git a/src/LaravelNovaMenuService.php b/src/LaravelNovaMenuService.php index b223835..09585a8 100644 --- a/src/LaravelNovaMenuService.php +++ b/src/LaravelNovaMenuService.php @@ -19,7 +19,7 @@ class LaravelNovaMenuService /** * The callback that should be used to build tree * - * @var (Closure(Collection):array)|null + * @var (Closure(Menu):array)|null */ protected ?Closure $buildTreeUsing = null; @@ -39,7 +39,7 @@ public function setTreeUsing(Closure $callback): self /** * Register callback that should be used to build tree * - * @param Closure(Collection):array $callback + * @param Closure(Menu):array $callback * @return $this */ public function setBuildTreeUsing(Closure $callback): self @@ -62,8 +62,8 @@ public function tree(Menu $menu, array $tree): array /** * Return the tree that will be passed to the view */ - public function buildTree(Collection $items): array + public function buildTree(Menu $menu): array { - return ($this->buildTreeUsing ?: [MenuHelper::class, 'getTree'])($items); + return ($this->buildTreeUsing ?: [MenuHelper::class, 'buildTree'])($menu); } }