From e96f89502141fd5f690ec2dcf5b64a6338ea202d Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 18 Jul 2025 17:53:38 +0100 Subject: [PATCH 1/2] Width fieldtype: cast options to integers --- resources/lang/en/fieldtypes.php | 1 + src/Fieldtypes/Lists.php | 16 +++++++++++++--- src/Fieldtypes/Width.php | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/resources/lang/en/fieldtypes.php b/resources/lang/en/fieldtypes.php index 8e24f7e3bb5..e6765e1358c 100644 --- a/resources/lang/en/fieldtypes.php +++ b/resources/lang/en/fieldtypes.php @@ -113,6 +113,7 @@ 'link.config.collections' => 'Entries from these collections will be available. Leaving this empty will make entries from routable collections available.', 'link.config.container' => 'Choose which asset container to use for this field.', 'link.title' => 'Link', + 'list.config.cast_integers' => 'Cast numeric values to integers.', 'list.title' => 'List', 'markdown.config.automatic_line_breaks' => 'Enables automatic line breaks.', 'markdown.config.automatic_links' => 'Enables automatic linking of any URLs.', diff --git a/src/Fieldtypes/Lists.php b/src/Fieldtypes/Lists.php index e42dba1433a..db1cbbf738d 100644 --- a/src/Fieldtypes/Lists.php +++ b/src/Fieldtypes/Lists.php @@ -13,6 +13,12 @@ class Lists extends Fieldtype protected function configFieldItems(): array { return [ + 'cast_integers' => [ + 'display' => __('Cast Integers'), + 'instructions' => __('statamic::fieldtypes.list.config.cast_integers'), + 'type' => 'toggle', + 'default' => false, + ], 'default' => [ 'display' => __('Default Value'), 'instructions' => __('statamic::messages.fields_default_instructions'), @@ -36,9 +42,13 @@ public function process($data) return $data; } - return collect($data)->reject(function ($item) { - return in_array($item, [null, ''], true); - })->values()->all(); + return collect($data) + ->reject(fn ($value) => in_array($value, [null, ''], true)) + ->when($this->config('cast_integers'), function ($collection) { + return $collection->map(fn ($value) => (int) $value); + }) + ->values() + ->all(); } public function toGqlType() diff --git a/src/Fieldtypes/Width.php b/src/Fieldtypes/Width.php index 967247555e2..c70e44f646d 100644 --- a/src/Fieldtypes/Width.php +++ b/src/Fieldtypes/Width.php @@ -22,6 +22,7 @@ protected function configFieldItems(): array 'instructions' => __('statamic::fieldtypes.width.config.options'), 'type' => 'list', 'default' => [25, 33, 50, 66, 75, 100], + 'cast_integers' => true, ], 'default' => [ 'display' => __('Default Value'), From e63007e2a71175e73d2589c258b78a67a00fa161 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 7 Aug 2025 15:13:16 -0400 Subject: [PATCH 2/2] just cast numbers. dont need a config for it. --- resources/lang/en/fieldtypes.php | 1 - src/Fieldtypes/Lists.php | 20 +++++++------------- src/Fieldtypes/Width.php | 1 - tests/Fieldtypes/ListTest.php | 27 +++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 tests/Fieldtypes/ListTest.php diff --git a/resources/lang/en/fieldtypes.php b/resources/lang/en/fieldtypes.php index e6765e1358c..8e24f7e3bb5 100644 --- a/resources/lang/en/fieldtypes.php +++ b/resources/lang/en/fieldtypes.php @@ -113,7 +113,6 @@ 'link.config.collections' => 'Entries from these collections will be available. Leaving this empty will make entries from routable collections available.', 'link.config.container' => 'Choose which asset container to use for this field.', 'link.title' => 'Link', - 'list.config.cast_integers' => 'Cast numeric values to integers.', 'list.title' => 'List', 'markdown.config.automatic_line_breaks' => 'Enables automatic line breaks.', 'markdown.config.automatic_links' => 'Enables automatic linking of any URLs.', diff --git a/src/Fieldtypes/Lists.php b/src/Fieldtypes/Lists.php index db1cbbf738d..9fac3224547 100644 --- a/src/Fieldtypes/Lists.php +++ b/src/Fieldtypes/Lists.php @@ -13,12 +13,6 @@ class Lists extends Fieldtype protected function configFieldItems(): array { return [ - 'cast_integers' => [ - 'display' => __('Cast Integers'), - 'instructions' => __('statamic::fieldtypes.list.config.cast_integers'), - 'type' => 'toggle', - 'default' => false, - ], 'default' => [ 'display' => __('Default Value'), 'instructions' => __('statamic::messages.fields_default_instructions'), @@ -42,13 +36,13 @@ public function process($data) return $data; } - return collect($data) - ->reject(fn ($value) => in_array($value, [null, ''], true)) - ->when($this->config('cast_integers'), function ($collection) { - return $collection->map(fn ($value) => (int) $value); - }) - ->values() - ->all(); + return collect($data)->reject(function ($item) { + return in_array($item, [null, ''], true); + })->map(function ($item) { + return is_numeric($item) + ? (str_contains($item, '.') ? (float) $item : (int) $item) + : $item; + })->values()->all(); } public function toGqlType() diff --git a/src/Fieldtypes/Width.php b/src/Fieldtypes/Width.php index c70e44f646d..967247555e2 100644 --- a/src/Fieldtypes/Width.php +++ b/src/Fieldtypes/Width.php @@ -22,7 +22,6 @@ protected function configFieldItems(): array 'instructions' => __('statamic::fieldtypes.width.config.options'), 'type' => 'list', 'default' => [25, 33, 50, 66, 75, 100], - 'cast_integers' => true, ], 'default' => [ 'display' => __('Default Value'), diff --git a/tests/Fieldtypes/ListTest.php b/tests/Fieldtypes/ListTest.php new file mode 100644 index 00000000000..e09d9a6e829 --- /dev/null +++ b/tests/Fieldtypes/ListTest.php @@ -0,0 +1,27 @@ +assertSame( + ['a', 2, 3, '4 and a half', 5.7, 8.3], + $this->field()->process(['a', '2', 3, '4 and a half', '5.7', 8.3]) + ); + } + + private function field($config = []) + { + $ft = new Lists; + + return $ft->setField(new Field('test', array_merge($config, ['type' => $ft->handle()]))); + } +}