Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 Add support for custom menu and menu item fields #6

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions src/FilamentMenuBuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class FilamentMenuBuilderPlugin implements Plugin
{
protected array $locations = [];

protected array | Closure $menuFields = [];

protected array | Closure $menuItemFields = [];

/**
* @var MenuPanel[]
*/
Expand Down Expand Up @@ -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[]
*/
Expand All @@ -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;
}
}
6 changes: 6 additions & 0 deletions src/Livewire/MenuItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/MenuResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
]);
}

Expand Down