diff --git a/core/includes/form.inc b/core/includes/form.inc index 8170820..8452fe7 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -43,7 +43,7 @@ function template_preprocess_select(&$variables) { } /** - * Converts an array of options into HTML, for use in select list form elements. + * Converts an options form element into a structured array for output. * * This function calls itself recursively to obtain the values for each optgroup * within the list of options and when the function encounters an object with @@ -78,13 +78,16 @@ function template_preprocess_select(&$variables) { * $element['#options'] above, or NULL. This parameter is only used internally * and is not intended to be passed in to the initial function call. * - * @return string - * An HTML string of options and optgroups for use in a select form element. + * @return array + * A structured array of options and optgroups for use in a select form + * element. + * + * @todo Document the format of this array. */ function form_select_options($element, $choices = NULL) { if (!isset($choices)) { if (empty($element['#options'])) { - return ''; + return []; } $choices = $element['#options']; } @@ -94,29 +97,35 @@ function form_select_options($element, $choices = NULL) { $value_is_array = $value_valid && is_array($element['#value']); // Check if the element is multiple select and no value has been selected. $empty_value = (empty($element['#value']) && !empty($element['#multiple'])); - $options = ''; + $options = []; foreach ($choices as $key => $choice) { if (is_array($choice)) { - $options .= ''; - $options .= form_select_options($element, $choice); - $options .= ''; + $options[] = [ + 'type' => 'optgroup', + 'label' => $key, + 'options' => form_select_options($element, $choice), + ]; } elseif (is_object($choice) && isset($choice->option)) { - $options .= form_select_options($element, $choice->option); + $options = form_select_options($element, $choice->option); } else { + $option = []; $key = (string) $key; $empty_choice = $empty_value && $key == '_none'; if ($value_valid && ((!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value']))) || $empty_choice)) { - $selected = ' selected="selected"'; + $option['selected'] = TRUE; } else { - $selected = ''; + $option['selected'] = FALSE; } - $options .= ''; + $option['type'] = 'option'; + $option['value'] = $key; + $option['label'] = $choice; + $options[] = $option; } } - return SafeMarkup::set($options); + return $options; } /** diff --git a/core/modules/system/templates/select.html.twig b/core/modules/system/templates/select.html.twig index 21f32ac..fb7226e 100644 --- a/core/modules/system/templates/select.html.twig +++ b/core/modules/system/templates/select.html.twig @@ -12,4 +12,18 @@ * @ingroup themeable */ #} -{{ options }} +{% spaceless %} + + {% for option in options %} + {% if option.type == 'optgroup' %} + + {% for sub_option in option.options %} + + {% endfor %} + + {% elseif option.type == 'option' %} + + {% endif %} + {% endfor %} + +{% endspaceless %} diff --git a/core/themes/classy/templates/form/select.html.twig b/core/themes/classy/templates/form/select.html.twig deleted file mode 100644 index 51baa7a..0000000 --- a/core/themes/classy/templates/form/select.html.twig +++ /dev/null @@ -1,13 +0,0 @@ -{# -/** - * @file - * Theme override for a select element. - * - * Available variables: - * - attributes: HTML attributes for the select tag. - * - options: The option element children. - * - * @see template_preprocess_select() - */ -#} -{{ options }}