@@ -42,22 +42,57 @@ class FieldForm implements FormInterface
4242 public static function schema (bool $ withOptionsRelationship = true ): array
4343 {
4444 $ optionsRepeater = Repeater::make ('options ' )
45- ->table ([
46- TableColumn::make ('Color ' )->width ('150px ' )->hiddenHeaderLabel (),
47- TableColumn::make ('Name ' )->hiddenHeaderLabel (),
48- ])
49- ->schema ([
50- ColorPicker::make ('settings.color ' )
51- ->columnSpan (3 )
52- ->hexColor ()
53- ->visible (
54- fn (
55- Get $ get
56- ): bool => FeatureManager::isEnabled (CustomFieldsFeature::FIELD_OPTION_COLORS ) &&
57- $ get ('../../settings.enable_option_colors ' )
58- ),
59- TextInput::make ('name ' )->required ()->columnSpan (9 )->distinct (),
60- ])
45+ ->table (function (Get $ get ): array {
46+ $ hasColors = FeatureManager::isEnabled (CustomFieldsFeature::FIELD_OPTION_COLORS ) &&
47+ $ get ('settings.enable_option_colors ' );
48+
49+ $ columns = [];
50+
51+ if ($ hasColors ) {
52+ $ columns [] = TableColumn::make ('Color ' )->width ('100px ' );
53+ }
54+
55+ if (! $ hasColors ) {
56+ $ columns [] = TableColumn::make ('Value ' )->width ('150px ' );
57+ }
58+
59+ $ columns [] = TableColumn::make ('Name ' );
60+
61+ return $ columns ;
62+ })
63+ ->schema (function (Get $ get ): array {
64+ $ hasColors = FeatureManager::isEnabled (CustomFieldsFeature::FIELD_OPTION_COLORS ) &&
65+ $ get ('settings.enable_option_colors ' );
66+
67+ $ fields = [];
68+
69+ if ($ hasColors ) {
70+ $ fields [] = ColorPicker::make ('settings.color ' )
71+ ->hexColor ()
72+ ->label ('Color ' )
73+ ->columnSpan (2 )
74+ ;
75+ } else {
76+ $ fields [] = TextInput::make ('value ' )
77+ ->label ('Value ' )
78+ ->placeholder ('M ' )
79+ ->distinct ()
80+ ->disabled (fn (Get $ get ): bool => (bool ) $ get ('../../uses_entity_column ' ))
81+ ->columnSpan (3 )
82+ ;
83+ }
84+
85+ $ fields [] = TextInput::make ('name ' )
86+ ->required ()
87+ ->label ('Name ' )
88+ ->placeholder ('Male ' )
89+ ->distinct ()
90+ ->disabled (fn (Get $ get ): bool => (bool ) $ get ('../../uses_entity_column ' ))
91+ ->columnSpan ($ hasColors ? 10 : 9 )
92+ ;
93+
94+ return $ fields ;
95+ })
6196 ->columns (12 )
6297 ->columnSpanFull ()
6398 ->requiredUnless ('type ' , function (callable $ get ) {
@@ -81,6 +116,7 @@ public static function schema(bool $withOptionsRelationship = true): array
81116 && CustomFieldsType::getFieldType ($ get ('type ' ))->dataType ->isChoiceField ()
82117 && ! CustomFieldsType::getFieldType ($ get ('type ' ))->withoutUserOptions
83118 )
119+ ->disabled (fn (Get $ get ): bool => (bool ) $ get ('uses_entity_column ' ))
84120 ->mutateRelationshipDataBeforeCreateUsing (function (
85121 array $ data
86122 ): array {
@@ -89,7 +125,8 @@ public static function schema(bool $withOptionsRelationship = true): array
89125 }
90126
91127 return $ data ;
92- });
128+ })
129+ ;
93130
94131 if ($ withOptionsRelationship ) {
95132 $ optionsRepeater = $ optionsRepeater ->relationship ();
@@ -156,11 +193,6 @@ public static function schema(bool $withOptionsRelationship = true): array
156193 ->live (onBlur: true )
157194 ->required ()
158195 ->maxLength (50 )
159- ->disabled (
160- fn (
161- ?CustomField $ record
162- ): bool => (bool ) $ record ?->system_defined
163- )
164196 ->unique (
165197 table: CustomFields::customFieldModel (),
166198 column: 'name ' ,
@@ -257,64 +289,79 @@ public static function schema(bool $withOptionsRelationship = true): array
257289 ->columnSpanFull ()
258290 ->columns (2 )
259291 ->schema ([
260- // Visibility settings
261- Toggle::make ('settings.visible_in_list ' )
292+ // Storage settings
293+ Toggle::make ('uses_entity_column ' )
262294 ->inline (false )
263295 ->live ()
296+ ->label ('Store in Entity Column ' )
297+ ->helperText ('When enabled, this field will store its value directly in a column on the entity model instead of the custom_field_values table. The column name must match the field code. ' )
298+ ->visible (
299+ fn (Get $ get ): bool => $ get ('type ' ) !== null
300+ )
301+ ->disabled (
302+ fn (
303+ ?CustomField $ record
304+ ): bool => (bool ) $ record ?->exists
305+ )
306+ ->default (false ),
307+ Toggle::make ('settings.list_toggleable_hidden ' )
308+ ->inline (false )
264309 ->label (
265310 __ (
266- 'custom-fields::custom-fields.field.form.visible_in_list '
311+ 'custom-fields::custom-fields.field.form.list_toggleable_hidden '
312+ )
313+ )
314+ ->helperText (
315+ __ (
316+ 'custom-fields::custom-fields.field.form.list_toggleable_hidden_hint '
267317 )
268318 )
319+ ->visible (
320+ fn (Get $ get ): bool => $ get (
321+ 'settings.visible_in_list '
322+ ) &&
323+ FeatureManager::isEnabled (CustomFieldsFeature::UI_TOGGLEABLE_COLUMNS )
324+ )
269325 ->afterStateHydrated (function (
270326 Toggle $ component ,
271327 ?Model $ record
272328 ): void {
273- if (is_null ($ record )) {
274- $ component ->state (true );
329+ if ($ record === null ) {
330+ $ component ->state (
331+ FeatureManager::isEnabled (CustomFieldsFeature::UI_TOGGLEABLE_COLUMNS_HIDDEN_DEFAULT )
332+ );
275333 }
276334 }),
277- Toggle::make ('settings.visible_in_view ' )
335+ // Visibility settings
336+ Toggle::make ('settings.visible_in_list ' )
278337 ->inline (false )
338+ ->live ()
279339 ->label (
280340 __ (
281- 'custom-fields::custom-fields.field.form.visible_in_view '
341+ 'custom-fields::custom-fields.field.form.visible_in_list '
282342 )
283343 )
284344 ->afterStateHydrated (function (
285345 Toggle $ component ,
286346 ?Model $ record
287347 ): void {
288- if (is_null ( $ record) ) {
348+ if ($ record === null ) {
289349 $ component ->state (true );
290350 }
291351 }),
292- Toggle::make ('settings.list_toggleable_hidden ' )
352+ Toggle::make ('settings.visible_in_view ' )
293353 ->inline (false )
294354 ->label (
295355 __ (
296- 'custom-fields::custom-fields.field.form.list_toggleable_hidden '
297- )
298- )
299- ->helperText (
300- __ (
301- 'custom-fields::custom-fields.field.form.list_toggleable_hidden_hint '
356+ 'custom-fields::custom-fields.field.form.visible_in_view '
302357 )
303358 )
304- ->visible (
305- fn (Get $ get ): bool => $ get (
306- 'settings.visible_in_list '
307- ) &&
308- FeatureManager::isEnabled (CustomFieldsFeature::UI_TOGGLEABLE_COLUMNS )
309- )
310359 ->afterStateHydrated (function (
311360 Toggle $ component ,
312361 ?Model $ record
313362 ): void {
314- if (is_null ($ record )) {
315- $ component ->state (
316- FeatureManager::isEnabled (CustomFieldsFeature::UI_TOGGLEABLE_COLUMNS_HIDDEN_DEFAULT )
317- );
363+ if ($ record === null ) {
364+ $ component ->state (true );
318365 }
319366 }),
320367 // Data settings
@@ -339,7 +386,7 @@ public static function schema(bool $withOptionsRelationship = true): array
339386 Toggle $ component ,
340387 mixed $ state
341388 ): void {
342- if (is_null ( $ state) ) {
389+ if ($ state === null ) {
343390 $ component ->state (false );
344391 }
345392 }),
@@ -381,7 +428,8 @@ public static function schema(bool $withOptionsRelationship = true): array
381428 ->visible (
382429 fn (
383430 Get $ get
384- ): bool => FeatureManager::isEnabled (CustomFieldsFeature::FIELD_OPTION_COLORS ) &&
431+ ): bool => ! $ get ('uses_entity_column ' ) &&
432+ FeatureManager::isEnabled (CustomFieldsFeature::FIELD_OPTION_COLORS ) &&
385433 in_array ((string ) $ get ('type ' ), [
386434 'select ' ,
387435 'multi_select ' ,
@@ -410,7 +458,7 @@ public static function schema(bool $withOptionsRelationship = true): array
410458 'options ' => __ (
411459 'custom-fields::custom-fields.field.form.options_lookup_type.options '
412460 ),
413- 'lookup ' => __ (
461+ 'lookup ' => __ (
414462 'custom-fields::custom-fields.field.form.options_lookup_type.lookup '
415463 ),
416464 ])
0 commit comments