Active
Project:
Drupal core
Version:
main
Component:
documentation
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
18 May 2023 at 11:11 UTC
Updated:
18 May 2023 at 11:18 UTC
Jump to comment: Most recent
The documentation for Select contains the following property description. (Emphasis is mine.)
- If #required is TRUE, this defaults to '' (an empty string).
- If #required is not TRUE and this value isn't set, then no extra option is added to the select control, leaving the control in a slightly illogical state, because there's no way for the user to select nothing, since all user agents automatically preselect the first available option. But people are used to this being the behavior of select controls. @todo Address the above issue in Drupal 8.
- If #required is not TRUE and this value is set (most commonly to an empty string), then an extra option (see #empty_option above) representing a "non-selection" is added with this as its value.
The code that handles that property is in Select::processSelect(), but it does not handle the case reported in the second point.
// If the element is set to #required through #states, override the
// element's #required setting.
$required = isset($element['#states']['required']) ? TRUE : $element['#required'];
// If the element is required and there is no #default_value, then add an
// empty option that will fail validation, so that the user is required to
// make a choice. Also, if there's a value for #empty_value or
// #empty_option, then add an option that represents emptiness.
if ($required && !isset($element['#default_value']) || isset($element['#empty_value']) || isset($element['#empty_option'])) {
$element += [
'#empty_value' => '',
'#empty_option' => $required ? t('- Select -') : t('- None -'),
];
// The empty option is prepended to #options and purposively not merged
// to prevent another option in #options mistakenly using the same value
// as #empty_value.
$empty_option = [
$element['#empty_value'] => $element['#empty_option'],
];
$element['#options'] = $empty_option + $element['#options'];
}
Either the code is changed as that @todo says, the @todo is removed, or the @todo sentence is changed to still say that needs to be better handled, but without any reference to Drupal 8.
(I selected documentation as component, since this is a task to change a documentation comment.)
Comments
Comment #2
avpadernoComment #3
avpadernoGiven what the third point says,
Select::processSelect()could also set#empty_valueto an empty string, when#requiredis notTRUEand#empty_valuehas not been set.