Skip to content

Commit c6f8ff1

Browse files
committed
add "Parent" field for navifation to easily insert data
1 parent 35bb29e commit c6f8ff1

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

resources/lang/en/resources/navigation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@
2929
'label' => 'Content',
3030
'validation_attribute' => 'content',
3131
],
32+
'parent_id' => [
33+
'label' => 'Parent',
34+
'validation_attribute' => 'parent',
35+
],
3236
];

resources/lang/zh_TW/resources/navigation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@
2929
'label' => '內容',
3030
'validation_attribute' => '內容',
3131
],
32+
'parent_id' => [
33+
'label' => '父級',
34+
'validation_attribute' => 'parent',
35+
],
3236
];

src/Filament/Resources/NavigationResource.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static function form(Form $form): Form
4848
->columns(2)
4949
->schema([
5050
static::getCategoryFormComponent(),
51+
static::getParentFormComponent(),
5152
Forms\Components\Grid::make(2)
5253
->schema([
5354
static::getTitleFormComponent(),
@@ -271,7 +272,31 @@ protected static function getCategoryFormComponent()
271272
'main',
272273
'footer',
273274
])
274-
->default('main');
275+
->default('main')
276+
->live()
277+
->afterStateUpdated(function ($old, $state, $set) {
278+
if (trim($old) !== trim($state)) {
279+
$set('parent_id', null);
280+
}
281+
});
282+
}
283+
284+
/**
285+
* @return Forms\Components\Field | Forms\Components\Component
286+
*/
287+
protected static function getParentFormComponent()
288+
{
289+
return Forms\Components\Select::make('parent_id')
290+
->label(__('inspirecms::resources/navigation.parent_id.label'))
291+
->validationAttribute(__('inspirecms::resources/navigation.parent_id.validation_attribute'))
292+
->options(function ($record, $get) {
293+
$keyName = app(static::getModel())->getKeyName();
294+
return static::getEloquentQuery()
295+
->where('category', $get('category'))
296+
->when($record, fn ($query) => $query->whereNot($keyName, $record->getKey()))
297+
->pluck('title', $keyName)
298+
->toArray();
299+
});
275300
}
276301

277302
/**

0 commit comments

Comments
 (0)