Skip to content

Commit

Permalink
Merge pull request #26 from Danoctum/25-abilitiy-to-hide-and-show
Browse files Browse the repository at this point in the history
Add abilitiy to hide and show
  • Loading branch information
Jehizkia authored Nov 13, 2023
2 parents 093b229 + e3d06d8 commit 3314835
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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'
],
```

## Usage

Expand All @@ -123,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.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
12 changes: 12 additions & 0 deletions config/translation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/Pages/QuickTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use Filament\Forms\Contracts\HasForms;
use Filament\Resources\Pages\Page;
use Kenepa\TranslationManager\Resources\LanguageLineResource;
use Kenepa\TranslationManager\Traits\CanRegisterPanelNavigation;
use Spatie\TranslationLoader\LanguageLine;

class QuickTranslate extends Page implements HasForms
{
use InteractsWithForms;
use CanRegisterPanelNavigation, InteractsWithForms;

protected static string $view = 'translation-manager::quick-translate';
protected static string $resource = LanguageLineResource::class;
Expand All @@ -27,7 +28,7 @@ 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 static function getNavigationGroup(): ?string
Expand All @@ -40,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.
*/
Expand Down
14 changes: 12 additions & 2 deletions src/Resources/LanguageLineResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@
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<string, mixed> $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);
Expand Down Expand Up @@ -145,12 +155,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
Expand Down
23 changes: 23 additions & 0 deletions src/Traits/CanRegisterPanelNavigation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Kenepa\TranslationManager\Traits;

use Filament\Facades\Filament;

trait CanRegisterPanelNavigation
{
public static function shouldRegisterOnPanel(): bool
{
if (empty(config('translation-manager.dont_register_navigation_on_panel_ids'))) {
return true;
}

foreach (config('translation-manager.dont_register_navigation_on_panel_ids') as $panelName) {
if (Filament::getPanel()->getId()) {
return false;
}
}

return true;
}
}

0 comments on commit 3314835

Please sign in to comment.