Repository navigation
Fix item creation failing when a Yes/No field has a non boolean default value #1293
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
Open
Lainow
wants to merge
2
commits into
11.0/bugfixes
Choose a base branch
from
fix/yesno-default-value
base: 11.0/bugfixes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,6 +127,25 @@ public static function installBaseData(Migration $migration, $version) | |
| // change default_value from varchar to longtext | ||
| $migration->changeField($table, 'default_value', 'default_value', 'longtext'); | ||
|
|
||
| // Yes/No default values were typed as free text, normalize them to 0/1 | ||
| $yesno_fields = $DB->request([ | ||
| 'SELECT' => ['id', 'default_value'], | ||
| 'FROM' => $table, | ||
| 'WHERE' => [ | ||
| 'type' => 'yesno', | ||
| 'NOT' => ['default_value' => ['0', '1']], | ||
| ], | ||
| ]); | ||
| foreach ($yesno_fields as $yesno_field) { | ||
| $migration->addPostQuery( | ||
| $DB->buildUpdate( | ||
| $table, | ||
| ['default_value' => (string) self::normalizeYesNoValue($yesno_field['default_value'])], | ||
| ['id' => $yesno_field['id']], | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| $toolbox = new PluginFieldsToolbox(); | ||
| $toolbox->fixFieldsNames($migration, ['NOT' => ['type' => 'dropdown']]); | ||
|
|
||
|
|
@@ -288,6 +307,8 @@ public function prepareInputForAdd($input) | |
|
|
||
| if ($input['multiple'] ?? false) { | ||
| $input['default_value'] = json_encode($input['default_value'] ?: []); | ||
| } elseif (($input['type'] ?? '') === 'yesno' && array_key_exists('default_value', $input)) { | ||
| $input['default_value'] = (string) self::normalizeYesNoValue($input['default_value']); | ||
|
Comment on lines
+310
to
+311
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new Yes/No field now always stores |
||
| } | ||
|
|
||
| //reject adding when field name is too long for mysql | ||
|
|
@@ -368,6 +389,11 @@ public function prepareInputForUpdate($input) | |
| && $this->fields['multiple'] | ||
| ) { | ||
| $input['default_value'] = json_encode($input['default_value'] ?: []); | ||
| } elseif ( | ||
| array_key_exists('default_value', $input) | ||
| && $this->fields['type'] === 'yesno' | ||
| ) { | ||
| $input['default_value'] = (string) self::normalizeYesNoValue($input['default_value']); | ||
| } | ||
|
|
||
| return $input; | ||
|
|
@@ -744,6 +770,8 @@ public function showSummary($container) | |
| } else { | ||
| echo Dropdown::getDropdownName($table, $this->fields['default_value']); | ||
| } | ||
| } elseif ($this->fields['type'] === 'yesno') { | ||
| echo Dropdown::getYesNo(self::getDefaultValue($this->fields)); | ||
| } else { | ||
| echo htmlspecialchars((string) $this->fields['default_value']); | ||
| } | ||
|
|
@@ -1170,7 +1198,10 @@ public static function getDefaultValue(array $field) | |
| { | ||
| $value = null; | ||
|
|
||
| if (in_array($field['type'], ['dropdown', 'yesno']) && $field['default_value'] === '') { | ||
| if ($field['type'] === 'yesno') { | ||
| // Yes/No values are stored in an integer column, ignore any non boolean default value | ||
| $value = self::normalizeYesNoValue($field['default_value']); | ||
| } elseif ($field['type'] === 'dropdown' && $field['default_value'] === '') { | ||
| $value = 0; | ||
| } elseif ($field['default_value'] !== '') { | ||
| $value = $field['default_value']; | ||
|
|
@@ -1187,6 +1218,14 @@ public static function getDefaultValue(array $field) | |
| return $value; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize a Yes/No value to 0 or 1. | ||
| */ | ||
| public static function normalizeYesNoValue(mixed $value): int | ||
| { | ||
| return filter_var($value, FILTER_VALIDATE_BOOL) ? 1 : 0; | ||
| } | ||
|
|
||
| public static function prepareHtmlFields( | ||
| $fields, | ||
| $item, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for this migration (e.g.
Nonandyesrewritten to0/1)?