field.html.twig

Same filename in this branch
  1. 11.x core/profiles/demo_umami/themes/umami/templates/classy/field/field.html.twig
  2. 11.x core/themes/olivero/templates/field/field.html.twig
  3. 11.x core/themes/claro/templates/classy/field/field.html.twig
  4. 11.x core/themes/starterkit_theme/templates/field/field.html.twig
  5. 11.x core/modules/system/templates/field.html.twig
Same filename in other branches
  1. 9 core/profiles/demo_umami/themes/umami/templates/classy/field/field.html.twig
  2. 9 core/themes/olivero/templates/field/field.html.twig
  3. 9 core/themes/stable9/templates/field/field.html.twig
  4. 9 core/themes/seven/templates/classy/field/field.html.twig
  5. 9 core/themes/claro/templates/classy/field/field.html.twig
  6. 9 core/themes/bartik/templates/classy/field/field.html.twig
  7. 9 core/themes/stable/templates/field/field.html.twig
  8. 9 core/themes/starterkit_theme/templates/field/field.html.twig
  9. 9 core/themes/classy/templates/field/field.html.twig
  10. 9 core/modules/system/templates/field.html.twig
  11. 8.9.x core/profiles/demo_umami/themes/umami/templates/classy/field/field.html.twig
  12. 8.9.x core/themes/seven/templates/classy/field/field.html.twig
  13. 8.9.x core/themes/claro/templates/classy/field/field.html.twig
  14. 8.9.x core/themes/bartik/templates/classy/field/field.html.twig
  15. 8.9.x core/themes/stable/templates/field/field.html.twig
  16. 8.9.x core/themes/classy/templates/field/field.html.twig
  17. 8.9.x core/modules/system/templates/field.html.twig
  18. 10 core/profiles/demo_umami/themes/umami/templates/classy/field/field.html.twig
  19. 10 core/themes/olivero/templates/field/field.html.twig
  20. 10 core/themes/stable9/templates/field/field.html.twig
  21. 10 core/themes/claro/templates/classy/field/field.html.twig
  22. 10 core/themes/starterkit_theme/templates/field/field.html.twig
  23. 10 core/modules/system/templates/field.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:

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.

See also

template_preprocess_field()

1 theme call to field.html.twig
FormatterBase::view in core/lib/Drupal/Core/Field/FormatterBase.php
Builds a renderable array for a fully themed field.

File

core/themes/stable9/templates/field/field.html.twig

View 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. * @see template_preprocess_field()
  37. */
  38. #}
  39. {%
  40. set title_classes = [
  41. label_display == 'visually_hidden' ? 'visually-hidden',
  42. ]
  43. %}
  44. {% if label_hidden %}
  45. {% if multiple %}
  46. <div{{ attributes }}>
  47. {% for item in items %}
  48. <div{{ item.attributes }}>{{ item.content }}</div>
  49. {% endfor %}
  50. </div>
  51. {% else %}
  52. {% for item in items %}
  53. <div{{ attributes }}>{{ item.content }}</div>
  54. {% endfor %}
  55. {% endif %}
  56. {% else %}
  57. <div{{ attributes }}>
  58. <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
  59. {% if multiple %}
  60. <div>
  61. {% endif %}
  62. {% for item in items %}
  63. <div{{ item.attributes }}>{{ item.content }}</div>
  64. {% endfor %}
  65. {% if multiple %}
  66. </div>
  67. {% endif %}
  68. </div>
  69. {% endif %}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.