Skip to content

Commit 26efb68

Browse files
committed
Add max depth to menu model
1 parent 0270835 commit 26efb68

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

database/migrations/create_filament_flexible_content_block_menus_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ return new class extends Migration
1515
$table->string('code')->unique();
1616
$table->text('description')->nullable();
1717
$table->string('style')->default('default');
18+
$table->integer('max_depth')->nullable();
1819
$table->timestamps();
1920

2021
// Index for code lookups

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@
6868
'description_help' => 'Optional description for content managers. Not displayed to website visitors.',
6969
'style_lbl' => 'Menu Style',
7070
'style_help' => 'Choose the visual style for this menu. This affects how the menu is displayed on the frontend.',
71+
'max_depth_lbl' => 'Maximum depth',
72+
'max_depth_help' => 'Maximum number of menu levels allowed (1-10). Leave blank to use the global default.',
7173
],
7274
'table' => [
7375
'name_col' => 'Name',
7476
'code_col' => 'Code',
7577
'style_col' => 'Style',
78+
'max_depth_col' => 'Max depth',
79+
'default_depth' => 'Default',
7680
'items_count_col' => 'Items',
7781
'created_at_col' => 'Created at',
7882
],

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@
6868
'description_help' => 'Optionele beschrijving voor contentbeheerders. Wordt niet getoond aan websitebezoekers.',
6969
'style_lbl' => 'Menu Stijl',
7070
'style_help' => 'Kies de visuele stijl voor dit menu. Dit beïnvloedt hoe het menu wordt weergegeven op de frontend.',
71+
'max_depth_lbl' => 'Maximale diepte',
72+
'max_depth_help' => 'Maximaal aantal menu niveaus toegestaan (1-10). Laat leeg om de globale standaard te gebruiken.',
7173
],
7274
'table' => [
7375
'name_col' => 'Naam',
7476
'code_col' => 'Code',
7577
'style_col' => 'Stijl',
78+
'max_depth_col' => 'Max diepte',
79+
'default_depth' => 'Standaard',
7680
'items_count_col' => 'Items',
7781
'created_at_col' => 'Aangemaakt op',
7882
],

src/Models/Menu.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
use Statikbe\FilamentFlexibleContentBlocks\Models\Concerns\HasCodeTrait;
1010
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasCode;
1111

12+
/**
13+
* @property int $id
14+
* @property string $name
15+
* @property string $code
16+
* @property string|null $description
17+
* @property string $style
18+
* @property int|null $max_depth
19+
* @property \Carbon\Carbon $created_at
20+
* @property \Carbon\Carbon $updated_at
21+
* @property \Illuminate\Database\Eloquent\Collection<\Statikbe\FilamentFlexibleContentBlockPages\Models\MenuItem> $menuItems
22+
* @property \Illuminate\Database\Eloquent\Collection<\Statikbe\FilamentFlexibleContentBlockPages\Models\MenuItem> $allMenuItems
23+
*/
1224
class Menu extends Model implements HasCode
1325
{
1426
use HasCodeTrait;
@@ -19,6 +31,7 @@ class Menu extends Model implements HasCode
1931
'code',
2032
'description',
2133
'style',
34+
'max_depth',
2235
];
2336

2437
public function getTable()
@@ -56,4 +69,10 @@ public function getEffectiveStyle(): string
5669

5770
return FilamentFlexibleContentBlockPages::config()->getDefaultMenuStyle();
5871
}
72+
73+
public function getEffectiveMaxDepth(): int
74+
{
75+
// Return the menu's max_depth if set, otherwise fall back to config default
76+
return $this->max_depth ?? FilamentFlexibleContentBlockPages::config()->getMenuMaxDepth();
77+
}
5978
}

src/Resources/MenuResource.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public static function form(Form $form): Form
7979
->helperText(flexiblePagesTrans('menus.form.style_help'));
8080
}
8181

82+
$formFields[] = TextInput::make('max_depth')
83+
->label(flexiblePagesTrans('menus.form.max_depth_lbl'))
84+
->numeric()
85+
->minValue(1)
86+
->maxValue(10)
87+
->placeholder(FilamentFlexibleContentBlockPages::config()->getMenuMaxDepth())
88+
->helperText(flexiblePagesTrans('menus.form.max_depth_help'));
89+
8290
$formFields[] = Textarea::make('description')
8391
->label(flexiblePagesTrans('menus.form.description_lbl'))
8492
->rows(3)
@@ -116,6 +124,13 @@ public static function table(Table $table): Table
116124
->color('gray')
117125
->visible(fn () => count(FilamentFlexibleContentBlockPages::config()->getMenuStyles()) > 1),
118126

127+
TextColumn::make('max_depth')
128+
->label(flexiblePagesTrans('menus.table.max_depth_col'))
129+
->formatStateUsing(function (?int $state): string {
130+
return $state ? (string) $state : flexiblePagesTrans('menus.table.default_depth');
131+
})
132+
->sortable(),
133+
119134
TextColumn::make('menuItems_count')
120135
->label(flexiblePagesTrans('menus.table.items_count_col'))
121136
->counts('menuItems')

src/Resources/MenuResource/Pages/ManageMenuItems.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ManageMenuItems extends TreePage
2222

2323
protected static string $resource = MenuResource::class;
2424

25+
protected static int $modelMaxDepth;
26+
2527
public mixed $menu;
2628

2729
public function mount(): void
@@ -216,6 +218,21 @@ protected function getFormSchema(): array
216218

217219
public static function getMaxDepth(): int
218220
{
219-
return FilamentFlexibleContentBlockPages::config()->getMenuMaxDepth() ?? 3;
221+
if (! isset(static::$modelMaxDepth)) {
222+
// Since this is static, we need to get the menu from the current request
223+
$recordId = request()->route('record');
224+
if ($recordId) {
225+
$menuModelClass = MenuResource::getModel();
226+
$menu = app($menuModelClass)->find($recordId);
227+
if ($menu) {
228+
return $menu->getEffectiveMaxDepth();
229+
}
230+
}
231+
232+
// Fallback to global config if we can't determine the menu
233+
static::$modelMaxDepth = FilamentFlexibleContentBlockPages::config()->getMenuMaxDepth() ?? 3;
234+
}
235+
236+
return static::$modelMaxDepth;
220237
}
221238
}

0 commit comments

Comments
 (0)