template_preprocess_form_element(&$variables)
Returns HTML for a form element. Prepares variables for form element templates.
Default template: form-element.html.twig.
In addition to the element itself, the DIV contains a label for the element based on the optional #title_display property, and an optional #description.
The optional #title_display property can have these values:
If the #title property is not set, then the label and any required marker will not be output, regardless of the #title_display or #required values. This can be useful in cases such as the password_confirm element, which creates children elements that have their own labels and required markers, but the parent element should have neither. Use this carefully because a field without an associated label can cause accessibility challenges.
array $variables: An associative array containing:
function template_preprocess_form_element(&$variables) { $element = &$variables['element']; // This function is invoked as theme wrapper, but the rendered form element // may not necessarily have been processed by // \Drupal::formBuilder()->doBuildForm(). $element += array( '#title_display' => 'before', '#wrapper_attributes' => array(), ); $variables['attributes'] = $element['#wrapper_attributes']; // Add element #id for #type 'item'. if (isset($element['#markup']) && !empty($element['#id'])) { $variables['attributes']['id'] = $element['#id']; } // Pass elements #type and #name to template. if (!empty($element['#type'])) { $variables['type'] = $element['#type']; } if (!empty($element['#name'])) { $variables['name'] = $element['#name']; } // Pass elements disabled status to template. $variables['disabled'] = !empty($element['#attributes']['disabled']) ? $element['#attributes']['disabled'] : NULL; // Suppress error messages. $variables['errors'] = NULL; // If #title is not set, we don't display any label. if (!isset($element['#title'])) { $element['#title_display'] = 'none'; } $variables['title_display'] = $element['#title_display']; $variables['prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL; $variables['suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL; $variables['description'] = NULL; if (!empty($element['#description'])) { $variables['description_display'] = $element['#description_display']; $description_attributes = []; if (!empty($element['#id'])) { $description_attributes['id'] = $element['#id'] . '--description'; } $variables['description']['attributes'] = new Attribute($description_attributes); $variables['description']['content'] = $element['#description']; } // Add label_display and label variables to template. $variables['label_display'] = $element['#title_display']; $variables['label'] = array('#theme' => 'form_element_label'); $variables['label'] += array_intersect_key($element, array_flip(array('#id', '#required', '#title', '#title_display'))); $variables['children'] = $element['#children']; }
© 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/8.1.x