Skip to content

Commit 355f0dd

Browse files
committed
improve extendability of panel + fix bugs in supported locales
1 parent d5c5efa commit 355f0dd

7 files changed

+74
-37
lines changed

config/filament-flexible-content-block-pages.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
'page_resource' => [
5050
\Statikbe\FilamentFlexibleContentBlockPages\Models\Page::class => [
5151
'enable_hero_call_to_actions' => true,
52-
'enable_author' => true,
52+
'enable_author' => true,
5353
'enable_parent' => true,
5454
'enable_undeletable' => true,
5555
],
@@ -72,18 +72,6 @@
7272
'auth_middleware' => [
7373
Authenticate::class,
7474
],
75-
'navigation_items' => [
76-
// WARNING: Do not place redirect NavigationItems first in the array.
77-
// Filament automatically redirects to the first navigation item on panel load.
78-
// If that item is an external redirect (like the home route), users will be
79-
// bounced out of the panel, creating the appearance of authentication failure.
80-
//
81-
// NavigationItem::make(fn () => flexiblePagesTrans('panel.navigation_items.go_to_website_lbl'))
82-
// ->url('/')
83-
// ->openUrlInNewTab()
84-
// ->icon('heroicon-o-globe-alt')
85-
// ->sort(-100),
86-
],
8775
],
8876

8977
'route_helper' => \Statikbe\FilamentFlexibleContentBlockPages\Routes\LocalisedPageRouteHelper::class,

src/FilamentFlexibleContentBlockPagesConfig.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Filament\Resources\Resource;
66
use Illuminate\Database\Eloquent\Model;
7+
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
78
use Statikbe\FilamentFlexibleContentBlockPages\Models\Menu;
89
use Statikbe\FilamentFlexibleContentBlockPages\Models\MenuItem;
910
use Statikbe\FilamentFlexibleContentBlockPages\Models\Page;
@@ -14,7 +15,7 @@
1415
use Statikbe\FilamentFlexibleContentBlockPages\Routes\Contracts\HandlesPageRoutes;
1516
use Statikbe\FilamentFlexibleContentBlockPages\Routes\LocalisedPageRouteHelper;
1617
use Statikbe\FilamentFlexibleContentBlockPages\Services\Enum\SitemapGeneratorMethod;
17-
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleContentBlocksServiceProvider;
18+
use Statikbe\FilamentFlexibleContentBlocks\Facades\FilamentFlexibleContentBlocks;
1819

1920
class FilamentFlexibleContentBlockPagesConfig
2021
{
@@ -66,10 +67,12 @@ public function __construct()
6667

6768
public function getSupportedLocales(): array
6869
{
69-
return config(
70-
FilamentFlexibleContentBlocksServiceProvider::$name.'.supported_locales',
71-
config('app.supported_locales', ['en'])
72-
);
70+
$flexibleBlocksLocales = FilamentFlexibleContentBlocks::getLocales();
71+
if (! empty($flexibleBlocksLocales)) {
72+
return $flexibleBlocksLocales;
73+
}
74+
75+
return LaravelLocalization::getSupportedLanguagesKeys() ?? ['en'];
7376
}
7477

7578
public function getPageModel(): Page
@@ -233,11 +236,6 @@ public function getRouteHelper(): HandlesPageRoutes
233236
return $this->routeHelper;
234237
}
235238

236-
public function getPanelNavigationItems(): array
237-
{
238-
return $this->packageConfig('panel.navigation_items', []);
239-
}
240-
241239
public function getCustomPageTemplates(): array
242240
{
243241
return $this->packageConfig('page_templates', []);
@@ -302,35 +300,31 @@ public function getSitemapCustomUrls(): array
302300
}
303301

304302
/**
305-
* @param class-string<Model> $modelClass
306-
* @return bool
303+
* @param class-string<Model> $modelClass
307304
*/
308305
public function isHeroCallToActionsEnabled(string $modelClass): bool
309306
{
310307
return $this->packageConfig("page_resource.{$modelClass}.enable_hero_call_to_actions", true);
311308
}
312309

313310
/**
314-
* @param class-string<Model> $modelClass
315-
* @return bool
311+
* @param class-string<Model> $modelClass
316312
*/
317313
public function isAuthorEnabled(string $modelClass): bool
318314
{
319315
return $this->packageConfig("page_resource.{$modelClass}.enable_author", true);
320316
}
321317

322318
/**
323-
* @param class-string<Model> $modelClass
324-
* @return bool
319+
* @param class-string<Model> $modelClass
325320
*/
326321
public function isParentEnabled(string $modelClass): bool
327322
{
328323
return $this->packageConfig("page_resource.{$modelClass}.enable_parent", true);
329324
}
330325

331326
/**
332-
* @param class-string<Model> $modelClass
333-
* @return bool
327+
* @param class-string<Model> $modelClass
334328
*/
335329
public function isUndeletableEnabled(string $modelClass): bool
336330
{

src/FilamentFlexibleContentBlockPagesServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Statikbe\FilamentFlexibleContentBlockPages;
44

55
use Illuminate\Database\Eloquent\Relations\Relation;
6+
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
67
use Spatie\LaravelPackageTools\Package;
78
use Spatie\LaravelPackageTools\PackageServiceProvider;
89
use Statikbe\FilamentFlexibleContentBlockPages\Commands\GenerateSitemapCommand;
@@ -11,6 +12,7 @@
1112
use Statikbe\FilamentFlexibleContentBlockPages\Components\LanguageSwitch;
1213
use Statikbe\FilamentFlexibleContentBlockPages\Components\Menu;
1314
use Statikbe\FilamentFlexibleContentBlockPages\Components\MenuItem;
15+
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleContentBlocks;
1416

1517
class FilamentFlexibleContentBlockPagesServiceProvider extends PackageServiceProvider
1618
{
@@ -50,6 +52,10 @@ public function packageBooted()
5052
{
5153
// add morph map
5254
Relation::morphMap(\Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages::config()->getMorphMap());
55+
56+
$this->mergeConfigFrom(__DIR__.'/../config/'.$this->package->name.'.php', $this->package->name);
57+
58+
FilamentFlexibleContentBlocks::setLocales(LaravelLocalization::getSupportedLanguagesKeys());
5359
}
5460

5561
public function packageRegistered()

src/FlexibleContentBlockPagesPanel.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,35 @@ class FlexibleContentBlockPagesPanel extends PanelProvider
1212
const ID = 'filament-flexible-content-block-pages';
1313

1414
public function panel(Panel $panel): Panel
15+
{
16+
return static::configurePanel($panel, static::ID);
17+
}
18+
19+
/**
20+
* This function can be used to create a custom panel. Just create a new PanelProvider sub class and implement the
21+
* `panel` function. You can then call `configurePanel` statically.
22+
*
23+
* Another option if you are not already extending from another class, is to extend this class and overwrite the functions.
24+
*/
25+
public static function configurePanel(Panel $panel, string $id): Panel
1526
{
1627
return $panel
17-
->id(static::ID)
28+
->id($id)
1829
->path(FilamentFlexibleContentBlockPages::config()->getPanelPath())
1930
->middleware(FilamentFlexibleContentBlockPages::config()->getPanelMiddleware())
2031
->authMiddleware(FilamentFlexibleContentBlockPages::config()->getPanelAuthMiddleware())
2132
->resources(FilamentFlexibleContentBlockPages::config()->getResources())
2233
->plugin(FlexibleContentBlockPagesPlugin::make())
2334
->plugin(SpatieLaravelTranslatablePlugin::make()
2435
->defaultLocales(FilamentFlexibleContentBlockPages::config()->getSupportedLocales()))
25-
->navigationItems(FilamentFlexibleContentBlockPages::config()->getPanelNavigationItems());
36+
->navigationItems(static::getExtraNavigationItems());
37+
}
38+
39+
/**
40+
* Implement this function in a sub class, to add nav items.
41+
*/
42+
public static function getExtraNavigationItems(): array
43+
{
44+
return [];
2645
}
2746
}

src/FlexibleContentBlockPagesPlugin.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,34 @@ public static function getWidgets(): array
8383
{
8484
return static::$widgets;
8585
}
86+
87+
/**
88+
* @param array<class-string<\Filament\Pages\Page>>
89+
*/
90+
public static function pages(array $pages): static
91+
{
92+
static::$pages = $pages;
93+
94+
return new static;
95+
}
96+
97+
/**
98+
* @param array<class-string<\Filament\Widgets\Widget>>
99+
*/
100+
public static function widgets(array $widgets): static
101+
{
102+
static::$widgets = $widgets;
103+
104+
return new static;
105+
}
106+
107+
/**
108+
* @param array<class-string<resource>> $resources
109+
*/
110+
public static function resources(array $resources): static
111+
{
112+
static::$resources = $resources;
113+
114+
return new static;
115+
}
86116
}

src/Models/Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function isHomePage(): bool
8181

8282
public function isDeletable(): bool
8383
{
84-
if(!$this->hasAttribute('is_undeletable')){
84+
if (! $this->hasAttribute('is_undeletable')) {
8585
return true;
8686
}
8787

src/Resources/PageResource.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ protected static function getGeneralTabFields(): array
111111
HeroImageSection::create(true),
112112
];
113113

114-
if(FilamentFlexibleContentBlockPages::config()->isHeroCallToActionsEnabled(static::getModel())){
115-
$fields[] = new HeroCallToActionSection();
114+
if (FilamentFlexibleContentBlockPages::config()->isHeroCallToActionsEnabled(static::getModel())) {
115+
$fields[] = new HeroCallToActionSection;
116116
}
117117

118118
return $fields;
@@ -162,7 +162,7 @@ protected static function getAdvancedTabFields(): array
162162
->searchable(['title', 'code', 'slug', 'intro']);
163163
}
164164

165-
if (!empty($gridFields)) {
165+
if (! empty($gridFields)) {
166166
$fields[] = Grid::make()->schema($gridFields);
167167
}
168168

0 commit comments

Comments
 (0)