Skip to content

Commit 297c174

Browse files
committed
refactor: simplify field form layout and hide entity type
1 parent cd9e48c commit 297c174

File tree

2 files changed

+82
-150
lines changed

2 files changed

+82
-150
lines changed

src/Console/Commands/MigrateEmailFieldValuesCommand.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Console\Command;
88
use Illuminate\Support\Facades\DB;
9+
use Throwable;
910

1011
/**
1112
* Migrates email field values from string_value to json_value format.
@@ -25,12 +26,10 @@ public function handle(): int
2526
{
2627
$isDryRun = $this->option('dry-run');
2728

28-
if (app()->isProduction() && ! $isDryRun && ! $this->option('force')) {
29-
if (! $this->confirm('You are running in production. Are you sure you want to continue?')) {
30-
$this->info('Migration cancelled.');
29+
if (app()->isProduction() && ! $isDryRun && ! $this->option('force') && ! $this->confirm('You are running in production. Are you sure you want to continue?')) {
30+
$this->info('Migration cancelled.');
3131

32-
return self::SUCCESS;
33-
}
32+
return self::SUCCESS;
3433
}
3534

3635
$fieldTable = config('custom-fields.database.table_names.custom_fields');
@@ -54,7 +53,7 @@ public function handle(): int
5453
->whereIn('custom_field_id', $emailFields)
5554
->whereNotNull('string_value')
5655
->where('string_value', '!=', '')
57-
->where(function ($query) {
56+
->where(function ($query): void {
5857
$query->whereNull('json_value')
5958
->orWhere('json_value', '=', '[]')
6059
->orWhere('json_value', '=', 'null');
@@ -75,7 +74,7 @@ public function handle(): int
7574

7675
$this->table(
7776
['ID', 'Entity Type', 'Entity ID', 'Current Value', 'New Format'],
78-
$valuesToMigrate->map(fn ($value) => [
77+
$valuesToMigrate->map(fn ($value): array => [
7978
$value->id,
8079
$value->entity_type,
8180
$value->entity_id,
@@ -103,7 +102,7 @@ public function handle(): int
103102
]);
104103

105104
$migrated++;
106-
} catch (\Throwable $e) {
105+
} catch (Throwable $e) {
107106
$this->newLine();
108107
$this->error(sprintf('Failed to migrate value ID %d: %s', $value->id, $e->getMessage()));
109108
$errors++;

src/Filament/Management/Schemas/FieldForm.php

Lines changed: 75 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Filament\Forms\Components\Toggle;
1515
use Filament\Schemas\Components\Component;
1616
use Filament\Schemas\Components\Fieldset;
17+
use Filament\Schemas\Components\Grid;
1718
use Filament\Schemas\Components\Tabs;
1819
use Filament\Schemas\Components\Tabs\Tab;
1920
use Filament\Schemas\Components\Utilities\Get;
@@ -131,157 +132,89 @@ public static function schema(bool $withOptionsRelationship = true): array
131132
Tab::make(
132133
__('custom-fields::custom-fields.field.form.general')
133134
)->schema([
134-
Select::make('entity_type')
135-
->label(
136-
__(
137-
'custom-fields::custom-fields.field.form.entity_type'
138-
)
139-
)
140-
->options(Entities::getOptions(onlyCustomFields: true))
141-
->disabled()
135+
Hidden::make('entity_type')
142136
->default(
143137
fn () => request(
144138
'entityType',
145139
(Entities::withCustomFields()->first()?->getAlias()) ?? ''
146140
)
147-
)
148-
->required(),
149-
TypeField::make('type')
150-
->label(
151-
__(
152-
'custom-fields::custom-fields.field.form.type'
153-
)
154-
)
155-
->disabled(
156-
fn (
157-
?CustomField $record
158-
): bool => (bool) $record?->exists
159-
)
160-
->live()
161-
->afterStateHydrated(function (
162-
Select $component,
163-
mixed $state,
164-
?CustomField $record
165-
): void {
166-
if (blank($state)) {
167-
$component->state(
168-
$record->type ?? CustomFieldsType::toCollection()->first()->key
169-
);
170-
}
171-
})
172-
->required(),
173-
TextInput::make('name')
174-
->label(
175-
__(
176-
'custom-fields::custom-fields.field.form.name'
177-
)
178-
)
179-
->helperText(
180-
__(
181-
'custom-fields::custom-fields.field.form.name_helper_text'
182-
)
183-
)
184-
->live(onBlur: true)
185-
->required()
186-
->maxLength(50)
187-
->disabled(
188-
fn (
189-
?CustomField $record
190-
): bool => (bool) $record?->system_defined
191-
)
192-
->unique(
193-
table: CustomFields::customFieldModel(),
194-
column: 'name',
195-
ignoreRecord: true,
196-
modifyRuleUsing: fn (
197-
Unique $rule,
198-
Get $get
199-
) => $rule
200-
->where('entity_type', $get('entity_type'))
201-
->when(
202-
FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY),
203-
fn (Unique $rule) => $rule->where(
204-
config(
205-
'custom-fields.database.column_names.tenant_foreign_key'
206-
),
207-
TenantContextService::getCurrentTenantId()
208-
)
141+
),
142+
Grid::make()
143+
->columns(fn (): int => FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CODE_AUTO_GENERATE) ? 2 : 3)
144+
->columnSpanFull()
145+
->schema([
146+
TypeField::make('type')
147+
->label(__('custom-fields::custom-fields.field.form.type'))
148+
->disabled(fn (?CustomField $record): bool => (bool) $record?->exists)
149+
->live()
150+
->afterStateHydrated(function (
151+
Select $component,
152+
mixed $state,
153+
?CustomField $record
154+
): void {
155+
if (blank($state)) {
156+
$component->state(
157+
$record->type ?? CustomFieldsType::toCollection()->first()->key
158+
);
159+
}
160+
})
161+
->required(),
162+
TextInput::make('name')
163+
->label(__('custom-fields::custom-fields.field.form.name'))
164+
->live(onBlur: true)
165+
->required()
166+
->maxLength(50)
167+
->disabled(fn (?CustomField $record): bool => (bool) $record?->system_defined)
168+
->unique(
169+
table: CustomFields::customFieldModel(),
170+
column: 'name',
171+
ignoreRecord: true,
172+
modifyRuleUsing: fn (Unique $rule, Get $get) => $rule
173+
->where('entity_type', $get('entity_type'))
174+
->when(
175+
FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY),
176+
fn (Unique $rule) => $rule->where(
177+
config('custom-fields.database.column_names.tenant_foreign_key'),
178+
TenantContextService::getCurrentTenantId()
179+
)
180+
)
209181
)
210-
)
211-
->afterStateUpdated(function (
212-
Get $get,
213-
Set $set,
214-
?string $old,
215-
?string $state
216-
): void {
217-
$old ??= '';
218-
$state ??= '';
182+
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state): void {
183+
$old ??= '';
184+
$state ??= '';
219185

220-
if (
221-
($get('code') ?? '') !==
222-
Str::of($old)->slug('_')->toString()
223-
) {
224-
return;
225-
}
186+
if (($get('code') ?? '') !== Str::of($old)->slug('_')->toString()) {
187+
return;
188+
}
226189

227-
$set(
228-
'code',
229-
Str::of($state)->slug('_')->toString()
230-
);
231-
}),
232-
TextInput::make('code')
233-
->label(
234-
__(
235-
'custom-fields::custom-fields.field.form.code'
236-
)
237-
)
238-
->helperText(
239-
__(
240-
'custom-fields::custom-fields.field.form.code_helper_text'
241-
)
242-
)
243-
->live(onBlur: true)
244-
->required(
245-
fn (): bool => ! FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CODE_AUTO_GENERATE)
246-
)
247-
->alphaDash()
248-
->maxLength(50)
249-
->disabled(
250-
fn (
251-
?CustomField $record
252-
): bool => (bool) $record?->system_defined
253-
)
254-
->visible(
255-
fn (): bool => ! FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CODE_AUTO_GENERATE)
256-
)
257-
->unique(
258-
table: CustomFields::customFieldModel(),
259-
column: 'code',
260-
ignoreRecord: true,
261-
modifyRuleUsing: fn (
262-
Unique $rule,
263-
Get $get
264-
) => $rule
265-
->where('entity_type', $get('entity_type'))
266-
->when(
267-
FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY),
268-
fn (Unique $rule) => $rule->where(
269-
config(
270-
'custom-fields.database.column_names.tenant_foreign_key'
271-
),
272-
TenantContextService::getCurrentTenantId()
273-
)
190+
$set('code', Str::of($state)->slug('_')->toString());
191+
}),
192+
TextInput::make('code')
193+
->label(__('custom-fields::custom-fields.field.form.code'))
194+
->live(onBlur: true)
195+
->required(fn (): bool => ! FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CODE_AUTO_GENERATE))
196+
->alphaDash()
197+
->maxLength(50)
198+
->disabled(fn (?CustomField $record): bool => (bool) $record?->system_defined)
199+
->visible(fn (): bool => ! FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CODE_AUTO_GENERATE))
200+
->unique(
201+
table: CustomFields::customFieldModel(),
202+
column: 'code',
203+
ignoreRecord: true,
204+
modifyRuleUsing: fn (Unique $rule, Get $get) => $rule
205+
->where('entity_type', $get('entity_type'))
206+
->when(
207+
FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY),
208+
fn (Unique $rule) => $rule->where(
209+
config('custom-fields.database.column_names.tenant_foreign_key'),
210+
TenantContextService::getCurrentTenantId()
211+
)
212+
)
274213
)
275-
)
276-
->afterStateUpdated(function (
277-
Set $set,
278-
?string $state
279-
): void {
280-
$set(
281-
'code',
282-
Str::of($state)->slug('_')->toString()
283-
);
284-
}),
214+
->afterStateUpdated(function (Set $set, ?string $state): void {
215+
$set('code', Str::of($state)->slug('_')->toString());
216+
}),
217+
]),
285218
Fieldset::make(
286219
__(
287220
'custom-fields::custom-fields.field.form.settings'

0 commit comments

Comments
 (0)