-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a309b75
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
A NavigationMenu generator for (Laravel) Filament | ||
================================================== | ||
Because of how Filament wants you to build a menu, by setting sortOrders per Resource, you have to (eventually) keep an extensive mind map of how your NavigationMenu is structured. | ||
|
||
Because this is quite strenuous, we've created a package that will generate a NavigationMenu for you based on an array that holds all your Resources and/or Pages to create a simple 1-look view of your menu. | ||
|
||
Installation | ||
============ | ||
Make sure Composer is installed globally, as explained in the | ||
[installation chapter](https://getcomposer.org/doc/00-intro.md) | ||
of the Composer documentation. | ||
|
||
### Step 1: Require the module | ||
Open a command console, enter your project directory and execute: | ||
|
||
```console | ||
$ composer require coddin-web/filament-menu-generator | ||
``` | ||
|
||
### Step 2: using the module | ||
The abstract class `Navigation` should be extended by a class per Panel that you would like to supervise that Panel's NavigationMenu. | ||
|
||
e.g. | ||
|
||
```php | ||
use CoddinWeb\FilamentMenuGenerator\Navigation; | ||
|
||
final class AdminNavigation extends Navigation | ||
{ | ||
#[\Override] | ||
public static function getMenu(): array | ||
{ | ||
return [ | ||
CustomerResource::class => [ | ||
'group' => 'Management' | ||
], | ||
InvoiceResource::class => [ | ||
'group' => 'Management' | ||
], | ||
|
||
UserResource::class => [ | ||
'group' => 'Administration' | ||
], | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
And then within your specific Panel, you can call the `AdminNavigation` class to generate the menu. | ||
|
||
```php | ||
// ... \App\Filament\Admin\Resources\CustomerResource.php | ||
|
||
public static function getNavigationGroup(): string | ||
{ | ||
return AdminNavigation::group(self::class); | ||
} | ||
|
||
public static function getNavigationSort(): int | ||
{ | ||
return AdminNavigation::sort(self::class); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "coddin-web/filament-menu-generator", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"CoddinWeb\\FilamentMenuGenerator\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Marius", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": "^8.2", | ||
"filament/filament": "^3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CoddinWeb\FilamentMenuGenerator; | ||
|
||
use Filament\Pages\Page; | ||
use Filament\Resources\Resource; | ||
|
||
abstract class Navigation | ||
{ | ||
/** | ||
* @param class-string<Resource|Page> $type | ||
*/ | ||
final public static function group(string $type): string | ||
{ | ||
return \strval(__(static::getMenu()[$type]['group'])); | ||
} | ||
|
||
/** | ||
* @param class-string<Resource|Page> $type | ||
*/ | ||
final public static function sort(string $type): int | ||
{ | ||
$menuKeys = \array_keys(static::getMenu()); | ||
$key = \array_search($type, $menuKeys, true); | ||
|
||
if (!\is_int($key)) { | ||
throw new \LogicException( | ||
\sprintf( | ||
'The class %s is not in the menu order.', | ||
$type, | ||
), | ||
); | ||
} | ||
|
||
return ($key + 1); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Resource|Page>, array{group: string}> | ||
*/ | ||
abstract public static function getMenu(): array; | ||
} |