Skip to content

Commit

Permalink
Merge pull request #7 from Log1x/feat/override-resource
Browse files Browse the repository at this point in the history
πŸ§‘β€πŸ’» Add support for overriding the menu resource
  • Loading branch information
datlechin authored Aug 9, 2024
2 parents 33add32 + 4517cdf commit 58d3110
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ $panel
])
->addMenuItemFields([
TextInput::make('classes'),
]),
])
)
```

Expand Down Expand Up @@ -219,6 +219,42 @@ return new class extends Migration

Once done, simply run `php artisan migrate`.

### Customizing the Resource

Out of the box, a default Menu Resource is registered with Filament when registering the plugin in the admin provider. This resource can be extended and overridden allowing for more fine-grained control.

Start by extending the `Datlechin\FilamentMenuBuilder\Resources\MenuResource` class in your application. Below is an example:

```php
namespace App\Filament\Plugins\Resources;

use Datlechin\FilamentMenuBuilder\Resources\MenuResource as BaseMenuResource;

class MenuResource extends BaseMenuResource
{
protected static ?string $navigationGroup = 'Navigation';

public static function getNavigationBadge(): ?string
{
return number_format(static::getModel()::count());
}
}
```

Now pass the custom resource to `usingResource` while registering the plugin with the panel:

```php
use App\Filament\Plugins\Resources\MenuResource;
use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin;

$panel
...
->plugin(
FilamentMenuBuilderPlugin::make()
->usingResource(MenuResource::class)
)
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
18 changes: 15 additions & 3 deletions src/FilamentMenuBuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class FilamentMenuBuilderPlugin implements Plugin
{
protected string $resource = MenuResource::class;

protected array $locations = [];

protected array | Closure $menuFields = [];
Expand All @@ -29,9 +31,7 @@ public function getId(): string

public function register(Panel $panel): void
{
$panel->resources([
MenuResource::class,
]);
$panel->resources([$this->getResource()]);
}

public function boot(Panel $panel): void
Expand All @@ -52,6 +52,13 @@ public static function get(): static
return $plugin;
}

public function usingResource(string $resource): static
{
$this->resource = $resource;

return $this;
}

public function addLocation(string $key, string $label): static
{
$this->locations[$key] = $label;
Expand Down Expand Up @@ -94,6 +101,11 @@ public function addMenuItemFields(array | Closure $schema): static
return $this;
}

public function getResource(): string
{
return $this->resource;
}

/**
* @return MenuPanel[]
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Resources/MenuResource/Pages/EditMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Datlechin\FilamentMenuBuilder\Resources\MenuResource\Pages;

use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin;
use Datlechin\FilamentMenuBuilder\Models\Menu;
use Datlechin\FilamentMenuBuilder\Resources\MenuResource;
use Filament\Actions;
use Filament\Forms\Components\Section;
use Filament\Forms\Form;
Expand All @@ -15,10 +15,13 @@

class EditMenu extends EditRecord
{
protected static string $resource = MenuResource::class;

protected static string $view = 'filament-menu-builder::edit-record';

public static function getResource(): string
{
return FilamentMenuBuilderPlugin::get()->getResource();
}

public function form(Form $form): Form
{
return $form
Expand Down
7 changes: 5 additions & 2 deletions src/Resources/MenuResource/Pages/ListMenus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Datlechin\FilamentMenuBuilder\Resources\MenuResource\Pages;

use Datlechin\FilamentMenuBuilder\Resources\MenuResource;
use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListMenus extends ListRecords
{
protected static string $resource = MenuResource::class;
public static function getResource(): string
{
return FilamentMenuBuilderPlugin::get()->getResource();
}

protected function getHeaderActions(): array
{
Expand Down

0 comments on commit 58d3110

Please sign in to comment.