file field--node--field-image--article.html.twig

Theme override for a field.

To override output, copy the "field.html.twig" from the templates directory to your theme's directory and customize it, just like customizing other Drupal templates such as page.html.twig or node.html.twig.

Instead of overriding the theming for all fields, you can also just override theming for a subset of fields using Theme hook suggestions. For example, here are some theme hook suggestions that can be used for a field_foo field on an article node type:

  • field--node--field-foo--article.html.twig
  • field--node--field-foo.html.twig
  • field--node--article.html.twig
  • field--field-foo.html.twig
  • field--text-with-summary.html.twig
  • field.html.twig
Available variables:
  • attributes: HTML attributes for the containing element.
  • label_hidden: Whether to show the field label or not.
  • title_attributes: HTML attributes for the title.
  • label: The label for the field.
  • multiple: TRUE if a field can contain multiple items.
  • items: List of all the field items. Each item contains:
    • attributes: List of HTML attributes for each item.
    • content: The field item's content.
  • entity_type: The entity type to which the field belongs.
  • field_name: The name of the field.
  • field_type: The type of the field.
  • label_display: The display settings for the label.
Source
  1. {#
  2. /**
  3. * @file
  4. * Theme override for a field.
  5. *
  6. * To override output, copy the "field.html.twig" from the templates directory
  7. * to your theme's directory and customize it, just like customizing other
  8. * Drupal templates such as page.html.twig or node.html.twig.
  9. *
  10. * Instead of overriding the theming for all fields, you can also just override
  11. * theming for a subset of fields using
  12. * @link themeable Theme hook suggestions. @endlink For example,
  13. * here are some theme hook suggestions that can be used for a field_foo field
  14. * on an article node type:
  15. * - field--node--field-foo--article.html.twig
  16. * - field--node--field-foo.html.twig
  17. * - field--node--article.html.twig
  18. * - field--field-foo.html.twig
  19. * - field--text-with-summary.html.twig
  20. * - field.html.twig
  21. *
  22. * Available variables:
  23. * - attributes: HTML attributes for the containing element.
  24. * - label_hidden: Whether to show the field label or not.
  25. * - title_attributes: HTML attributes for the title.
  26. * - label: The label for the field.
  27. * - multiple: TRUE if a field can contain multiple items.
  28. * - items: List of all the field items. Each item contains:
  29. * - attributes: List of HTML attributes for each item.
  30. * - content: The field item's content.
  31. * - entity_type: The entity type to which the field belongs.
  32. * - field_name: The name of the field.
  33. * - field_type: The type of the field.
  34. * - label_display: The display settings for the label.
  35. *
  36. * @ingroup templates
  37. *
  38. * @see template_preprocess_field()
  39. */
  40. #}
  41. {%
  42. set classes = [
  43. 'field',
  44. 'field--name-' ~ field_name|clean_class,
  45. 'field--type-' ~ field_type|clean_class,
  46. 'field--label-' ~ label_display,
  47. 'col-sm-4',
  48. ]
  49. %}
  50. {%
  51. set title_classes = [
  52. 'field--label',
  53. label_display == 'visually_hidden' ? 'sr-only',
  54. ]
  55. %}
  56. {% if label_hidden %}
  57. {% if multiple %}
  58. <div{{ attributes.addClass(classes, 'field--items') }}>
  59. {% for item in items %}
  60. <div{{ item.attributes.addClass('field--item') }}>{{ item.content }}</div>
  61. {% endfor %}
  62. </div>
  63. {% else %}
  64. {% for item in items %}
  65. <div{{ attributes.addClass(classes, 'field--item') }}>{{ item.content }}</div>
  66. {% endfor %}
  67. {% endif %}
  68. {% else %}
  69. <div{{ attributes.addClass(classes) }}>
  70. <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
  71. {% if multiple %}
  72. <div class="field__items">
  73. {% endif %}
  74. {% for item in items %}
  75. <div{{ item.attributes.addClass('field--item') }}>{{ item.content }}</div>
  76. {% endfor %}
  77. {% if multiple %}
  78. </div>
  79. {% endif %}
  80. </div>
  81. {% endif %}

Related topics