F
Filament2mo ago
T

Navigation order - mixing groups and items

I know a variety of similar questions have been asked and Ive read them but I think Im trying the selected answer and not getting the suggested response. Im trying to make a custom menu so that i have 3 navigation items going to urls, then a group (My Account) and then 2 further Navigation items to log out and to go back to a previous website. A cut down version of my requirements is basically like this
->navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder->item(
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->sort(1)
->url(fn (): string => Dashboard::getUrl()),

)
->group(NavigationGroup::make('My Group')
->items([
NavigationItem::make('Other Item')->url('/other')
->sort(2),
])
)
->item(
NavigationItem::make('Item 2')
->icon('heroicon-o-home')
->sort(3)
->url("/item-2"),

);
})
->navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder->item(
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->sort(1)
->url(fn (): string => Dashboard::getUrl()),

)
->group(NavigationGroup::make('My Group')
->items([
NavigationItem::make('Other Item')->url('/other')
->sort(2),
])
)
->item(
NavigationItem::make('Item 2')
->icon('heroicon-o-home')
->sort(3)
->url("/item-2"),

);
})
However what I get is Dashboard, Item 2 and then the group. Is there something I'm doing wrong ? Thanks for any pointers
Solution:
one off items are put into their own 'group' and prepended to the navigation collection, but you should be able to achieve what you want with this (but the spacing might be weird, but should be adjustable with some custom CSS): ```php ->navigation(function (NavigationBuilder $builder): NavigationBuilder { return $builder...
Jump to solution
7 Replies
Dennis Koch
Dennis Koch2mo ago
Groups are always sorted first. Then items. And items without a group are grouped into one
T
TOP2mo ago
@Dennis Koch Thanks for the reply. If groups are sorted first, how do they appear beneath my items ? Is there any way of getting
item item item group group item item ? or will it always do item x5 group x2 ?
Dennis Koch
Dennis Koch2mo ago
I think the algorithm is: 1) Everything is sorted. 2) Then everything is grouped. Items with no group will be in their own category. => Therefore it will always be the second one. You might be able to overwrite that method, I don't know where it is though.
Solution
awcodes
awcodes2mo ago
one off items are put into their own 'group' and prepended to the navigation collection, but you should be able to achieve what you want with this (but the spacing might be weird, but should be adjustable with some custom CSS):
->navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder
->item(
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->url('/'),
)
->group(NavigationGroup::make('My Group')
->items([
NavigationItem::make('Other Item')->url('/other')
->sort(2),
])
)
->group(NavigationGroup::make()
->items([
NavigationItem::make('Item 2')
->icon('heroicon-o-home')
->url("/item-2"),
NavigationItem::make('Item 3')
->icon('heroicon-o-home')
->url("/item-3"),
])
);
})
->navigation(function (NavigationBuilder $builder): NavigationBuilder {
return $builder
->item(
NavigationItem::make('Dashboard')
->icon('heroicon-o-home')
->url('/'),
)
->group(NavigationGroup::make('My Group')
->items([
NavigationItem::make('Other Item')->url('/other')
->sort(2),
])
)
->group(NavigationGroup::make()
->items([
NavigationItem::make('Item 2')
->icon('heroicon-o-home')
->url("/item-2"),
NavigationItem::make('Item 3')
->icon('heroicon-o-home')
->url("/item-3"),
])
);
})
awcodes
awcodes2mo ago
This is probably not the intended use of the NavigationBuilder grouping logic, though, so might be a hack that could potentially break in the future.
awcodes
awcodes2mo ago
No description
T
TOP2mo ago
Thanks very much Adam. Its for our own system rather than a customer so if it does break in future then i'll revisit. Will apply that now and see how I go. That's very much appreciated.

Did you find this page helpful?