Skip to content

Commit 3d9b95d

Browse files
committed
Allow updating slug and only set it when not saved yet
1 parent d60fbd1 commit 3d9b95d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/Filament/RelationManagers/FieldsRelationManager.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ public function form(Form $form): Form
4343
->required()
4444
->placeholder(__('Name'))
4545
->live(onBlur: true)
46-
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
46+
->afterStateUpdated(function (Set $set, Get $get, ?string $state, ?string $old, ?Field $record) {
47+
if (! $record || blank($get('slug'))) {
48+
$set('slug', Str::slug($state));
49+
}
50+
51+
$currentSlug = $get('slug');
52+
53+
if (! $record?->slug && (! $currentSlug || $currentSlug === Str::slug($old))) {
54+
$set('slug', Str::slug($state));
55+
}
56+
}),
4757

48-
TextInput::make('slug')
49-
->readonly(),
58+
TextInput::make('slug'),
5059

5160
Select::make('field_type')
5261
->searchable()
@@ -60,8 +69,8 @@ public function form(Form $form): Form
6069
...FieldEnum::array(),
6170
...$this->prepareCustomFieldOptions(Fields::getFields()),
6271
])
63-
->sortBy(fn ($value) => $value)
64-
->mapWithKeys(fn ($value, $key) => [
72+
->sortBy(fn($value) => $value)
73+
->mapWithKeys(fn($value, $key) => [
6574
$key => Str::headline($value),
6675
])
6776
->toArray();
@@ -79,10 +88,10 @@ public function form(Form $form): Form
7988
]),
8089
Section::make('Configuration')
8190
->columns(3)
82-
->schema(fn (Get $get) => $this->getFieldTypeFormSchema(
91+
->schema(fn(Get $get) => $this->getFieldTypeFormSchema(
8392
$get('field_type')
8493
))
85-
->visible(fn (Get $get) => filled($get('field_type'))),
94+
->visible(fn(Get $get) => filled($get('field_type'))),
8695
]),
8796
]);
8897
}
@@ -181,8 +190,8 @@ public function table(Table $table): Table
181190
->after(function (Component $livewire, array $data, Model $record, array $arguments) {
182191
if (
183192
isset($record->valueColumn) && $this->ownerRecord->getConnection()
184-
->getSchemaBuilder()
185-
->hasColumn($this->ownerRecord->getTable(), $record->valueColumn)
193+
->getSchemaBuilder()
194+
->hasColumn($this->ownerRecord->getTable(), $record->valueColumn)
186195
) {
187196

188197
$key = $this->ownerRecord->getKeyName() ?? 'id';

0 commit comments

Comments
 (0)