You are here

field.html.twig in Field Label 8

Theme override for a field based on the @stable field template. See that file for full documentation.

field_label supplies one additional variable:

  • label_tag: The label wrapper tag as chosen in the field_label.

This template won't used if your theme overrides field.html.twig. Modify your own template to use the label_tag, e.g.:

<{{ label_tag|default('div') }}> {{ label }} </{{ label_tag|default('div') }}>

File

templates/field.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Theme override for a field based on the @stable field template. See that
  5. * file for full documentation.
  6. *
  7. * field_label supplies one additional variable:
  8. * - label_tag: The label wrapper tag as chosen in the field_label.
  9. *
  10. * This template won't used if your theme overrides field.html.twig. Modify
  11. * your own template to use the label_tag, e.g.:
  12. *
  13. * <{{ label_tag|default('div') }}>
  14. * {{ label }}
  15. * </{{ label_tag|default('div') }}>
  16. *
  17. * @see field_label_preprocess_field()
  18. */
  19. #}
  20. {%
  21. set classes = [
  22. 'field',
  23. 'field--name-' ~ field_name|clean_class,
  24. 'field--type-' ~ field_type|clean_class,
  25. 'field--label-' ~ label_display,
  26. ]
  27. %}
  28. {%
  29. set title_classes = [
  30. 'field__label',
  31. label_display == 'visually_hidden' ? 'visually-hidden',
  32. ]
  33. %}
  34. {% if label_hidden %}
  35. {% if multiple %}
  36. <div{{ attributes.addClass(classes, 'field__items') }}>
  37. {% for item in items %}
  38. <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
  39. {% endfor %}
  40. </div>
  41. {% else %}
  42. {% for item in items %}
  43. <div{{ attributes.addClass(classes, 'field__item') }}>{{ item.content }}</div>
  44. {% endfor %}
  45. {% endif %}
  46. {% else %}
  47. <div{{ attributes.addClass(classes) }}>
  48. <{{ label_tag|default('div') }}{{ title_attributes.addClass(title_classes) }}>{{ label }}</{{ label_tag|default('div') }}>
  49. {% if multiple %}
  50. <div class="field__items">
  51. {% endif %}
  52. {% for item in items %}
  53. <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
  54. {% endfor %}
  55. {% if multiple %}
  56. </div>
  57. {% endif %}
  58. </div>
  59. {% endif %}