Skip to content

Commit 745ec53

Browse files
committed
Make Page form more UX friendly by moving rarely used fields to advanced tab
Fix phpstan errors
1 parent 969399c commit 745ec53

21 files changed

+130
-65
lines changed

resources/lang/en/filament-flexible-content-block-pages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'content' => 'Content',
1212
'seo' => 'Search engine optimization',
1313
'overview' => 'Overview',
14+
'advanced' => 'Advanced',
1415
],
1516
'table' => [
1617
'created_at_col' => 'Created at',

resources/lang/nl/filament-flexible-content-block-pages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'content' => 'Inhoud',
1212
'seo' => 'Search engine optimalisatie',
1313
'overview' => 'Overzicht',
14+
'advanced' => 'Geavanceerd',
1415
],
1516
'table' => [
1617
'created_at_col' => 'Aangemaakt op',

src/Commands/SeedDefaultsCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,18 @@ public function seedSettings(): void
6565
}
6666
}
6767

68+
/**
69+
* Set a translated field value for all given locales.
70+
*
71+
* @param Model $model Model that uses HasTranslations trait
72+
* @param string $field
73+
* @param string $value
74+
* @param array<string> $locales
75+
*/
6876
private function setTranslatedField(Model $model, string $field, string $value, array $locales)
6977
{
7078
foreach ($locales as $locale) {
79+
/** @phpstan-ignore-next-line */
7180
$model->setTranslation($field, $locale, $value);
7281
}
7382
}

src/FilamentFlexibleContentBlockPagesServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Statikbe\FilamentFlexibleContentBlockPages\Components\LanguageSwitch;
1212
use Statikbe\FilamentFlexibleContentBlockPages\Components\Menu;
1313
use Statikbe\FilamentFlexibleContentBlockPages\Components\MenuItem;
14-
use Statikbe\FilamentFlexibleContentBlockPages\Livewire\MenuTreeItem;
1514

1615
class FilamentFlexibleContentBlockPagesServiceProvider extends PackageServiceProvider
1716
{
@@ -48,8 +47,5 @@ public function packageBooted()
4847
{
4948
// add morph map
5049
Relation::morphMap(\Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages::config()->getMorphMap());
51-
52-
// Register Livewire components
53-
Livewire::component('filament-flexible-content-block-pages::menu-tree-item', MenuTreeItem::class);
5450
}
5551
}

src/Form/Fields/Types/LinkableMenuItemType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Statikbe\FilamentFlexibleContentBlockPages\Form\Fields\Types;
44

55
use Closure;
6+
use phpDocumentor\Reflection\Types\ClassString;
67
use Statikbe\FilamentFlexibleContentBlockPages\Models\Contracts\HasMenuLabel;
8+
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\Linkable;
79

810
class LinkableMenuItemType extends AbstractMenuItemType
911
{
@@ -13,7 +15,7 @@ class LinkableMenuItemType extends AbstractMenuItemType
1315

1416
protected int $searchResultLimit = 50;
1517

16-
public function __construct(string $model)
18+
final public function __construct(string $model)
1719
{
1820
$this->model = $model;
1921
}
@@ -56,6 +58,8 @@ public function getSearchResults(string $search): array
5658
return [];
5759
}
5860

61+
/** @var class-string<HasMenuLabel> $modelClass */
62+
/** @phpstan-ignore-next-line */
5963
$results = $modelClass::searchForMenuItems($search)
6064
->limit($this->searchResultLimit)
6165
->get();

src/Form/Fields/Types/RouteMenuItemType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class RouteMenuItemType extends AbstractMenuItemType
99
{
1010
const TYPE_ROUTE = 'route';
1111

12+
final public function __construct() {}
13+
1214
public static function make(?string $model = null): static
1315
{
1416
return new static;
@@ -32,7 +34,7 @@ public function getRouteField(): Select
3234
public function getRouteOptions(): array
3335
{
3436
$routes = [];
35-
$routeCollection = Route::getRoutes();
37+
$routeCollection = Route::getRoutes()->getRoutes();
3638

3739
foreach ($routeCollection as $route) {
3840
$name = $route->getName();

src/Form/Fields/Types/UrlMenuItemType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class UrlMenuItemType extends AbstractMenuItemType
88
{
99
const TYPE_URL = 'url';
1010

11-
public static function make(?string $model = null): static
11+
final public function __construct() {}
12+
13+
public static function make(): static
1214
{
1315
return new static;
1416
}

src/Form/Forms/MenuItemForm.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ protected static function getLinkableField(): Select
121121
$type = static::getTypeByAlias($linkType);
122122

123123
if ($type && $type->isModelType()) {
124+
/** @var LinkableMenuItemType $type */
124125
$modelClass = $type->getModel();
125126

126127
// Use the model's searchForMenuItems scope if it implements HasMenuLabel
127128
if (is_subclass_of($modelClass, HasMenuLabel::class)) {
129+
/** @phpstan-ignore-next-line */
128130
$results = $modelClass::searchForMenuItems($search)
129131
->limit(50)
130132
->get();
@@ -142,6 +144,7 @@ protected static function getLinkableField(): Select
142144
$type = static::getTypeByAlias($linkType);
143145

144146
if ($type && $type->isModelType() && $value) {
147+
/** @var LinkableMenuItemType $type */
145148
$record = app($type->getModel())::find($value);
146149
if ($record && $record instanceof HasMenuLabel) {
147150
return $record->getMenuLabel();
@@ -157,6 +160,7 @@ protected static function getLinkableField(): Select
157160
$type = static::getTypeByAlias($linkType);
158161

159162
if ($type && $type->isModelType()) {
163+
/** @var LinkableMenuItemType $type */
160164
$modelLabel = static::getModelLabelFromResource($type->getModel());
161165

162166
return flexiblePagesTrans('menu_items.form.linkable_help', [
@@ -252,6 +256,7 @@ protected static function getTypeLabel(AbstractMenuItemType $type): string
252256
}
253257

254258
// Get translated model label from Filament resource
259+
/** @var LinkableMenuItemType $type */
255260
return Str::ucfirst(static::getModelLabelFromResource($type->getModel()));
256261
}
257262

src/Http/Controllers/PageController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function () use ($seoMedia, $conversion) {
194194
'height' => $image->getHeight(),
195195
];
196196
});
197-
} catch (CouldNotLoadImage $exception) {
197+
} catch (CouldNotLoadImage $exception) { // @phpstan-ignore-line
198198
return [];
199199
}
200200
}

src/Models/Concerns/HasTitleMenuLabelTrait.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,26 @@ trait HasTitleMenuLabelTrait
77
/**
88
* Get the display label for menu items.
99
* This implementation uses the 'title' field with locale support.
10+
* Assumes the model uses Spatie Laravel Translatable trait.
1011
*/
1112
public function getMenuLabel(?string $locale = null): string
1213
{
1314
$locale = $locale ?: app()->getLocale();
1415

15-
// Check if the model uses translations (like Spatie Laravel Translatable)
16-
if (method_exists($this, 'getTranslation')) {
17-
$title = $this->getTranslation('title', $locale);
18-
if (empty($title)) {
19-
// Fallback to the configured fallback locale
20-
$title = $this->getTranslation('title', config('app.fallback_locale', 'en'));
21-
}
22-
23-
return $title ?: 'Untitled';
16+
// Get translated title (assumes HasTranslations trait is used)
17+
$title = $this->getTranslation('title', $locale);
18+
if (empty($title)) {
19+
// Fallback to the configured fallback locale
20+
$title = $this->getTranslation('title', config('app.fallback_locale', 'en'));
2421
}
2522

26-
return $this->title ?: 'Untitled';
23+
return $title ?: 'Untitled';
2724
}
2825

2926
/**
3027
* Scope to search for models that can be used in menu items.
3128
* This implementation searches in common searchable fields.
29+
* Assumes the model uses Spatie Laravel Translatable trait.
3230
*/
3331
public function scopeSearchForMenuItems($query, string $search)
3432
{
@@ -39,8 +37,7 @@ public function scopeSearchForMenuItems($query, string $search)
3937
$searchableFields = ['name', 'intro', 'description', 'overview_title'];
4038
foreach ($searchableFields as $field) {
4139
if (in_array($field, $this->getFillable()) ||
42-
(method_exists($this, 'getTranslatableAttributes') &&
43-
in_array($field, $this->getTranslatableAttributes()))) {
40+
in_array($field, $this->getTranslatableAttributes())) {
4441
$query->orWhere($field, 'like', "%{$search}%");
4542
}
4643
}

0 commit comments

Comments
 (0)