file form-element-label.html.twig

Default theme implementation for a form element label.

Available variables:

  • element: an input element.
  • title: The label's text.
  • title_display: Elements title_display setting.
  • description: element description.
  • required: An indicator for whether the associated form element is required.
  • is_checkbox: Whether the label is outputted in checkbox context.
  • is_radio: Whether the label is outputted in radio button context.
  • attributes: A list of HTML attributes for the label.

See Also

  • template_preprocess_form_element_label()
Source
  1. {#
  2. /**
  3. * @file
  4. * Default theme implementation for a form element label.
  5. *
  6. * Available variables:
  7. * - element: an input element.
  8. * - title: The label's text.
  9. * - title_display: Elements title_display setting.
  10. * - description: element description.
  11. * - required: An indicator for whether the associated form element is required.
  12. * - is_checkbox: Whether the label is outputted in checkbox context.
  13. * - is_radio: Whether the label is outputted in radio button context.
  14. * - attributes: A list of HTML attributes for the label.
  15. *
  16. * @ingroup templates
  17. *
  18. * @see template_preprocess_form_element_label()
  19. */
  20. #}
  21. {%-
  22. set classes = [
  23. 'control-label',
  24. title_display == 'after' ? 'option',
  25. title_display == 'invisible' and not (is_checkbox or is_radio) ? 'sr-only',
  26. required ? 'js-form-required',
  27. required ? 'form-required',
  28. ]
  29. -%}
  30. {% if title is not empty and title_display == 'invisible' and (is_checkbox or is_radio) -%}
  31. {#
  32. Clear but preserve label text as attribute (e.g. for screen readers) for
  33. checkboxes/radio buttons when it actually should be invisible.
  34. #}
  35. {%- set attributes = attributes.setAttribute('title', title) -%}
  36. {%- set title = null -%}
  37. {%- endif -%}
  38. {#
  39. Labels for single checkboxes/radios contain the element itself and thus have
  40. always to be rendered regardless of whether they have a title or not.
  41. #}
  42. {%- if title is not empty or is_checkbox or is_radio -%}
  43. <label{{ attributes.addClass(classes) }}>{{ element }}{{ title }}
  44. {%- if description -%}
  45. <p class="help-block">{{ description }}</p>
  46. {%- endif -%}
  47. </label>
  48. {%- endif -%}

Related topics