file form-element.html.twig

Theme override to display a form element.

Available variables:

  • attributes: HTML attributes for the containing element.
  • errors: (optional) Any errors for this form element, may not be set.
  • prefix: (optional) The form element prefix, may not be set.
  • suffix: (optional) The form element suffix, may not be set.
  • required: The required marker, or empty if the associated form element is not required.
  • type: The type of the element.
  • name: The name of the element.
  • label: A rendered label element.
  • label_display: Label display setting. It 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.
  • description: (optional) A list of description properties containing:
    • content: A description of the form element, may not be set.
    • attributes: (optional) A list of HTML attributes to apply to the description content wrapper. Will only be set when description is set.
  • description_display: Description display setting. It can have these values:
    • before: The description is output before the element.
    • after: The description is output after the element. This is the default value.
    • invisible: The description is output after the element, hidden visually but available to screen readers.
  • disabled: True if the element is disabled.
  • title_display: Title display setting.

File

templates/form/form-element.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Theme override to display a form element.
  5. *
  6. * Available variables:
  7. * - attributes: HTML attributes for the containing element.
  8. * - errors: (optional) Any errors for this form element, may not be set.
  9. * - prefix: (optional) The form element prefix, may not be set.
  10. * - suffix: (optional) The form element suffix, may not be set.
  11. * - required: The required marker, or empty if the associated form element is
  12. * not required.
  13. * - type: The type of the element.
  14. * - name: The name of the element.
  15. * - label: A rendered label element.
  16. * - label_display: Label display setting. It can have these values:
  17. * - before: The label is output before the element. This is the default.
  18. * The label includes the #title and the required marker, if #required.
  19. * - after: The label is output after the element. For example, this is used
  20. * for radio and checkbox #type elements. If the #title is empty but the
  21. * field is #required, the label will contain only the required marker.
  22. * - invisible: Labels are critical for screen readers to enable them to
  23. * properly navigate through forms but can be visually distracting. This
  24. * property hides the label for everyone except screen readers.
  25. * - attribute: Set the title attribute on the element to create a tooltip but
  26. * output no label element. This is supported only for checkboxes and radios
  27. * in \Drupal\Core\Render\Element\CompositeFormElementTrait::preRenderCompositeFormElement().
  28. * It is used where a visual label is not needed, such as a table of
  29. * checkboxes where the row and column provide the context. The tooltip will
  30. * include the title and required marker.
  31. * - description: (optional) A list of description properties containing:
  32. * - content: A description of the form element, may not be set.
  33. * - attributes: (optional) A list of HTML attributes to apply to the
  34. * description content wrapper. Will only be set when description is set.
  35. * - description_display: Description display setting. It can have these values:
  36. * - before: The description is output before the element.
  37. * - after: The description is output after the element. This is the default
  38. * value.
  39. * - invisible: The description is output after the element, hidden visually
  40. * but available to screen readers.
  41. * - disabled: True if the element is disabled.
  42. * - title_display: Title display setting.
  43. *
  44. * @see template_preprocess_form_element()
  45. * @see uikit_preprocess_form_element()
  46. *
  47. * @ingroup uikit_themeable
  48. */
  49. #}
  50. {%
  51. set classes = [
  52. 'js-form-item',
  53. 'form-item',
  54. 'js-form-type-' ~ type|clean_class,
  55. 'form-type-' ~ type|clean_class,
  56. 'js-form-item-' ~ name|clean_class,
  57. 'form-item-' ~ name|clean_class,
  58. 'uk-margin',
  59. ]
  60. %}
  61. {%
  62. set description_classes = [
  63. description_display == 'invisible' ? 'visually-hidden',
  64. 'uk-text-meta',
  65. 'uk-margin-small-top',
  66. ]
  67. %}
  68. <div{{ attributes.addClass(classes) }}>
  69. {% if label_display in ['before', 'invisible'] %}
  70. {{ label }}
  71. {% endif %}
  72. {% if description_display == 'before' and description.content %}
  73. <p{{ description.attributes.addClass(description_classes) }}>
  74. {{ description.content }}
  75. </p>
  76. {% endif %}
  77. <div class="uk-form-controls">
  78. {% if prefix is not empty %}
  79. <span class="field-prefix">{{ prefix }}</span>
  80. {% endif %}
  81. {{ children }}
  82. {% if suffix is not empty %}
  83. <span class="field-suffix">{{ suffix }}</span>
  84. {% endif %}
  85. </div>
  86. {% if label_display == 'after' %}
  87. {{ label }}
  88. {% endif %}
  89. {% if errors %}
  90. <div class="uk-alert uk-alert-danger">
  91. {{ errors }}
  92. </div>
  93. {% endif %}
  94. {% if description_display in ['after', 'invisible'] and description.content %}
  95. <p{{ description.attributes.addClass(description_classes) }}>
  96. {{ description.content }}
  97. </p>
  98. {% endif %}
  99. </div>

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Log in