diff --git a/README.md b/README.md index 1f4879a..688e394 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,69 @@ $panel ![Model Menu Panel](./art/model-menu.png) +### Custom Fields + +In some cases, you may want to extend menu and menu items with custom fields. To do this, start by passing an array of form components to the `addMenuFields` and `addMenuItemFields` methods when registering the plugin: + +```php +use Filament\Forms\Components\TextInput; +use Filament\Forms\Components\Toggle; +use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin; + +$panel + ... + ->plugin( + FilamentMenuBuilderPlugin::make() + ->addMenuFields([ + Toggle::make('is_logged_in'), + ]) + ->addMenuItemFields([ + TextInput::make('classes'), + ]), + ) +``` + +Next, create a migration adding the additional columns to the appropriate tables: + +```php +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +return new class extends Migration +{ + /** + * Run the migrations. + */ + public function up(): void + { + Schema::table(config('filament-menu-builder.tables.menus'), function (Blueprint $table) { + $table->boolean('is_logged_in')->default(false); + }); + + Schema::table(config('filament-menu-builder.tables.menu_items'), function (Blueprint $table) { + $table->string('classes')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table(config('filament-menu-builder.tables.menus'), function (Blueprint $table) { + $table->dropColumn('is_logged_in'); + }); + + Schema::table(config('filament-menu-builder.tables.menu_items'), function (Blueprint $table) { + $table->dropColumn('classes'); + }); + } +} +``` + +Once done, simply run `php artisan migrate`. + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. diff --git a/src/FilamentMenuBuilderPlugin.php b/src/FilamentMenuBuilderPlugin.php index 847b239..a08333c 100644 --- a/src/FilamentMenuBuilderPlugin.php +++ b/src/FilamentMenuBuilderPlugin.php @@ -13,6 +13,10 @@ class FilamentMenuBuilderPlugin implements Plugin { protected array $locations = []; + protected array | Closure $menuFields = []; + + protected array | Closure $menuItemFields = []; + /** * @var MenuPanel[] */ @@ -76,6 +80,20 @@ public function addMenuPanels(array $menuPanels): static return $this; } + public function addMenuFields(array | Closure $schema): static + { + $this->menuFields = $schema; + + return $this; + } + + public function addMenuItemFields(array | Closure $schema): static + { + $this->menuItemFields = $schema; + + return $this; + } + /** * @return MenuPanel[] */ @@ -90,4 +108,14 @@ public function getLocations(): array { return $this->locations; } + + public function getMenuFields(): array | Closure + { + return $this->menuFields; + } + + public function getMenuItemFields(): array | Closure + { + return $this->menuItemFields; + } } diff --git a/src/Livewire/MenuItems.php b/src/Livewire/MenuItems.php index 41fc7d2..4c40df5 100644 --- a/src/Livewire/MenuItems.php +++ b/src/Livewire/MenuItems.php @@ -5,11 +5,14 @@ namespace Datlechin\FilamentMenuBuilder\Livewire; use Datlechin\FilamentMenuBuilder\Enums\LinkTarget; +use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin; use Datlechin\FilamentMenuBuilder\Models\Menu; use Datlechin\FilamentMenuBuilder\Models\MenuItem; use Filament\Actions\Action; use Filament\Actions\Concerns\InteractsWithActions; use Filament\Actions\Contracts\HasActions; +use Filament\Forms\Components\Component as FormComponent; +use Filament\Forms\Components\Group; use Filament\Forms\Components\Placeholder; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; @@ -100,6 +103,9 @@ public function editAction(): Action ->label(__('filament-menu-builder::menu-builder.open_in.label')) ->options(LinkTarget::class) ->default(LinkTarget::Self), + Group::make() + ->visible(fn (FormComponent $component) => $component->evaluate(FilamentMenuBuilderPlugin::get()->getMenuItemFields()) !== []) + ->schema(FilamentMenuBuilderPlugin::get()->getMenuItemFields()), ]) ->action( fn (array $data, array $arguments) => MenuItem::query() diff --git a/src/Resources/MenuResource.php b/src/Resources/MenuResource.php index 50c96f6..e3d186d 100644 --- a/src/Resources/MenuResource.php +++ b/src/Resources/MenuResource.php @@ -7,6 +7,8 @@ use Datlechin\FilamentMenuBuilder\FilamentMenuBuilderPlugin; use Datlechin\FilamentMenuBuilder\Models\Menu; use Filament\Forms\Components\CheckboxList; +use Filament\Forms\Components\Component; +use Filament\Forms\Components\Group; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Toggle; use Filament\Forms\Form; @@ -41,6 +43,9 @@ public static function form(Form $form): Form Toggle::make('is_visible') ->label(__('filament-menu-builder::menu-builder.resource.is_visible.label')) ->default(true), + Group::make() + ->visible(fn (Component $component) => $component->evaluate(FilamentMenuBuilderPlugin::get()->getMenuFields()) !== []) + ->schema(FilamentMenuBuilderPlugin::get()->getMenuFields()), ]); }