diff --git a/core/includes/form.inc b/core/includes/form.inc index e58b2f5..9c4e5ff 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -4714,7 +4714,9 @@ function template_preprocess_form_required_marker(&$variables) { } /** - * Returns HTML for a form element label and required marker. + * Prepares variables for form element label templates. + * + * Default template: form-element-label.html.twig. * * Form element labels include the #title and a #required marker. The label is * associated with the element itself by the element #id. Labels may appear @@ -4733,42 +4735,40 @@ function template_preprocess_form_required_marker(&$variables) { * An associative array containing: * - element: An associative array containing the properties of the element. * Properties used: #required, #title, #id, #value, #description. - * - * @ingroup themeable */ -function theme_form_element_label($variables) { +function template_preprocess_form_element_label(&$variables) { $element = $variables['element']; // This is also used in the installer, pre-database setup. $t = get_t(); - // If title and required marker are both empty, output no label. - if ((!isset($element['#title']) || $element['#title'] === '') && empty($element['#required'])) { - return ''; + // If title and required marker are both empty, prepare no label. + if (empty($element['#required']) && (!isset($element['#title']) || $element['#title'] === '')) { + $variables['label'] = FALSE; + $variables['required'] = FALSE; + return; } - + else { // If the element is required, a required marker is appended to the label. - $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : ''; - - $title = filter_xss_admin($element['#title']); - - $attributes = array(); - // Style the label as class option to display inline with the element. - if (!empty($element['#title_display'])) { - if ($element['#title_display']== 'after') { - $attributes['class'] = 'option'; - } - // Show label only to screen readers to avoid disruption in visual flows. - elseif ($element['#title_display'] == 'invisible') { - $attributes['class'] = 'element-invisible'; + $variables['label'] = (isset($element['#title'])) ? $t(filter_xss_admin($element['#title'])) : NULL; + $variables['required'] = (!empty($element['#required'])) ? array('#theme' => 'form_required_marker', '#element' => $element) : ''; + $variables['attributes'] = new Attribute(array('class' => array())); + // Associate the label with the field it is for. + if (!empty($element['#id'])) { + $variables['attributes']['for'] = $element['#id']; + } + // Add label positioning classes. + if (!empty($element['#title_display'])) { + // Style the label as class 'option' to display inline with the element. + // @todo: Use a more semantic class name like 'layout-inline-after'? + if ($element['#title_display'] == 'after') { + $variables['attributes']['class'][] = 'option'; + } + // Show label only to screen readers to avoid disruption in visual flows. + elseif ($element['#title_display'] == 'invisible') { + $variables['attributes']['class'][] = 'element-invisible'; + } } } - - - if (!empty($element['#id'])) { - $attributes['for'] = $element['#id']; - } - - return '' . $t('!title!required', array('!title' => $title, '!required' => $required)) . ''; } /** diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f9d345b..b0ff84f 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -3314,6 +3314,7 @@ function drupal_common_theme() { ), 'form_element_label' => array( 'render element' => 'element', + 'template' => 'form-element-label', ), 'vertical_tabs' => array( 'render element' => 'element', diff --git a/core/modules/system/templates/form-element-label.html.twig b/core/modules/system/templates/form-element-label.html.twig new file mode 100644 index 0000000..9acee29 --- /dev/null +++ b/core/modules/system/templates/form-element-label.html.twig @@ -0,0 +1,29 @@ +{# +/** + * @file + * Default theme implementation for a form element label. + * + * Available variables: + * - label: The text of the label, or FALSE if there is no title and the element + * is not required. + * - required: A rendered marker indicating the field is required, or + * an empty string if the field is not required. + * - attributes: An array of HTML attributes to apply to the label. + * + * @see template_preprocess() + * @see template_preprocess_form_label() + * @see form-required-marker.html.twig + * + * @ingroup themeable + */ +#} +{% if (label is not empty) or (required is not empty) %} + + {%- if label is not empty -%} + {{ label }} + {%- endif -%} + {%- if required is not empty -%} + {{ required }} + {%- endif -%} + +{% endif %}