Skip to content

Commit

Permalink
Fix use of buildTree
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Jan 26, 2024
1 parent 1b6b183 commit 7deb084
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions src/Helpers/MenuHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/LaravelNovaMenuService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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);
}
}

0 comments on commit 7deb084

Please sign in to comment.