You are here

viewfield.html.twig in Viewfield 8.3

Default theme implementation for a viewfield

To override output, copy the "viewfield.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:

  • viewfield--node--field-foo--article.html.twig
  • viewfield--node--field-foo.html.twig
  • viewfield--node--article.html.twig
  • viewfield--field-foo.html.twig
  • viewfield--text-with-summary.html.twig
  • viewfield.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 viewfield items.
  • entity: The entity to which the field belongs.
  • entity_type: The entity type to which the field belongs.
  • bundle: The entity bundle to which the field belongs.
  • field_name: The name of the field.
  • label_display: The display settings for the label.

File

templates/viewfield.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Default theme implementation for a viewfield
  5. *
  6. * To override output, copy the "viewfield.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. * - viewfield--node--field-foo--article.html.twig
  16. * - viewfield--node--field-foo.html.twig
  17. * - viewfield--node--article.html.twig
  18. * - viewfield--field-foo.html.twig
  19. * - viewfield--text-with-summary.html.twig
  20. * - viewfield.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 viewfield items.
  29. * - entity: The entity to which the field belongs.
  30. * - entity_type: The entity type to which the field belongs.
  31. * - bundle: The entity bundle to which the field belongs.
  32. * - field_name: The name of the field.
  33. * - label_display: The display settings for the label.
  34. *
  35. *
  36. * @see template_preprocess_viewfield()
  37. */
  38. #}
  39. {%
  40. set classes = [
  41. 'field',
  42. 'field--name-' ~ field_name|clean_class,
  43. 'field--type-' ~ field_type|clean_class,
  44. 'field--label-' ~ label_display,
  45. ]
  46. %}
  47. {%
  48. set title_classes = [
  49. 'field__label',
  50. label_display == 'visually_hidden' ? 'visually-hidden',
  51. ]
  52. %}
  53. <div{{ attributes.addClass(classes) }}>
  54. {{ title_prefix }}
  55. {% if not label_hidden and label %}
  56. <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
  57. {% endif %}
  58. {{ title_suffix }}
  59. {% if multiple %}
  60. <div class="field__items">
  61. {% endif %}
  62. {% for item in items %}
  63. {{ item }}
  64. {% endfor %}
  65. {% if multiple %}
  66. </div>
  67. {% endif %}
  68. </div>