|
14 | 14 | use Filament\Forms\Components\Toggle; |
15 | 15 | use Filament\Schemas\Components\Component; |
16 | 16 | use Filament\Schemas\Components\Fieldset; |
| 17 | +use Filament\Schemas\Components\Grid; |
17 | 18 | use Filament\Schemas\Components\Tabs; |
18 | 19 | use Filament\Schemas\Components\Tabs\Tab; |
19 | 20 | use Filament\Schemas\Components\Utilities\Get; |
@@ -131,157 +132,89 @@ public static function schema(bool $withOptionsRelationship = true): array |
131 | 132 | Tab::make( |
132 | 133 | __('custom-fields::custom-fields.field.form.general') |
133 | 134 | )->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') |
142 | 136 | ->default( |
143 | 137 | fn () => request( |
144 | 138 | 'entityType', |
145 | 139 | (Entities::withCustomFields()->first()?->getAlias()) ?? '' |
146 | 140 | ) |
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 | + ) |
209 | 181 | ) |
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 ??= ''; |
219 | 185 |
|
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 | + } |
226 | 189 |
|
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 | + ) |
274 | 213 | ) |
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 | + ]), |
285 | 218 | Fieldset::make( |
286 | 219 | __( |
287 | 220 | 'custom-fields::custom-fields.field.form.settings' |
|
0 commit comments