function template_preprocess_form_element

Same name and namespace in other branches
  1. 10 core/includes/form.inc \template_preprocess_form_element()
  2. 9 core/includes/form.inc \template_preprocess_form_element()
  3. 8.9.x core/includes/form.inc \template_preprocess_form_element()

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:

  • before: The label is output before the element. This is the default. The label includes the #title and the required marker, if #required.
  • after: The label is output after the element. For example, this is used for radio and checkbox #type elements. If the #title is empty but the field is #required, the label will contain only the required marker.
  • invisible: Labels are critical for screen readers to enable them to properly navigate through forms but can be visually distracting. This property hides the label for everyone except screen readers.
  • attribute: Set the title attribute on the element to create a tooltip but output no label element. This is supported only for checkboxes and radios in \Drupal\Core\Render\Element\CompositeFormElementTrait::preRenderCompositeFormElement(). It is used where a visual label is not needed, such as a table of checkboxes where the row and column provide the context. The tooltip will include the title and required marker.

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.

To associate the label with a different field, set the #label_for property to the ID of the desired field.

Parameters

array $variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #title, #title_display, #description, #id, #required, #children, #type, #name, #label_for.

Deprecated

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

See also

https://www.drupal.org/node/3504125

File

core/includes/form.inc, line 392

Code

function template_preprocess_form_element(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(FormPreprocess::class)->preprocessFormElement($variables);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.