1
+ <?php
2
+
3
+ namespace Statikbe \FilamentFlexibleContentBlockPages \Models \Concerns ;
4
+
5
+ trait HasTitleMenuLabelTrait
6
+ {
7
+ /**
8
+ * Get the display label for menu items.
9
+ * This implementation uses the 'title' field with locale support.
10
+ */
11
+ public function getMenuLabel (?string $ locale = null ): string
12
+ {
13
+ $ locale = $ locale ?: app ()->getLocale ();
14
+
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
+ return $ title ?: 'Untitled ' ;
23
+ }
24
+
25
+ return $ this ->title ?: 'Untitled ' ;
26
+ }
27
+
28
+ /**
29
+ * Scope to search for models that can be used in menu items.
30
+ * This implementation searches in common searchable fields.
31
+ */
32
+ public function scopeSearchForMenuItems ($ query , string $ search )
33
+ {
34
+ return $ query ->where (function ($ query ) use ($ search ) {
35
+ $ query ->where ('title ' , 'like ' , "% {$ search }% " );
36
+
37
+ // Add additional searchable fields if they exist
38
+ $ searchableFields = ['name ' , 'intro ' , 'description ' , 'overview_title ' ];
39
+ foreach ($ searchableFields as $ field ) {
40
+ if (in_array ($ field , $ this ->getFillable ()) ||
41
+ (method_exists ($ this , 'getTranslatableAttributes ' ) &&
42
+ in_array ($ field , $ this ->getTranslatableAttributes ()))) {
43
+ $ query ->orWhere ($ field , 'like ' , "% {$ search }% " );
44
+ }
45
+ }
46
+ });
47
+ }
48
+ }
0 commit comments