Skip to content

Commit 96ab032

Browse files
ZANX3Ydanharrin
andauthored
feat: allow tenant switching to be disabled (#19110)
* feat: allow tenant switching to be disabled * cleanup * Update 03-tenancy.md --------- Co-authored-by: Dan Harrin <git@danharrin.com>
1 parent 608bf9b commit 96ab032

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

docs/07-users/03-tenancy.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,23 @@ Action::make('lockSession')
466466
->postToUrl()
467467
```
468468

469+
### Disabling the tenant switcher
470+
471+
By default, users can switch between tenants using the tenant menu. If you want to keep the tenant menu visible but prevent users from switching tenants, you can use the `tenantSwitcher()` method in the [configuration](../panel-configuration):
472+
473+
```php
474+
use Filament\Panel;
475+
476+
public function panel(Panel $panel): Panel
477+
{
478+
return $panel
479+
// ...
480+
->tenantSwitcher(false);
481+
}
482+
```
483+
484+
This keeps the tenant menu visible, showing the current tenant name and any custom menu items, but hides the list of other tenants. This is useful when you want to display tenant information without allowing switching, or when switching should be controlled through other means.
485+
469486
### Hiding the tenant menu
470487

471488
You can hide the tenant menu by using the `tenantMenu(false)`

packages/panels/resources/views/components/tenant-menu.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
1212
$items = $this->getTenantMenuItems();
1313
14-
$canSwitchTenants = count($tenants = array_filter(
14+
$canSwitchTenants = filament()->hasTenantSwitcher() && filled($tenants = array_filter(
1515
filament()->getUserTenants(filament()->auth()->user()),
1616
fn (\Illuminate\Database\Eloquent\Model $tenant): bool => ! $tenant->is($currentTenant),
1717
));
1818
19-
$isSearchable = filled($canSwitchTenants) ? (filament()->isTenantMenuSearchable() ?? (count($tenants) >= 10)) : false;
19+
$isSearchable = $canSwitchTenants && (filament()->isTenantMenuSearchable() ?? (count($tenants) >= 10));
2020
2121
$itemsBeforeAndAfterTenantSwitcher = collect($items)
2222
->groupBy(fn (Action $item): bool => $canSwitchTenants && ($item->getSort() < 0), preserveKeys: true)

packages/panels/src/Facades/Filament.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
* @method static bool isServing()
160160
* @method static bool isSidebarCollapsibleOnDesktop()
161161
* @method static bool isSidebarFullyCollapsibleOnDesktop()
162+
* @method static bool hasTenantSwitcher()
162163
* @method static ?bool isTenantMenuSearchable()
163164
* @method static void serving(Closure $callback)
164165
* @method static void setCurrentPanel(Panel | string | null $panel = null)

packages/panels/src/FilamentManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ public function getTenantMenuItems(): array
480480
return $this->getCurrentOrDefaultPanel()->getTenantMenuItems();
481481
}
482482

483+
public function hasTenantSwitcher(): bool
484+
{
485+
return $this->getCurrentOrDefaultPanel()->hasTenantSwitcher();
486+
}
487+
483488
public function isTenantMenuSearchable(): ?bool
484489
{
485490
return $this->getCurrentOrDefaultPanel()->isTenantMenuSearchable();

packages/panels/src/Panel/Concerns/HasTenancy.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ trait HasTenancy
2323

2424
protected bool | Closure $hasTenantMenu = true;
2525

26+
protected bool | Closure $hasTenantSwitcher = true;
27+
2628
protected bool | Closure | null $isTenantMenuSearchable = null;
2729

2830
protected ?string $tenantRoutePrefix = null;
@@ -71,6 +73,13 @@ public function tenantMenuItems(array $items): static
7173
return $this;
7274
}
7375

76+
public function tenantSwitcher(bool | Closure $condition = true): static
77+
{
78+
$this->hasTenantSwitcher = $condition;
79+
80+
return $this;
81+
}
82+
7483
public function searchableTenantMenu(bool | Closure | null $condition = true): static
7584
{
7685
$this->isTenantMenuSearchable = $condition;
@@ -349,6 +358,11 @@ protected function getTenantRegistrationMenuItem(Action | Closure | MenuItem | n
349358
]) ?? $action;
350359
}
351360

361+
public function hasTenantSwitcher(): bool
362+
{
363+
return (bool) $this->evaluate($this->hasTenantSwitcher);
364+
}
365+
352366
public function isTenantMenuSearchable(): ?bool
353367
{
354368
return $this->evaluate($this->isTenantMenuSearchable);

0 commit comments

Comments
 (0)