You are here

form-element.html.twig in Open Social 8.7

Default theme implementation for a form element.

Available variables:

  • attributes: HTML attributes for the containing element.
  • 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

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