9
9
class Menu extends Component
10
10
{
11
11
public $ menu ;
12
+
12
13
public $ items ;
14
+
13
15
public $ locale ;
16
+
14
17
public string $ style ;
15
18
16
19
public function __construct (
@@ -20,7 +23,7 @@ public function __construct(
20
23
) {
21
24
$ this ->menu = $ this ->getMenuByCode ($ code );
22
25
$ this ->locale = $ locale ?: app ()->getLocale ();
23
-
26
+
24
27
// Determine the style to use with proper fallback chain
25
28
if ($ style ) {
26
29
$ this ->style = $ style ;
@@ -29,39 +32,39 @@ public function __construct(
29
32
} else {
30
33
$ this ->style = FilamentFlexibleContentBlockPages::config ()->getDefaultMenuStyle ();
31
34
}
32
-
35
+
33
36
$ this ->items = $ this ->menu ? $ this ->getMenuItems ($ this ->menu , $ this ->locale ) : [];
34
37
}
35
38
36
39
public function render ()
37
40
{
38
41
$ theme = FilamentFlexibleContentBlockPages::config ()->getMenuTheme ();
39
42
$ template = "filament-flexible-content-block-pages::components. {$ theme }.menu. {$ this ->style }" ;
40
-
43
+
41
44
// Check if the themed style template exists, otherwise try default style in theme
42
45
if (view ()->exists ($ template )) {
43
46
return view ($ template );
44
47
}
45
-
48
+
46
49
$ defaultTemplate = "filament-flexible-content-block-pages::components. {$ theme }.menu.default " ;
47
50
if (view ()->exists ($ defaultTemplate )) {
48
51
return view ($ defaultTemplate );
49
52
}
50
-
53
+
51
54
// Final fallback to tailwind theme default
52
55
return view ('filament-flexible-content-block-pages::components.tailwind.menu.default ' );
53
56
}
54
57
55
58
protected function getMenuByCode (string $ code )
56
59
{
57
60
$ menuModel = FilamentFlexibleContentBlockPages::config ()->getMenuModel ();
58
-
61
+
59
62
return $ menuModel ::getByCode ($ code );
60
63
}
61
64
62
65
protected function getMenuItems ($ menu , ?string $ locale = null ): array
63
66
{
64
- if (!$ menu ) {
67
+ if (! $ menu ) {
65
68
return [];
66
69
}
67
70
@@ -76,33 +79,33 @@ protected function getMenuItems($menu, ?string $locale = null): array
76
79
protected function buildMenuTree (array $ items , ?string $ locale = null , $ parentId = null ): array
77
80
{
78
81
$ tree = [];
79
-
82
+
80
83
foreach ($ items as $ item ) {
81
84
if ($ item ['parent_id ' ] == $ parentId ) {
82
85
$ processedItem = $ this ->processMenuItem ($ item , $ locale );
83
86
$ children = $ this ->buildMenuTree ($ items , $ locale , $ item ['id ' ]);
84
-
85
- if (!empty ($ children )) {
87
+
88
+ if (! empty ($ children )) {
86
89
$ processedItem ['children ' ] = $ children ;
87
90
$ processedItem ['has_children ' ] = true ;
88
91
} else {
89
92
$ processedItem ['has_children ' ] = false ;
90
93
}
91
-
94
+
92
95
$ tree [] = $ processedItem ;
93
96
}
94
97
}
95
-
98
+
96
99
return $ tree ;
97
100
}
98
101
99
102
protected function processMenuItem (array $ item , ?string $ locale = null ): array
100
103
{
101
104
$ locale = $ locale ?: app ()->getLocale ();
102
-
105
+
103
106
// Get the display label
104
107
$ label = $ item ['label ' ][$ locale ] ?? $ item ['label ' ][config ('app.fallback_locale ' , 'en ' )] ?? '' ;
105
-
108
+
106
109
// If use_model_title is true and we have a linkable model, use its label
107
110
if ($ item ['use_model_title ' ] && $ item ['linkable ' ]) {
108
111
$ linkableModel = $ this ->getLinkableModel ($ item ['linkable_type ' ], $ item ['linkable_id ' ]);
@@ -113,10 +116,10 @@ protected function processMenuItem(array $item, ?string $locale = null): array
113
116
114
117
// Generate the URL
115
118
$ url = $ this ->generateMenuItemUrl ($ item );
116
-
119
+
117
120
// Check if current page matches this menu item
118
121
$ isCurrent = $ this ->isCurrentMenuItem ($ item );
119
-
122
+
120
123
return [
121
124
'id ' => $ item ['id ' ],
122
125
'label ' => $ label ,
@@ -134,14 +137,15 @@ protected function getLinkableModel(string $type, int $id)
134
137
{
135
138
try {
136
139
$ morphMap = FilamentFlexibleContentBlockPages::config ()->getMorphMap ();
137
-
138
- if (!isset ($ morphMap [$ type ])) {
140
+
141
+ if (! isset ($ morphMap [$ type ])) {
139
142
return null ;
140
143
}
141
-
144
+
142
145
$ modelClass = $ morphMap [$ type ];
146
+
143
147
return $ modelClass ::find ($ id );
144
-
148
+
145
149
} catch (\Exception $ e ) {
146
150
return null ;
147
151
}
@@ -152,28 +156,30 @@ protected function generateMenuItemUrl(array $item): string
152
156
switch ($ item ['link_type ' ]) {
153
157
case 'url ' :
154
158
return $ item ['url ' ] ?? '# ' ;
155
-
159
+
156
160
case 'route ' :
157
161
try {
158
162
$ routeName = $ item ['route ' ] ?? '' ;
159
163
if (empty ($ routeName )) {
160
164
return '# ' ;
161
165
}
162
-
166
+
163
167
$ parameters = $ item ['route_parameters ' ] ?? [];
168
+
164
169
return route ($ routeName , $ parameters );
165
-
170
+
166
171
} catch (\Exception $ e ) {
167
172
return '# ' ;
168
173
}
169
-
174
+
170
175
case 'model ' :
171
176
$ linkableModel = $ this ->getLinkableModel ($ item ['linkable_type ' ], $ item ['linkable_id ' ]);
172
177
if ($ linkableModel && method_exists ($ linkableModel , 'getUrl ' )) {
173
178
return $ linkableModel ->getUrl ();
174
179
}
180
+
175
181
return '# ' ;
176
-
182
+
177
183
default :
178
184
return '# ' ;
179
185
}
@@ -183,15 +189,15 @@ protected function isCurrentMenuItem(array $item): bool
183
189
{
184
190
$ currentUrl = request ()->url ();
185
191
$ itemUrl = $ this ->generateMenuItemUrl ($ item );
186
-
192
+
187
193
// Remove trailing slashes for comparison
188
194
$ currentUrl = rtrim ($ currentUrl , '/ ' );
189
195
$ itemUrl = rtrim ($ itemUrl , '/ ' );
190
-
196
+
191
197
if ($ itemUrl === '# ' || empty ($ itemUrl )) {
192
198
return false ;
193
199
}
194
-
200
+
195
201
return $ currentUrl === $ itemUrl ;
196
202
}
197
203
@@ -200,13 +206,13 @@ public function hasActiveChildren(array $item): bool
200
206
if (empty ($ item ['children ' ])) {
201
207
return false ;
202
208
}
203
-
209
+
204
210
foreach ($ item ['children ' ] as $ child ) {
205
211
if ($ child ['is_current ' ] || $ this ->hasActiveChildren ($ child )) {
206
212
return true ;
207
213
}
208
214
}
209
-
215
+
210
216
return false ;
211
217
}
212
- }
218
+ }
0 commit comments