5
5
use Filament \Actions \Action ;
6
6
use Filament \Actions \Concerns \InteractsWithActions ;
7
7
use Filament \Actions \Contracts \HasActions ;
8
+ use Filament \Actions \LocaleSwitcher ;
8
9
use Filament \Forms \Concerns \InteractsWithForms ;
9
10
use Filament \Forms \Contracts \HasForms ;
10
11
use Livewire \Component ;
12
+ use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
11
13
use Statikbe \FilamentFlexibleContentBlockPages \Filament \Form \Forms \MenuItemForm ;
12
14
13
15
class MenuTreeItem extends Component implements HasActions, HasForms
14
16
{
15
17
use InteractsWithActions;
16
18
use InteractsWithForms;
17
19
18
- public array $ item ;
20
+ public $ item ; // MenuItem model
19
21
20
22
public int $ depth = 0 ;
21
23
@@ -25,9 +27,16 @@ class MenuTreeItem extends Component implements HasActions, HasForms
25
27
26
28
public bool $ isExpanded = true ;
27
29
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
29
31
{
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
+
31
40
$ this ->depth = $ depth ;
32
41
$ this ->maxDepth = $ maxDepth ;
33
42
$ this ->showActions = $ showActions ;
@@ -46,30 +55,26 @@ public function canHaveChildren(): bool
46
55
47
56
public function hasChildren (): bool
48
57
{
49
- return ! empty ( $ this ->item [ ' children ' ] );
58
+ return $ this ->item -> children ()-> exists ( );
50
59
}
51
60
52
61
public function getItemDisplayLabel (): string
53
62
{
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 ();
59
64
}
60
65
61
66
public function getItemTypeLabel (): string
62
67
{
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 );
65
70
}
66
71
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 ;
69
74
}
70
75
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 ;
73
78
}
74
79
75
80
return flexiblePagesTrans ('menu_items.tree.no_link ' );
@@ -80,42 +85,48 @@ public function addChildAction(): Action
80
85
return Action::make ('addChild ' )
81
86
->form (MenuItemForm::getSchema ())
82
87
->fillForm ([
83
- 'parent_id ' => $ this ->item [ ' id ' ] ,
88
+ 'parent_id ' => $ this ->item -> id ,
84
89
'is_visible ' => true ,
85
90
'target ' => '_self ' ,
86
91
])
87
92
->action (function (array $ data ): void {
88
- $ data ['parent_id ' ] = $ this ->item [ ' id ' ] ;
93
+ $ data ['parent_id ' ] = $ this ->item -> id ;
89
94
$ this ->createMenuItem ($ data );
90
95
})
91
96
->modalHeading (flexiblePagesTrans ('menu_items.tree.add_child ' ))
92
97
->modalSubmitActionLabel (__ ('Create ' ))
93
98
->modalWidth ('2xl ' )
94
- ->slideOver ();
99
+ ->slideOver ()
100
+ ->extraModalFooterActions ([
101
+ LocaleSwitcher::make (),
102
+ ]);
95
103
}
96
104
97
105
public function editAction (): Action
98
106
{
99
107
return Action::make ('edit ' )
100
108
->form (MenuItemForm::getSchema ())
101
109
->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 ,
111
119
])
112
120
->action (function (array $ data ): void {
113
- $ this ->updateMenuItem ($ this ->item [ ' id ' ] , $ data );
121
+ $ this ->updateMenuItem ($ this ->item -> id , $ data );
114
122
})
115
123
->modalHeading (flexiblePagesTrans ('menu_items.tree.edit ' ))
116
124
->modalSubmitActionLabel (__ ('Update ' ))
117
125
->modalWidth ('2xl ' )
118
- ->slideOver ();
126
+ ->slideOver ()
127
+ ->extraModalFooterActions ([
128
+ LocaleSwitcher::make (),
129
+ ]);
119
130
}
120
131
121
132
public function deleteAction (): Action
@@ -127,7 +138,7 @@ public function deleteAction(): Action
127
138
->modalSubmitActionLabel (flexiblePagesTrans ('menu_items.tree.delete ' ))
128
139
->color ('danger ' )
129
140
->action (function (): void {
130
- $ this ->deleteMenuItem ($ this ->item [ ' id ' ] );
141
+ $ this ->deleteMenuItem ($ this ->item -> id );
131
142
});
132
143
}
133
144
0 commit comments