99class Menu extends Component
1010{
1111 public $ menu ;
12+
1213 public $ items ;
14+
1315 public $ locale ;
16+
1417 public string $ style ;
1518
1619 public function __construct (
@@ -20,7 +23,7 @@ public function __construct(
2023 ) {
2124 $ this ->menu = $ this ->getMenuByCode ($ code );
2225 $ this ->locale = $ locale ?: app ()->getLocale ();
23-
26+
2427 // Determine the style to use with proper fallback chain
2528 if ($ style ) {
2629 $ this ->style = $ style ;
@@ -29,39 +32,39 @@ public function __construct(
2932 } else {
3033 $ this ->style = FilamentFlexibleContentBlockPages::config ()->getDefaultMenuStyle ();
3134 }
32-
35+
3336 $ this ->items = $ this ->menu ? $ this ->getMenuItems ($ this ->menu , $ this ->locale ) : [];
3437 }
3538
3639 public function render ()
3740 {
3841 $ theme = FilamentFlexibleContentBlockPages::config ()->getMenuTheme ();
3942 $ template = "filament-flexible-content-block-pages::components. {$ theme }.menu. {$ this ->style }" ;
40-
43+
4144 // Check if the themed style template exists, otherwise try default style in theme
4245 if (view ()->exists ($ template )) {
4346 return view ($ template );
4447 }
45-
48+
4649 $ defaultTemplate = "filament-flexible-content-block-pages::components. {$ theme }.menu.default " ;
4750 if (view ()->exists ($ defaultTemplate )) {
4851 return view ($ defaultTemplate );
4952 }
50-
53+
5154 // Final fallback to tailwind theme default
5255 return view ('filament-flexible-content-block-pages::components.tailwind.menu.default ' );
5356 }
5457
5558 protected function getMenuByCode (string $ code )
5659 {
5760 $ menuModel = FilamentFlexibleContentBlockPages::config ()->getMenuModel ();
58-
61+
5962 return $ menuModel ::getByCode ($ code );
6063 }
6164
6265 protected function getMenuItems ($ menu , ?string $ locale = null ): array
6366 {
64- if (!$ menu ) {
67+ if (! $ menu ) {
6568 return [];
6669 }
6770
@@ -76,33 +79,33 @@ protected function getMenuItems($menu, ?string $locale = null): array
7679 protected function buildMenuTree (array $ items , ?string $ locale = null , $ parentId = null ): array
7780 {
7881 $ tree = [];
79-
82+
8083 foreach ($ items as $ item ) {
8184 if ($ item ['parent_id ' ] == $ parentId ) {
8285 $ processedItem = $ this ->processMenuItem ($ item , $ locale );
8386 $ children = $ this ->buildMenuTree ($ items , $ locale , $ item ['id ' ]);
84-
85- if (!empty ($ children )) {
87+
88+ if (! empty ($ children )) {
8689 $ processedItem ['children ' ] = $ children ;
8790 $ processedItem ['has_children ' ] = true ;
8891 } else {
8992 $ processedItem ['has_children ' ] = false ;
9093 }
91-
94+
9295 $ tree [] = $ processedItem ;
9396 }
9497 }
95-
98+
9699 return $ tree ;
97100 }
98101
99102 protected function processMenuItem (array $ item , ?string $ locale = null ): array
100103 {
101104 $ locale = $ locale ?: app ()->getLocale ();
102-
105+
103106 // Get the display label
104107 $ label = $ item ['label ' ][$ locale ] ?? $ item ['label ' ][config ('app.fallback_locale ' , 'en ' )] ?? '' ;
105-
108+
106109 // If use_model_title is true and we have a linkable model, use its label
107110 if ($ item ['use_model_title ' ] && $ item ['linkable ' ]) {
108111 $ linkableModel = $ this ->getLinkableModel ($ item ['linkable_type ' ], $ item ['linkable_id ' ]);
@@ -113,10 +116,10 @@ protected function processMenuItem(array $item, ?string $locale = null): array
113116
114117 // Generate the URL
115118 $ url = $ this ->generateMenuItemUrl ($ item );
116-
119+
117120 // Check if current page matches this menu item
118121 $ isCurrent = $ this ->isCurrentMenuItem ($ item );
119-
122+
120123 return [
121124 'id ' => $ item ['id ' ],
122125 'label ' => $ label ,
@@ -134,14 +137,15 @@ protected function getLinkableModel(string $type, int $id)
134137 {
135138 try {
136139 $ morphMap = FilamentFlexibleContentBlockPages::config ()->getMorphMap ();
137-
138- if (!isset ($ morphMap [$ type ])) {
140+
141+ if (! isset ($ morphMap [$ type ])) {
139142 return null ;
140143 }
141-
144+
142145 $ modelClass = $ morphMap [$ type ];
146+
143147 return $ modelClass ::find ($ id );
144-
148+
145149 } catch (\Exception $ e ) {
146150 return null ;
147151 }
@@ -152,28 +156,30 @@ protected function generateMenuItemUrl(array $item): string
152156 switch ($ item ['link_type ' ]) {
153157 case 'url ' :
154158 return $ item ['url ' ] ?? '# ' ;
155-
159+
156160 case 'route ' :
157161 try {
158162 $ routeName = $ item ['route ' ] ?? '' ;
159163 if (empty ($ routeName )) {
160164 return '# ' ;
161165 }
162-
166+
163167 $ parameters = $ item ['route_parameters ' ] ?? [];
168+
164169 return route ($ routeName , $ parameters );
165-
170+
166171 } catch (\Exception $ e ) {
167172 return '# ' ;
168173 }
169-
174+
170175 case 'model ' :
171176 $ linkableModel = $ this ->getLinkableModel ($ item ['linkable_type ' ], $ item ['linkable_id ' ]);
172177 if ($ linkableModel && method_exists ($ linkableModel , 'getUrl ' )) {
173178 return $ linkableModel ->getUrl ();
174179 }
180+
175181 return '# ' ;
176-
182+
177183 default :
178184 return '# ' ;
179185 }
@@ -183,15 +189,15 @@ protected function isCurrentMenuItem(array $item): bool
183189 {
184190 $ currentUrl = request ()->url ();
185191 $ itemUrl = $ this ->generateMenuItemUrl ($ item );
186-
192+
187193 // Remove trailing slashes for comparison
188194 $ currentUrl = rtrim ($ currentUrl , '/ ' );
189195 $ itemUrl = rtrim ($ itemUrl , '/ ' );
190-
196+
191197 if ($ itemUrl === '# ' || empty ($ itemUrl )) {
192198 return false ;
193199 }
194-
200+
195201 return $ currentUrl === $ itemUrl ;
196202 }
197203
@@ -200,13 +206,13 @@ public function hasActiveChildren(array $item): bool
200206 if (empty ($ item ['children ' ])) {
201207 return false ;
202208 }
203-
209+
204210 foreach ($ item ['children ' ] as $ child ) {
205211 if ($ child ['is_current ' ] || $ this ->hasActiveChildren ($ child )) {
206212 return true ;
207213 }
208214 }
209-
215+
210216 return false ;
211217 }
212- }
218+ }
0 commit comments