55use Filament \Actions \Action ;
66use Filament \Actions \Concerns \InteractsWithActions ;
77use Filament \Actions \Contracts \HasActions ;
8+ use Filament \Actions \LocaleSwitcher ;
89use Filament \Forms \Concerns \InteractsWithForms ;
910use Filament \Forms \Contracts \HasForms ;
1011use Livewire \Component ;
12+ use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
1113use Statikbe \FilamentFlexibleContentBlockPages \Filament \Form \Forms \MenuItemForm ;
1214
1315class MenuTreeItem extends Component implements HasActions, HasForms
1416{
1517 use InteractsWithActions;
1618 use InteractsWithForms;
1719
18- public array $ item ;
20+ public $ item ; // MenuItem model
1921
2022 public int $ depth = 0 ;
2123
@@ -25,9 +27,16 @@ class MenuTreeItem extends Component implements HasActions, HasForms
2527
2628 public bool $ isExpanded = true ;
2729
28- public function mount (array $ item , int $ depth = 0 , int $ maxDepth = 2 , bool $ showActions = true ): void
30+ public function mount ($ item , int $ depth = 0 , int $ maxDepth = 2 , bool $ showActions = true ): void
2931 {
30- $ this ->item = $ item ;
32+ // Convert array to model if needed
33+ if (is_array ($ item )) {
34+ $ menuItemModel = FilamentFlexibleContentBlockPages::config ()->getMenuItemModel ();
35+ $ this ->item = $ menuItemModel ::with ('linkable ' )->find ($ item ['id ' ]);
36+ } else {
37+ $ this ->item = $ item ;
38+ }
39+
3140 $ this ->depth = $ depth ;
3241 $ this ->maxDepth = $ maxDepth ;
3342 $ this ->showActions = $ showActions ;
@@ -46,30 +55,26 @@ public function canHaveChildren(): bool
4655
4756 public function hasChildren (): bool
4857 {
49- return ! empty ( $ this ->item [ ' children ' ] );
58+ return $ this ->item -> children ()-> exists ( );
5059 }
5160
5261 public function getItemDisplayLabel (): string
5362 {
54- if (($ this ->item ['use_model_title ' ] ?? false ) && ! empty ($ this ->item ['linkable ' ])) {
55- return $ this ->item ['linkable ' ]['title ' ] ?? $ this ->item ['label ' ] ?? flexiblePagesTrans ('menu_items.status.no_label ' );
56- }
57-
58- return $ this ->item ['label ' ] ?? flexiblePagesTrans ('menu_items.status.no_label ' );
63+ return $ this ->item ->getDisplayLabel ();
5964 }
6065
6166 public function getItemTypeLabel (): string
6267 {
63- if (! empty ( $ this ->item [ ' linkable_type ' ]) && ! empty ( $ this ->item [ ' linkable ' ]) ) {
64- return flexiblePagesTrans ('menu_items.tree.linked_to ' ). ' ' . class_basename ($ this ->item [ ' linkable_type ' ] );
68+ if ($ this ->item -> linkable_type && $ this ->item -> linkable ) {
69+ return flexiblePagesTrans ('menu_items.tree.linked_to ' ) . ' ' . class_basename ($ this ->item -> linkable_type );
6570 }
6671
67- if (! empty ( $ this ->item [ ' url ' ]) ) {
68- return flexiblePagesTrans ('menu_items.tree.external_url ' ). ': ' . $ this ->item [ ' url ' ] ;
72+ if ($ this ->item -> url ) {
73+ return flexiblePagesTrans ('menu_items.tree.external_url ' ) . ': ' . $ this ->item -> url ;
6974 }
7075
71- if (! empty ( $ this ->item [ ' route ' ]) ) {
72- return flexiblePagesTrans ('menu_items.tree.route ' ). ': ' . $ this ->item [ ' route ' ] ;
76+ if ($ this ->item -> route ) {
77+ return flexiblePagesTrans ('menu_items.tree.route ' ) . ': ' . $ this ->item -> route ;
7378 }
7479
7580 return flexiblePagesTrans ('menu_items.tree.no_link ' );
@@ -80,42 +85,48 @@ public function addChildAction(): Action
8085 return Action::make ('addChild ' )
8186 ->form (MenuItemForm::getSchema ())
8287 ->fillForm ([
83- 'parent_id ' => $ this ->item [ ' id ' ] ,
88+ 'parent_id ' => $ this ->item -> id ,
8489 'is_visible ' => true ,
8590 'target ' => '_self ' ,
8691 ])
8792 ->action (function (array $ data ): void {
88- $ data ['parent_id ' ] = $ this ->item [ ' id ' ] ;
93+ $ data ['parent_id ' ] = $ this ->item -> id ;
8994 $ this ->createMenuItem ($ data );
9095 })
9196 ->modalHeading (flexiblePagesTrans ('menu_items.tree.add_child ' ))
9297 ->modalSubmitActionLabel (__ ('Create ' ))
9398 ->modalWidth ('2xl ' )
94- ->slideOver ();
99+ ->slideOver ()
100+ ->extraModalFooterActions ([
101+ LocaleSwitcher::make (),
102+ ]);
95103 }
96104
97105 public function editAction (): Action
98106 {
99107 return Action::make ('edit ' )
100108 ->form (MenuItemForm::getSchema ())
101109 ->fillForm ([
102- 'link_type ' => $ this ->item [ ' link_type ' ] ,
103- 'label ' => $ this ->item [ ' label ' ] ,
104- 'use_model_title ' => $ this ->item [ ' use_model_title ' ] ,
105- 'url ' => $ this ->item [ ' url ' ] ,
106- 'route ' => $ this ->item [ ' route ' ] ,
107- 'linkable_id ' => $ this ->item [ ' linkable_id ' ] ,
108- 'target ' => $ this ->item [ ' target ' ] ?? '_self ' ,
109- 'icon ' => $ this ->item [ ' icon ' ] ,
110- 'is_visible ' => $ this ->item [ ' is_visible ' ] ,
110+ 'link_type ' => $ this ->item -> link_type ,
111+ 'label ' => $ this ->item -> label ,
112+ 'use_model_title ' => $ this ->item -> use_model_title ,
113+ 'url ' => $ this ->item -> url ,
114+ 'route ' => $ this ->item -> route ,
115+ 'linkable_id ' => $ this ->item -> linkable_id ,
116+ 'target ' => $ this ->item -> target ?? '_self ' ,
117+ 'icon ' => $ this ->item -> icon ,
118+ 'is_visible ' => $ this ->item -> is_visible ,
111119 ])
112120 ->action (function (array $ data ): void {
113- $ this ->updateMenuItem ($ this ->item [ ' id ' ] , $ data );
121+ $ this ->updateMenuItem ($ this ->item -> id , $ data );
114122 })
115123 ->modalHeading (flexiblePagesTrans ('menu_items.tree.edit ' ))
116124 ->modalSubmitActionLabel (__ ('Update ' ))
117125 ->modalWidth ('2xl ' )
118- ->slideOver ();
126+ ->slideOver ()
127+ ->extraModalFooterActions ([
128+ LocaleSwitcher::make (),
129+ ]);
119130 }
120131
121132 public function deleteAction (): Action
@@ -127,7 +138,7 @@ public function deleteAction(): Action
127138 ->modalSubmitActionLabel (flexiblePagesTrans ('menu_items.tree.delete ' ))
128139 ->color ('danger ' )
129140 ->action (function (): void {
130- $ this ->deleteMenuItem ($ this ->item [ ' id ' ] );
141+ $ this ->deleteMenuItem ($ this ->item -> id );
131142 });
132143 }
133144
0 commit comments