@@ -22,6 +22,8 @@ class MenuItemForm
22
22
23
23
const FIELD_LINKABLE_ID = 'linkable_id ' ;
24
24
25
+ const FIELD_LINKABLE_TYPE = 'linkable_type ' ;
26
+
25
27
const FIELD_URL = 'url ' ;
26
28
27
29
const FIELD_ROUTE = 'route ' ;
@@ -52,6 +54,7 @@ public static function getSchema(): array
52
54
// Link Configuration Section
53
55
Grid::make (1 )->schema ([
54
56
static ::getLinkableField (),
57
+ static ::getLinkableTypeField (),
55
58
static ::getUrlField (),
56
59
static ::getRouteField (),
57
60
])->columnSpan (1 ),
@@ -79,7 +82,12 @@ protected static function getLinkTypeField(): Select
79
82
->options (static ::getLinkTypeOptions ())
80
83
->required ()
81
84
->live ()
82
- ->helperText (flexiblePagesTrans ('menu_items.form.link_type_help ' ));
85
+ ->helperText (flexiblePagesTrans ('menu_items.form.link_type_help ' ))
86
+ ->afterStateUpdated (function ($ state , callable $ set ) {
87
+ // Clear linkable fields when link type changes
88
+ $ set (static ::FIELD_LINKABLE_ID , null );
89
+ $ set (static ::FIELD_LINKABLE_TYPE , null );
90
+ });
83
91
}
84
92
85
93
protected static function getLabelField (): TextInput
@@ -151,9 +159,23 @@ protected static function getLinkableField(): Select
151
159
}
152
160
153
161
return '' ;
162
+ })
163
+ ->afterStateUpdated (function ($ state , callable $ set , Get $ get ) {
164
+ // When linkable_id changes, automatically set linkable_type
165
+ $ linkType = $ get (static ::FIELD_LINK_TYPE );
166
+ $ type = static ::getTypeByAlias ($ linkType );
167
+
168
+ if ($ type && $ type ->isModelType () && $ state ) {
169
+ $ set (static ::FIELD_LINKABLE_TYPE , $ type ->getModel ());
170
+ }
154
171
});
155
172
}
156
173
174
+ protected static function getLinkableTypeField (): Hidden
175
+ {
176
+ return Hidden::make (static ::FIELD_LINKABLE_TYPE );
177
+ }
178
+
157
179
protected static function getUrlField (): TextInput
158
180
{
159
181
return TextInput::make (static ::FIELD_URL )
0 commit comments