Skip to content

Commit bd63185

Browse files
committed
Fix missing linkable type in item form
1 parent 8de807c commit bd63185

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Filament/Form/Forms/MenuItemForm.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class MenuItemForm
2222

2323
const FIELD_LINKABLE_ID = 'linkable_id';
2424

25+
const FIELD_LINKABLE_TYPE = 'linkable_type';
26+
2527
const FIELD_URL = 'url';
2628

2729
const FIELD_ROUTE = 'route';
@@ -52,6 +54,7 @@ public static function getSchema(): array
5254
// Link Configuration Section
5355
Grid::make(1)->schema([
5456
static::getLinkableField(),
57+
static::getLinkableTypeField(),
5558
static::getUrlField(),
5659
static::getRouteField(),
5760
])->columnSpan(1),
@@ -79,7 +82,12 @@ protected static function getLinkTypeField(): Select
7982
->options(static::getLinkTypeOptions())
8083
->required()
8184
->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+
});
8391
}
8492

8593
protected static function getLabelField(): TextInput
@@ -151,9 +159,23 @@ protected static function getLinkableField(): Select
151159
}
152160

153161
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+
}
154171
});
155172
}
156173

174+
protected static function getLinkableTypeField(): Hidden
175+
{
176+
return Hidden::make(static::FIELD_LINKABLE_TYPE);
177+
}
178+
157179
protected static function getUrlField(): TextInput
158180
{
159181
return TextInput::make(static::FIELD_URL)

0 commit comments

Comments
 (0)