field.html.twig 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. *
  37. * @see template_preprocess_field()
  38. */
  39. #}
  40. {%
  41. set classes = [
  42. 'field',
  43. 'field--name-' ~ field_name|clean_class,
  44. 'field--type-' ~ field_type|clean_class,
  45. 'field--label-' ~ label_display,
  46. ]
  47. %}
  48. {%
  49. set title_classes = [
  50. 'field__label',
  51. label_display == 'visually_hidden' ? 'visually-hidden',
  52. ]
  53. %}
  54. {% if label_hidden %}
  55. {% if multiple %}
  56. <div{{ attributes.addClass(classes, 'field__items') }}>
  57. {% for item in items %}
  58. <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
  59. {% endfor %}
  60. </div>
  61. {% else %}
  62. {% for item in items %}
  63. <div{{ attributes.addClass(classes, 'field__item') }}>{{ item.content }}</div>
  64. {% endfor %}
  65. {% endif %}
  66. {% else %}
  67. <div{{ attributes.addClass(classes) }}>
  68. <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
  69. {% if multiple %}
  70. <div class="field__items">
  71. {% endif %}
  72. {% for item in items %}
  73. <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div>
  74. {% endfor %}
  75. {% if multiple %}
  76. </div>
  77. {% endif %}
  78. </div>
  79. {% endif %}