From 9b4379993df26ad018f14b6264c888d2c0ba3f4b Mon Sep 17 00:00:00 2001 From: Ivo Date: Mon, 13 Nov 2023 12:03:53 +0100 Subject: [PATCH 1/3] Added possibility for configuring panels not to show the translation navigation on. --- README.md | 7 +++++++ config/translation-manager.php | 12 ++++++++++++ src/Pages/QuickTranslate.php | 11 +++++++++-- src/Resources/LanguageLineResource.php | 15 +++++++++++++-- src/Traits/CanRegisterPanelNavigation.php | 23 +++++++++++++++++++++++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/Traits/CanRegisterPanelNavigation.php diff --git a/README.md b/README.md index aed6da6..a2cef6d 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,13 @@ Determines which locales your application supports. For example: Enable or disable the language switcher feature. This allows users to switch their language - disable if you have your own implementation. ![Language Switcher](https://raw.githubusercontent.com/kenepa/translation-manager/4.x/.github/language-switcher.png) +#### `dont_register_navigation_on_panel_ids` +Disable registering the translation manager navigation on certain panel id's. The following will disable the translation navigation for the guest panel , but still allow guest panel users to change the language: +```php + 'dont_register_navigation_on_panel_ids' => [ + 'guest' + ], +``` ## Usage diff --git a/config/translation-manager.php b/config/translation-manager.php index ff9ac75..8c87c9c 100644 --- a/config/translation-manager.php +++ b/config/translation-manager.php @@ -80,6 +80,18 @@ 'quick_translate_navigation_registration' => true, + /* + |-------------------------------------------------------------------------- + | Don't Register Navigation On Panels + |-------------------------------------------------------------------------- + | + | Array of panel id's which not to register navigation on. + | i.e. => ['guest', 'team1'] + | + */ + + 'dont_register_navigation_on_panel_ids' => [], + /* |-------------------------------------------------------------------------- | Flags or Initials diff --git a/src/Pages/QuickTranslate.php b/src/Pages/QuickTranslate.php index 3c7ff16..c00db43 100644 --- a/src/Pages/QuickTranslate.php +++ b/src/Pages/QuickTranslate.php @@ -7,12 +7,14 @@ use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; use Filament\Resources\Pages\Page; +use Illuminate\Support\Facades\Route; use Kenepa\TranslationManager\Resources\LanguageLineResource; +use Kenepa\TranslationManager\Traits\CanRegisterPanelNavigation; use Spatie\TranslationLoader\LanguageLine; class QuickTranslate extends Page implements HasForms { - use InteractsWithForms; + use InteractsWithForms, CanRegisterPanelNavigation; protected static string $view = 'translation-manager::quick-translate'; protected static string $resource = LanguageLineResource::class; @@ -27,7 +29,12 @@ class QuickTranslate extends Page implements HasForms */ public static function shouldRegisterNavigation(array $parameters = []): bool { - return config('translation-manager.quick_translate_navigation_registration'); + return static::shouldRegisterOnPanel() ? config('translation-manager.quick_translate_navigation_registration') : false; + } + + public function mount(): void + { + abort_unless(static::shouldRegisterOnPanel(), 403); } public static function getNavigationGroup(): ?string diff --git a/src/Resources/LanguageLineResource.php b/src/Resources/LanguageLineResource.php index c48ba01..60c624f 100644 --- a/src/Resources/LanguageLineResource.php +++ b/src/Resources/LanguageLineResource.php @@ -18,18 +18,29 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\Route; use Kenepa\TranslationManager\Filters\NotTranslatedFilter; use Kenepa\TranslationManager\Pages\QuickTranslate; use Kenepa\TranslationManager\Resources\LanguageLineResource\Pages\EditLanguageLine; use Kenepa\TranslationManager\Resources\LanguageLineResource\Pages\ListLanguageLines; +use Kenepa\TranslationManager\Traits\CanRegisterPanelNavigation; use Spatie\TranslationLoader\LanguageLine; class LanguageLineResource extends Resource { + use CanRegisterPanelNavigation; protected static ?string $model = LanguageLine::class; protected static ?string $navigationIcon = 'heroicon-o-globe-alt'; protected static ?string $slug = 'translation-manager'; + /** + * @param array $parameters + */ + public static function shouldRegisterNavigation(array $parameters = []): bool + { + return static::shouldRegisterOnPanel(); + } + public static function getLabel(): ?string { return trans_choice('translation-manager::translations.translation-label', 1); @@ -145,12 +156,12 @@ public static function getPages(): array public static function canViewAny(): bool { - return Gate::allows('use-translation-manager'); + return static::shouldRegisterOnPanel() ? Gate::allows('use-translation-manager') : false; } public static function canEdit(Model $record): bool { - return Gate::allows('use-translation-manager'); + return static::shouldRegisterOnPanel() ? Gate::allows('use-translation-manager') : false; } public static function getNavigationLabel(): string diff --git a/src/Traits/CanRegisterPanelNavigation.php b/src/Traits/CanRegisterPanelNavigation.php new file mode 100644 index 0000000..274ec21 --- /dev/null +++ b/src/Traits/CanRegisterPanelNavigation.php @@ -0,0 +1,23 @@ +getName(); + foreach (config('translation-manager.dont_register_navigation_on_panel_ids') as $panelName) { + if(str_starts_with($routeName, 'filament.' . $panelName)) { + return false; + } + } + return true; + } +} \ No newline at end of file From f8b79fb4626a094b1bedad2e39c98088118c8847 Mon Sep 17 00:00:00 2001 From: Ivo Date: Mon, 13 Nov 2023 12:04:55 +0100 Subject: [PATCH 2/3] Update CanRegisterPanelNavigation.php --- src/Traits/CanRegisterPanelNavigation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/CanRegisterPanelNavigation.php b/src/Traits/CanRegisterPanelNavigation.php index 274ec21..b2481cf 100644 --- a/src/Traits/CanRegisterPanelNavigation.php +++ b/src/Traits/CanRegisterPanelNavigation.php @@ -20,4 +20,4 @@ static function shouldRegisterOnPanel() : bool } return true; } -} \ No newline at end of file +} From e3d06d8f2d55fefb8a2bd79fc6be69d240b854a4 Mon Sep 17 00:00:00 2001 From: Jehizkia Date: Mon, 13 Nov 2023 13:00:28 +0100 Subject: [PATCH 3/3] Duster & use Filament facade for getting panel ids --- README.md | 4 ++-- src/Pages/QuickTranslate.php | 13 ++++++------- src/Resources/LanguageLineResource.php | 1 - src/Traits/CanRegisterPanelNavigation.php | 10 +++++----- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a2cef6d..4ffe9d7 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Enable or disable the language switcher feature. This allows users to switch the ![Language Switcher](https://raw.githubusercontent.com/kenepa/translation-manager/4.x/.github/language-switcher.png) #### `dont_register_navigation_on_panel_ids` -Disable registering the translation manager navigation on certain panel id's. The following will disable the translation navigation for the guest panel , but still allow guest panel users to change the language: +Disable registering the translation manager navigation on certain panel IDs. The following will disable the translation navigation for the guest panel but still allow guest panel users to change the language. ```php 'dont_register_navigation_on_panel_ids' => [ 'guest' @@ -130,4 +130,4 @@ Once installed, the Translation Manager can be accessed via the Filament sidebar ## License -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/src/Pages/QuickTranslate.php b/src/Pages/QuickTranslate.php index c00db43..1bc46bc 100644 --- a/src/Pages/QuickTranslate.php +++ b/src/Pages/QuickTranslate.php @@ -7,14 +7,13 @@ use Filament\Forms\Concerns\InteractsWithForms; use Filament\Forms\Contracts\HasForms; use Filament\Resources\Pages\Page; -use Illuminate\Support\Facades\Route; use Kenepa\TranslationManager\Resources\LanguageLineResource; use Kenepa\TranslationManager\Traits\CanRegisterPanelNavigation; use Spatie\TranslationLoader\LanguageLine; class QuickTranslate extends Page implements HasForms { - use InteractsWithForms, CanRegisterPanelNavigation; + use CanRegisterPanelNavigation, InteractsWithForms; protected static string $view = 'translation-manager::quick-translate'; protected static string $resource = LanguageLineResource::class; @@ -32,11 +31,6 @@ public static function shouldRegisterNavigation(array $parameters = []): bool return static::shouldRegisterOnPanel() ? config('translation-manager.quick_translate_navigation_registration') : false; } - public function mount(): void - { - abort_unless(static::shouldRegisterOnPanel(), 403); - } - public static function getNavigationGroup(): ?string { return config('translation-manager.navigation_group'); @@ -47,6 +41,11 @@ public static function getNavigationIcon(): ?string return 'heroicon-o-bolt'; } + public function mount(): void + { + abort_unless(static::shouldRegisterOnPanel(), 403); + } + /** * Returns an array containing two forms for quick translation of content. */ diff --git a/src/Resources/LanguageLineResource.php b/src/Resources/LanguageLineResource.php index 60c624f..b29b735 100644 --- a/src/Resources/LanguageLineResource.php +++ b/src/Resources/LanguageLineResource.php @@ -18,7 +18,6 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Gate; -use Illuminate\Support\Facades\Route; use Kenepa\TranslationManager\Filters\NotTranslatedFilter; use Kenepa\TranslationManager\Pages\QuickTranslate; use Kenepa\TranslationManager\Resources\LanguageLineResource\Pages\EditLanguageLine; diff --git a/src/Traits/CanRegisterPanelNavigation.php b/src/Traits/CanRegisterPanelNavigation.php index b2481cf..6d36d4f 100644 --- a/src/Traits/CanRegisterPanelNavigation.php +++ b/src/Traits/CanRegisterPanelNavigation.php @@ -2,22 +2,22 @@ namespace Kenepa\TranslationManager\Traits; -use Illuminate\Support\Facades\Route; +use Filament\Facades\Filament; trait CanRegisterPanelNavigation { - static function shouldRegisterOnPanel() : bool + public static function shouldRegisterOnPanel(): bool { - if(empty(config('translation-manager.dont_register_navigation_on_panel_ids'))) { + if (empty(config('translation-manager.dont_register_navigation_on_panel_ids'))) { return true; } - $routeName = Route::getCurrentRoute()?->getName(); foreach (config('translation-manager.dont_register_navigation_on_panel_ids') as $panelName) { - if(str_starts_with($routeName, 'filament.' . $panelName)) { + if (Filament::getPanel()->getId()) { return false; } } + return true; } }