Skip to content

Commit

Permalink
Merge pull request #62 from iRaziul/main
Browse files Browse the repository at this point in the history
Add methods to configure navigation label, group, icon, sort and count
  • Loading branch information
datlechin authored Dec 26, 2024
2 parents 9cbaa78 + 1b3ba09 commit a4d09f0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 14 deletions.
90 changes: 80 additions & 10 deletions src/FilamentMenuBuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
use Datlechin\FilamentMenuBuilder\Resources\MenuResource;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;

class FilamentMenuBuilderPlugin implements Plugin
{
use EvaluatesClosures;

protected string $resource = MenuResource::class;

protected string $menuModel = Menu::class;
Expand All @@ -25,9 +28,19 @@ class FilamentMenuBuilderPlugin implements Plugin

protected array $locations = [];

protected array | Closure $menuFields = [];
protected array|Closure $menuFields = [];

protected array|Closure $menuItemFields = [];

protected string|Closure|null $navigationLabel = null;

protected string|Closure|null $navigationGroup = null;

protected string|Closure|null $navigationIcon = 'heroicon-o-bars-3';

protected int|Closure|null $navigationSort = null;

protected array | Closure $menuItemFields = [];
protected bool $navigationCountBadge = false;

/**
* @var MenuPanel[]
Expand Down Expand Up @@ -60,10 +73,7 @@ public static function make(): static

public static function get(): static
{
/** @var static $plugin */
$plugin = filament(app(static::class)->getId());

return $plugin;
return filament(app(static::class)->getId());
}

public function usingResource(string $resource): static
Expand Down Expand Up @@ -143,20 +153,55 @@ public function showCustomTextPanel(bool $show = true): static
return $this;
}

public function addMenuFields(array | Closure $schema): static
public function addMenuFields(array|Closure $schema): static
{
$this->menuFields = $schema;

return $this;
}

public function addMenuItemFields(array | Closure $schema): static
public function addMenuItemFields(array|Closure $schema): static
{
$this->menuItemFields = $schema;

return $this;
}

public function navigationLabel(string|Closure $label = null): static
{
$this->navigationLabel = $label;

return $this;
}

public function navigationGroup(string|Closure $group = null): static
{
$this->navigationGroup = $group;

return $this;
}

public function navigationIcon(string|Closure $icon): static
{
$this->navigationIcon = $icon;

return $this;
}

public function navigationSort(int|Closure $order): static
{
$this->navigationSort = $order;

return $this;
}

public function navigationCountBadge(bool $show = true): static
{
$this->navigationCountBadge = $show;

return $this;
}

public function getResource(): string
{
return $this->resource;
Expand Down Expand Up @@ -202,13 +247,38 @@ public function getLocations(): array
return $this->locations;
}

public function getMenuFields(): array | Closure
public function getMenuFields(): array|Closure
{
return $this->menuFields;
}

public function getMenuItemFields(): array | Closure
public function getMenuItemFields(): array|Closure
{
return $this->menuItemFields;
}

public function getNavigationGroup(): ?string
{
return $this->evaluate($this->navigationGroup);
}

public function getNavigationLabel(): ?string
{
return $this->evaluate($this->navigationLabel);
}

public function getNavigationIcon(): ?string
{
return $this->evaluate($this->navigationIcon);
}

public function getNavigationSort(): ?int
{
return $this->evaluate($this->navigationSort);
}

public function getNavigationCountBadge(): bool
{
return $this->navigationCountBadge;
}
}
32 changes: 28 additions & 4 deletions src/Resources/MenuResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,40 @@
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Str;

class MenuResource extends Resource
{
protected static ?string $navigationIcon = 'heroicon-o-bars-3';

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

public static function getNavigationLabel(): string
{
return FilamentMenuBuilderPlugin::get()->getNavigationLabel() ?? Str::title(static::getPluralModelLabel()) ?? Str::title(static::getModelLabel());
}

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

public static function getNavigationSort(): ?int
{
return FilamentMenuBuilderPlugin::get()->getNavigationSort();
}

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

public static function getNavigationBadge(): ?string
{
return FilamentMenuBuilderPlugin::get()->getNavigationCountBadge() ? number_format(static::getModel()::count()) : null;
}

public static function form(Form $form): Form
{
return $form
Expand Down Expand Up @@ -97,8 +121,8 @@ public static function table(Table $table): Table
public static function getPages(): array
{
return [
'index' => \Datlechin\FilamentMenuBuilder\Resources\MenuResource\Pages\ListMenus::route('/'),
'edit' => \Datlechin\FilamentMenuBuilder\Resources\MenuResource\Pages\EditMenu::route('/{record}/edit'),
'index' => MenuResource\Pages\ListMenus::route('/'),
'edit' => MenuResource\Pages\EditMenu::route('/{record}/edit'),
];
}
}

0 comments on commit a4d09f0

Please sign in to comment.