form-element.html.twig 3.69 KB
Newer Older
1 2 3 4 5 6 7
{#
/**
 * @file
 * Default theme implementation for a form element.
 *
 * Available variables:
 * - attributes: HTML attributes for the containing element.
8
 * - errors: (optional) Any errors for this form element, may not be set.
9 10 11 12
 * - 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.
13 14
 * - type: The type of the element.
 * - name: The name of the element.
15 16 17 18 19
 * - 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
20 21
 *     for radio and checkbox #type elements. If the #title is empty but the
 *     field is #required, the label will contain only the required marker.
22 23 24 25 26
 *   - 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
27 28 29 30
 *     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.
31 32 33 34
 * - 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.
35 36 37 38 39 40
 * - 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.
41 42
 * - disabled: True if the element is disabled.
 * - title_display: Title display setting.
43 44 45 46 47 48
 *
 * @see template_preprocess_form_element()
 *
 * @ingroup themeable
 */
#}
49 50 51
{%
  set classes = [
    'form-item',
52
    'js-form-type-' ~ type|clean_class,
53 54 55
    'form-item-' ~ name|clean_class,
    title_display not in ['after', 'before'] ? 'form-no-label',
    disabled == 'disabled' ? 'form-disabled',
56
    errors ? 'form-item--error',
57 58 59 60 61 62 63 64 65
  ]
%}
{%
  set description_classes = [
    'description',
    description_display == 'invisible' ? 'visually-hidden',
  ]
%}
<div{{ attributes.addClass(classes) }}>
66 67 68 69 70 71
  {% if label_display in ['before', 'invisible'] %}
    {{ label }}
  {% endif %}
  {% if prefix is not empty %}
    <span class="field-prefix">{{ prefix }}</span>
  {% endif %}
72 73 74 75 76
  {% if description_display == 'before' and description.content %}
    <div{{ description.attributes }}>
      {{ description.content }}
    </div>
  {% endif %}
77 78 79 80 81 82 83
  {{ children }}
  {% if suffix is not empty %}
    <span class="field-suffix">{{ suffix }}</span>
  {% endif %}
  {% if label_display == 'after' %}
    {{ label }}
  {% endif %}
84
  {% if errors %}
85
    <div class="form-item--error-message">
86 87 88
      {{ errors }}
    </div>
  {% endif %}
89
  {% if description_display in ['after', 'invisible'] and description.content %}
90
    <div{{ description.attributes.addClass(description_classes) }}>
91 92 93 94
      {{ description.content }}
    </div>
  {% endif %}
</div>