From a17fcf1e5d06c4a04853edfb6d3fa7e3afcdaa90 Mon Sep 17 00:00:00 2001 From: Jhonatan Bianchi Date: Mon, 6 May 2024 04:08:42 -0300 Subject: [PATCH] Add config option to redirect to the resource index instead of the view (#179) * add config option to redirect to the resource index instead of the resource view * add config option to redirect to the resource index instead of the resource view --------- Co-authored-by: Bianchi --- config/filament-spatie-roles-permissions.php | 14 ++++++++++++++ .../PermissionResource/Pages/CreatePermission.php | 10 ++++++++++ .../PermissionResource/Pages/EditPermission.php | 9 +++++++++ src/Resources/RoleResource/Pages/CreateRole.php | 9 +++++++++ src/Resources/RoleResource/Pages/EditRole.php | 9 +++++++++ 5 files changed, 51 insertions(+) diff --git a/config/filament-spatie-roles-permissions.php b/config/filament-spatie-roles-permissions.php index dcab7a2..ae61c23 100644 --- a/config/filament-spatie-roles-permissions.php +++ b/config/filament-spatie-roles-permissions.php @@ -25,6 +25,20 @@ 'roles' => true, ], + /** + * Set to true to redirect to the resource index instead of the view + */ + 'should_redirect_to_index' => [ + 'permissions' => [ + 'after_create' => false, + 'after_edit' => false + ], + 'roles' => [ + 'after_create' => false, + 'after_edit' => false + ], + ], + /* * If you want to place the Resource in a Cluster, then set the required Cluster class. * Eg. \App\Filament\Clusters\Cluster::class diff --git a/src/Resources/PermissionResource/Pages/CreatePermission.php b/src/Resources/PermissionResource/Pages/CreatePermission.php index ba61d2f..fb1ab1c 100644 --- a/src/Resources/PermissionResource/Pages/CreatePermission.php +++ b/src/Resources/PermissionResource/Pages/CreatePermission.php @@ -3,9 +3,19 @@ namespace Althinect\FilamentSpatieRolesPermissions\Resources\PermissionResource\Pages; use Althinect\FilamentSpatieRolesPermissions\Resources\PermissionResource; +use Althinect\FilamentSpatieRolesPermissions\Resources\RoleResource; use Filament\Resources\Pages\CreateRecord; class CreatePermission extends CreateRecord { protected static string $resource = PermissionResource::class; + + protected function getRedirectUrl(): string + { + $resource = static::getResource(); + + return config('filament-spatie-roles-permissions.should_redirect_to_index.permissions.after_create', false) + ? $resource::getUrl('index') + : parent::getRedirectUrl(); + } } diff --git a/src/Resources/PermissionResource/Pages/EditPermission.php b/src/Resources/PermissionResource/Pages/EditPermission.php index 985b00a..e37de2c 100644 --- a/src/Resources/PermissionResource/Pages/EditPermission.php +++ b/src/Resources/PermissionResource/Pages/EditPermission.php @@ -8,4 +8,13 @@ class EditPermission extends EditRecord { protected static string $resource = PermissionResource::class; + + protected function getRedirectUrl(): ?string + { + $resource = static::getResource(); + + return config('filament-spatie-roles-permissions.should_redirect_to_index.permissions.after_edit', false) + ? $resource::getUrl('index') + : parent::getRedirectUrl(); + } } diff --git a/src/Resources/RoleResource/Pages/CreateRole.php b/src/Resources/RoleResource/Pages/CreateRole.php index ad0eb43..b6c0f15 100644 --- a/src/Resources/RoleResource/Pages/CreateRole.php +++ b/src/Resources/RoleResource/Pages/CreateRole.php @@ -8,4 +8,13 @@ class CreateRole extends CreateRecord { protected static string $resource = RoleResource::class; + + protected function getRedirectUrl(): string + { + $resource = static::getResource(); + + return config('filament-spatie-roles-permissions.should_redirect_to_index.roles.after_create', false) + ? $resource::getUrl('index') + : parent::getRedirectUrl(); + } } diff --git a/src/Resources/RoleResource/Pages/EditRole.php b/src/Resources/RoleResource/Pages/EditRole.php index 5719096..7d017ee 100644 --- a/src/Resources/RoleResource/Pages/EditRole.php +++ b/src/Resources/RoleResource/Pages/EditRole.php @@ -18,4 +18,13 @@ public function getHeaderActions(): array DeleteAction::make(), ]; } + + protected function getRedirectUrl(): ?string + { + $resource = static::getResource(); + + return config('filament-spatie-roles-permissions.should_redirect_to_index.roles.after_create', false) + ? $resource::getUrl('index') + : parent::getRedirectUrl(); + } }