ryangjchandler / filament-navigation
Build structured navigation menus in Filament.
Fund package maintenance!
ryangjchandler
Installs: 70 867
Dependents: 2
Suggesters: 0
Security: 0
Stars: 169
Watchers: 3
Forks: 65
Open Issues: 15
Requires
- php: ^8.1
- doctrine/dbal: ^3.5.1
- filament/filament: ^3.0
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^7.0|^8.0
- nunomaduro/larastan: ^2.0.1
- orchestra/canvas: ^8.0
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5|^10.0
- spatie/laravel-ray: ^1.26
- dev-main
- v1.0.0-beta5
- v1.0.0-beta4
- v1.0.0-beta3
- v1.0.0-beta2
- v1.0.0-beta1
- v0.5.0
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-5
- dev-dependabot/github_actions/actions/checkout-4
- dev-dependabot/github_actions/ramsey/composer-install-2
This package is auto-updated.
Last update: 2024-12-13 03:27:35 UTC
README
This plugin for Filament provides a Navigation
resource that lets you build structural navigation menus with a clean drag-and-drop UI.
Installation
Begin by installing this package via Composer:
composer require ryangjchandler/filament-navigation
Run migrations.
php artisan migrate
Publish the package's assets:
php artisan filament:assets
Usage
You first need to register the plugin with Filament. This can be done inside of your PanelProvider
, e.g. AdminPanelProvider
.
use RyanChandler\FilamentNavigation\FilamentNavigation; return $panel ->plugin(FilamentNavigation::make());
If you wish to customise the navigation group, sort or icon, you can use the NavigationResource::navigationGroup()
, NavigationResource::navigationSort()
and NavigationResource::navigationIcon()
methods.
Data structure
Each navigation menu is required to have a name
and handle
. The handle
should be unique and used to retrieve the menu.
Items are stored inside of a JSON column called items
. This is a recursive data structure that looks like:
[ { "label": "Home", "type": "external-link", "data": { "url": "/", "target": "_blank", }, "children": [ // ... ], } ]
The recursive structure makes it really simple to render nested menus / dropdowns:
<ul> @foreach($menu->items as $item) <li> {{ $item['label'] }} @if($item['children']) <ul> {{-- Render the item's children here... --}} </ul> @endif </li> @endforeach </ul>
Retrieving a navigation object
To retrieve a navigation object, provide the handle to the RyanChandler\FilamentNavigation\Models\Navigation::fromHandle()
method.
use RyanChandler\FilamentNavigation\Models\Navigation; $menu = Navigation::fromHandle('main-menu');
Custom item types
Out of the box, this plugin comes with a single "item type" called "External link". This item type expects a URL to be provided and an optional "target" (same tab or new tab).
It's possible to extend the plugin with custom item types. Custom item types have a name and an array of Filament field objects (or a Closure
that produces an array) that will be displayed inside of the "Item" modal.
This API allows you to deeply integrate navigation menus with your application's own entities and models.
return $panel ->plugin( FilamentNavigation::make() ->itemType('post', [ Select::make('post_id') ->//... ]) );
All custom fields for the item type can be found inside of the data
property on the item.
Global custom fields
There might be cases where you want all item types to have an additional set of fields. This is useful for classes, custom IDs and more.
To register global custom fields, use the withExtraFields()
method on the plugin object.
return $panel ->plugin( FilamentNavigation::make() ->withExtraFields([ TextInput::make('classes'), ]), );
The Navigation
field type
This plugin also provides a custom Filament field that can be used to search and select a navigation menu inside other forms and resources.
use RyanChandler\FilamentNavigation\Filament\Fields\NavigationSelect; ->schema([ NavigationSelect::make('navigation_id'), ])
By default, this field will not be searchable and the value for each option will be the menu id
.
To make the field searchable, call the ->searchable()
method.
->schema([ NavigationSelect::make('navigation_id') ->searchable(), ])
If you wish to change the value for each option, call the ->optionValue()
method.
->schema([ NavigationSelect::make('navigation_id') ->optionValue('handle'), ])
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.