Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cast_booleans configuration option to Radio fieldtype #2601

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/lang/en/fieldtypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
'markdown.config.parser' => 'The name of a customized Markdown parser. Leave blank for default.',
'markdown.config.restrict' => 'Prevent users from navigating to other folders.',
'markdown.config.smartypants' => 'Automatically convert straight quotes into curly quotes, dashes into en/em-dashes, and other similar text transformations.',
'radio.config.cast_booleans' => 'Options with values of true and false will be saved as booleans.',
duncanmcclean marked this conversation as resolved.
Show resolved Hide resolved
'radio.config.inline' => 'Show the radio buttons in a row.',
'radio.config.options' => 'Set the array keys and their optional labels.',
'range.config.append' => 'Add text to the end (right-side) of the slider.',
Expand Down
33 changes: 33 additions & 0 deletions src/Fieldtypes/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ protected function configFieldItems(): array
'type' => 'toggle',
'width' => 50,
],
'cast_booleans' => [
'display' => __('Cast Booleans'),
'instructions' => __('statamic::fieldtypes.radio.config.cast_booleans'),
'type' => 'toggle',
'default' => false,
'width' => 50,
],
];
}

Expand All @@ -31,4 +38,30 @@ public function augment($value)

return new LabeledValue($value, $label);
}

public function preProcess($value)
{
if ($this->config('cast_booleans')) {
if ($value === true) {
return 'true';
} elseif ($value === false) {
return 'false';
}
}

return $value;
}

public function process($value)
{
if ($this->config('cast_booleans')) {
if ($value === 'true') {
return true;
} elseif ($value === 'false') {
return false;
}
}

return $value;
}
}