template_preprocess_form_element_label(&$variables)
Prepares variables for form label templates.
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 before or after elements, depending on form-element.html.twig and #title_display.
This function will not be called for elements with no labels, depending on #title_display. For elements that have an empty #title and are not required, this function will output no label (''). For required elements that have an empty #title, this will output the required marker alone within the label. The label will use the #id to associate the marker with the field that is required. That is especially important for screenreader users to know which field is required.
array $variables: An associative array containing:
function template_preprocess_form_element_label(&$variables) { $element = $variables['element']; // If title and required marker are both empty, output no label. if (isset($element['#title']) && $element['#title'] !== '') { $variables['title'] = ['#markup' => $element['#title']]; } $variables['attributes'] = array(); // Pass elements title_display to template. $variables['title_display'] = $element['#title_display']; // A #for property of a dedicated #type 'label' element as precedence. if (!empty($element['#for'])) { $variables['attributes']['for'] = $element['#for']; // A custom #id allows the referenced form input element to refer back to // the label element; e.g., in the 'aria-labelledby' attribute. if (!empty($element['#id'])) { $variables['attributes']['id'] = $element['#id']; } } // Otherwise, point to the #id of the form input element. elseif (!empty($element['#id'])) { $variables['attributes']['for'] = $element['#id']; } // Pass elements required to template. $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!form.inc/function/template_preprocess_form_element_label/8.1.x