diff --git a/docs/07-users/03-tenancy.md b/docs/07-users/03-tenancy.md index 27cac9fb33..ec7277b20e 100644 --- a/docs/07-users/03-tenancy.md +++ b/docs/07-users/03-tenancy.md @@ -367,6 +367,23 @@ public function panel(Panel $panel): Panel } ``` +### Allowing the tenants to be searched + +You can use the `searchableTenantMenu()` method in the [configuration](../panel-configuration) to allow the tenants to be searched: + +```php +use Filament\Panel; + +public function panel(Panel $panel): Panel +{ + return $panel + // ... + ->searchableTenantMenu(); +} +``` + +This is automatically enabled when there are more than 10 tenants in a user's list. You can disable it using `searchableTenantMenu(false)`. + ### Customizing the registration link To customize the [registration](#adding-a-tenant-registration-page) link in the tenant menu, register a new item with the `register` array key, and pass a function that [customizes the action](../actions) object: diff --git a/packages/panels/resources/lang/en/layout.php b/packages/panels/resources/lang/en/layout.php index 95189118bd..8e1bf7f055 100644 --- a/packages/panels/resources/lang/en/layout.php +++ b/packages/panels/resources/lang/en/layout.php @@ -60,4 +60,13 @@ return [ 'alt' => ':name logo', ], + 'tenant_menu' => [ + + 'search_field' => [ + 'label' => 'Tenant search', + 'placeholder' => 'Search', + ], + + ], + ]; diff --git a/packages/panels/resources/views/components/tenant-menu.blade.php b/packages/panels/resources/views/components/tenant-menu.blade.php index a805f69385..ab8a4da477 100644 --- a/packages/panels/resources/views/components/tenant-menu.blade.php +++ b/packages/panels/resources/views/components/tenant-menu.blade.php @@ -12,6 +12,8 @@ fn (\Illuminate\Database\Eloquent\Model $tenant): bool => ! $tenant->is($currentTenant), )); + $isSearchable = filled($canSwitchTenants) ? (filament()->isTenantMenuSearchable() ?? (count($tenants) >= 10)) : false; + $itemsBeforeAndAfterTenantSwitcher = collect($items) ->groupBy(fn (Action $item): bool => $canSwitchTenants && ($item->getSort() < 0), preserveKeys: true) ->all(); @@ -88,22 +90,49 @@ @endif @if ($canSwitchTenants) - - @foreach ($tenants as $tenant) - @php - $tenantUrl = filament()->getUrl($tenant); - $tenantImage = filament()->getTenantAvatarUrl($tenant); - @endphp +
+ + @if ($isSearchable) +
+ - - {{ filament()->getTenantName($tenant) }} - - @endforeach - + +
+ @endif + + @foreach ($tenants as $tenant) + @php + $tenantImage = filament()->getTenantAvatarUrl($tenant); + $tenantName = filament()->getTenantName($tenant); + $tenantUrl = filament()->getUrl($tenant); + @endphp + +
+ + {{ $tenantName }} + +
+ @endforeach +
+
@endif @if ($itemsAfterTenantSwitcher->isNotEmpty()) diff --git a/packages/panels/src/Facades/Filament.php b/packages/panels/src/Facades/Filament.php index 0131963a4d..7ce275d241 100644 --- a/packages/panels/src/Facades/Filament.php +++ b/packages/panels/src/Facades/Filament.php @@ -149,6 +149,7 @@ use Livewire\Component; * @method static bool isServing() * @method static bool isSidebarCollapsibleOnDesktop() * @method static bool isSidebarFullyCollapsibleOnDesktop() + * @method static ?bool isTenantMenuSearchable() * @method static void serving(Closure $callback) * @method static void setCurrentPanel(Panel | string | null $panel = null) * @method static void setServingStatus(bool $condition = true) diff --git a/packages/panels/src/FilamentManager.php b/packages/panels/src/FilamentManager.php index ae1e67ef44..0b444ebc01 100644 --- a/packages/panels/src/FilamentManager.php +++ b/packages/panels/src/FilamentManager.php @@ -462,6 +462,11 @@ class FilamentManager return $this->getCurrentOrDefaultPanel()->getTenantMenuItems(); } + public function isTenantMenuSearchable(): ?bool + { + return $this->getCurrentOrDefaultPanel()->isTenantMenuSearchable(); + } + /** * @return class-string|null */ diff --git a/packages/panels/src/Panel/Concerns/HasTenancy.php b/packages/panels/src/Panel/Concerns/HasTenancy.php index 4230108f1b..3f4707c506 100644 --- a/packages/panels/src/Panel/Concerns/HasTenancy.php +++ b/packages/panels/src/Panel/Concerns/HasTenancy.php @@ -23,6 +23,8 @@ trait HasTenancy protected bool | Closure $hasTenantMenu = true; + protected bool | Closure | null $isTenantMenuSearchable = null; + protected ?string $tenantRoutePrefix = null; protected ?string $tenantDomain = null; @@ -67,6 +69,13 @@ trait HasTenancy return $this; } + public function searchableTenantMenu(bool | Closure | null $condition = true): static + { + $this->isTenantMenuSearchable = $condition; + + return $this; + } + public function tenantMenu(bool | Closure $condition = true): static { $this->hasTenantMenu = $condition; @@ -325,6 +334,11 @@ trait HasTenancy ]) ?? $action; } + public function isTenantMenuSearchable(): ?bool + { + return $this->evaluate($this->isTenantMenuSearchable); + } + /** * @return array */