From 63a06c7d1d0948cfb975d66f9af4debc9701adcc Mon Sep 17 00:00:00 2001 From: Sumardi Shukor Date: Sat, 5 Aug 2023 14:58:50 +0800 Subject: [PATCH 1/5] Add ability to hide menu items. --- packages/panels/src/Navigation/MenuItem.php | 32 +++++++++++++++++++ .../panels/src/Panel/Concerns/HasTenancy.php | 1 + .../panels/src/Panel/Concerns/HasUserMenu.php | 1 + 3 files changed, 34 insertions(+) diff --git a/packages/panels/src/Navigation/MenuItem.php b/packages/panels/src/Navigation/MenuItem.php index a637c87a55..b47e1fa9ba 100644 --- a/packages/panels/src/Navigation/MenuItem.php +++ b/packages/panels/src/Navigation/MenuItem.php @@ -21,6 +21,10 @@ class MenuItem extends Component protected string | Closure | Native | null $url = null; + protected bool | Closure $isHidden = false; + + protected bool | Closure $isVisible = true; + final public function __construct() { } @@ -68,6 +72,34 @@ class MenuItem extends Component return $this; } + public function hidden(bool | Closure $condition = true): static + { + $this->isHidden = $condition; + + return $this; + } + + public function visible(bool | Closure $condition = true): static + { + $this->isVisible = $condition; + + return $this; + } + + public function isVisible(): bool + { + return ! $this->isHidden(); + } + + public function isHidden(): bool + { + if ($this->evaluate($this->isHidden)) { + return true; + } + + return ! $this->evaluate($this->isVisible); + } + /** * @return string | array{50: string, 100: string, 200: string, 300: string, 400: string, 500: string, 600: string, 700: string, 800: string, 900: string, 950: string} | null */ diff --git a/packages/panels/src/Panel/Concerns/HasTenancy.php b/packages/panels/src/Panel/Concerns/HasTenancy.php index d6879f66ad..1b34a20b3d 100644 --- a/packages/panels/src/Panel/Concerns/HasTenancy.php +++ b/packages/panels/src/Panel/Concerns/HasTenancy.php @@ -190,6 +190,7 @@ trait HasTenancy public function getTenantMenuItems(): array { return collect($this->tenantMenuItems) + ->reject(fn (MenuItem $item): bool => $item->isHidden()) ->sort(fn (MenuItem $item): int => $item->getSort()) ->all(); } diff --git a/packages/panels/src/Panel/Concerns/HasUserMenu.php b/packages/panels/src/Panel/Concerns/HasUserMenu.php index 96c31caeaa..c43963e3a2 100644 --- a/packages/panels/src/Panel/Concerns/HasUserMenu.php +++ b/packages/panels/src/Panel/Concerns/HasUserMenu.php @@ -30,6 +30,7 @@ trait HasUserMenu public function getUserMenuItems(): array { return collect($this->userMenuItems) + ->reject(fn (MenuItem $item): bool => $item->isHidden()) ->sort(fn (MenuItem $item): int => $item->getSort()) ->all(); } From 7084dcf72376571b5a82953faee14330fb1a54ef Mon Sep 17 00:00:00 2001 From: Sumardi Shukor Date: Sat, 5 Aug 2023 15:15:19 +0800 Subject: [PATCH 2/5] Add docs. --- packages/panels/docs/06-navigation.md | 15 +++++++++++++++ packages/panels/docs/10-tenancy.md | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/panels/docs/06-navigation.md b/packages/panels/docs/06-navigation.md index e400e92b06..c47cc42b3a 100644 --- a/packages/panels/docs/06-navigation.md +++ b/packages/panels/docs/06-navigation.md @@ -374,6 +374,21 @@ public function panel(Panel $panel): Panel } ``` +## Conditionally hiding user menus + +You can also conditionally hide a user item by using the visible() or hidden() methods, passing in a condition to check: + +```php +use Filament\Navigation\MenuItem; +use App\Models\Payment; + +MenuItem::make() + ->label('Payments') + ->visible(auth()->user()->can('viewAny', Payment::class)) + // or + ->hidden(! auth()->user()->can('viewAny', Payment::class)) +``` + ## Disabling breadcrumbs The default layout will show breadcrumbs to indicate the location of the current page within the hierarchy of the app. diff --git a/packages/panels/docs/10-tenancy.md b/packages/panels/docs/10-tenancy.md index d505aa65fe..6fcc23df82 100644 --- a/packages/panels/docs/10-tenancy.md +++ b/packages/panels/docs/10-tenancy.md @@ -296,6 +296,20 @@ public function panel(Panel $panel): Panel } ``` +## Conditionally hiding tenant menus + +You can also conditionally hide a tenant item by using the visible() or hidden() methods, passing in a condition to check: + +```php +use Filament\Navigation\MenuItem; + +MenuItem::make() + ->label('Settings') + ->visible(auth()->user()->can('manage-team')) + // or + ->hidden(! auth()->user()->can('manage-team')) +``` + ### Customizing the registration link To customize the registration link on the tenant menu, register a new item with the `register` array key: From c3ebfc12fe9587a9e0792337eb6f175b046d82b1 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 6 Aug 2023 22:56:44 +0100 Subject: [PATCH 3/5] Update 06-navigation.md --- packages/panels/docs/06-navigation.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/panels/docs/06-navigation.md b/packages/panels/docs/06-navigation.md index c47cc42b3a..6cee952b39 100644 --- a/packages/panels/docs/06-navigation.md +++ b/packages/panels/docs/06-navigation.md @@ -374,9 +374,9 @@ public function panel(Panel $panel): Panel } ``` -## Conditionally hiding user menus +### Conditionally hiding user menu items -You can also conditionally hide a user item by using the visible() or hidden() methods, passing in a condition to check: +You can also conditionally hide a user menu item by using the `visible()` or `hidden()` methods, passing in a condition to check. Passing a function will defer condition evaluation until the menu is actually being rendered: ```php use Filament\Navigation\MenuItem; @@ -384,9 +384,9 @@ use App\Models\Payment; MenuItem::make() ->label('Payments') - ->visible(auth()->user()->can('viewAny', Payment::class)) + ->visible(fn (): bool => auth()->user()->can('viewAny', Payment::class)) // or - ->hidden(! auth()->user()->can('viewAny', Payment::class)) + ->hidden(fn (): bool => ! auth()->user()->can('viewAny', Payment::class)) ``` ## Disabling breadcrumbs From 512e3e1534601c9e05df796ae0926390fbef6ca2 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 6 Aug 2023 22:57:02 +0100 Subject: [PATCH 4/5] Update 06-navigation.md --- packages/panels/docs/06-navigation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/panels/docs/06-navigation.md b/packages/panels/docs/06-navigation.md index 6cee952b39..ee84f29a2f 100644 --- a/packages/panels/docs/06-navigation.md +++ b/packages/panels/docs/06-navigation.md @@ -379,8 +379,8 @@ public function panel(Panel $panel): Panel You can also conditionally hide a user menu item by using the `visible()` or `hidden()` methods, passing in a condition to check. Passing a function will defer condition evaluation until the menu is actually being rendered: ```php -use Filament\Navigation\MenuItem; use App\Models\Payment; +use Filament\Navigation\MenuItem; MenuItem::make() ->label('Payments') From bba71e67bd786ff0682348e765370d0903d9a839 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 6 Aug 2023 22:58:43 +0100 Subject: [PATCH 5/5] Update 10-tenancy.md --- packages/panels/docs/10-tenancy.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/panels/docs/10-tenancy.md b/packages/panels/docs/10-tenancy.md index 6fcc23df82..394ca36efb 100644 --- a/packages/panels/docs/10-tenancy.md +++ b/packages/panels/docs/10-tenancy.md @@ -296,20 +296,6 @@ public function panel(Panel $panel): Panel } ``` -## Conditionally hiding tenant menus - -You can also conditionally hide a tenant item by using the visible() or hidden() methods, passing in a condition to check: - -```php -use Filament\Navigation\MenuItem; - -MenuItem::make() - ->label('Settings') - ->visible(auth()->user()->can('manage-team')) - // or - ->hidden(! auth()->user()->can('manage-team')) -``` - ### Customizing the registration link To customize the registration link on the tenant menu, register a new item with the `register` array key: @@ -348,6 +334,20 @@ public function panel(Panel $panel): Panel } ``` +### Conditionally hiding tenant menu items + +You can also conditionally hide a tenant menu item by using the `visible()` or `hidden()` methods, passing in a condition to check. Passing a function will defer condition evaluation until the menu is actually being rendered: + +```php +use Filament\Navigation\MenuItem; + +MenuItem::make() + ->label('Settings') + ->visible(fn (): bool => auth()->user()->can('manage-team')) + // or + ->hidden(fn (): bool => ! auth()->user()->can('manage-team')) +``` + ## Setting up avatars Out of the box, Filament uses [ui-avatars.com](https://ui-avatars.com) to generate avatars based on a user's name. However, if you user model has an `avatar_url` attribute, that will be used instead. To customize how Filament gets a user's avatar URL, you can implement the `HasAvatar` contract: