Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusjp committed Feb 9, 2024
0 parents commit a309b75
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
63 changes: 63 additions & 0 deletions README.md
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);
}
```
21 changes: 21 additions & 0 deletions composer.json
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"
}
}
44 changes: 44 additions & 0 deletions src/Navigation.php
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;
}

0 comments on commit a309b75

Please sign in to comment.