Skip to content

Commit e96f895

Browse files
committed
Width fieldtype: cast options to integers
1 parent 5468fab commit e96f895

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

resources/lang/en/fieldtypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
'link.config.collections' => 'Entries from these collections will be available. Leaving this empty will make entries from routable collections available.',
114114
'link.config.container' => 'Choose which asset container to use for this field.',
115115
'link.title' => 'Link',
116+
'list.config.cast_integers' => 'Cast numeric values to integers.',
116117
'list.title' => 'List',
117118
'markdown.config.automatic_line_breaks' => 'Enables automatic line breaks.',
118119
'markdown.config.automatic_links' => 'Enables automatic linking of any URLs.',

src/Fieldtypes/Lists.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class Lists extends Fieldtype
1313
protected function configFieldItems(): array
1414
{
1515
return [
16+
'cast_integers' => [
17+
'display' => __('Cast Integers'),
18+
'instructions' => __('statamic::fieldtypes.list.config.cast_integers'),
19+
'type' => 'toggle',
20+
'default' => false,
21+
],
1622
'default' => [
1723
'display' => __('Default Value'),
1824
'instructions' => __('statamic::messages.fields_default_instructions'),
@@ -36,9 +42,13 @@ public function process($data)
3642
return $data;
3743
}
3844

39-
return collect($data)->reject(function ($item) {
40-
return in_array($item, [null, ''], true);
41-
})->values()->all();
45+
return collect($data)
46+
->reject(fn ($value) => in_array($value, [null, ''], true))
47+
->when($this->config('cast_integers'), function ($collection) {
48+
return $collection->map(fn ($value) => (int) $value);
49+
})
50+
->values()
51+
->all();
4252
}
4353

4454
public function toGqlType()

src/Fieldtypes/Width.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ protected function configFieldItems(): array
2222
'instructions' => __('statamic::fieldtypes.width.config.options'),
2323
'type' => 'list',
2424
'default' => [25, 33, 50, 66, 75, 100],
25+
'cast_integers' => true,
2526
],
2627
'default' => [
2728
'display' => __('Default Value'),

0 commit comments

Comments
 (0)