You are here

formatter-suite-field-list.html.twig in Formatter Suite 8

Override field template to support customized lists of fields.

This template is used by the Formatter Suite's field list formatters for numbers, entity references, etc. In each case, the formatter offers settings to select the type of list to create and whether to include separators (such as commas).

This template is loosely based upon the "field.html.twig" template in the Drupal core "classy" theme. That template is, in turn, based upon the Drupal core theme within the system module. The core theme does not add "field__*" classes, while the classy theme does. Most other core and third-party themes extend the classy theme in order to retain the "field__*" classes.

Available variables from Drupal core's FormatterBase:

  • 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.

Available variables added by Formatter Suite's list formatters:

  • list_style: The type of list to create. One of: ul, ol, div, or span.
  • list_separator: The separator string to append to each item, except the last.

File

templates/formatter-suite-field-list.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Override field template to support customized lists of fields.
  5. *
  6. * This template is used by the Formatter Suite's field list
  7. * formatters for numbers, entity references, etc. In each case,
  8. * the formatter offers settings to select the type of list to
  9. * create and whether to include separators (such as commas).
  10. *
  11. * This template is loosely based upon the "field.html.twig"
  12. * template in the Drupal core "classy" theme. That template is,
  13. * in turn, based upon the Drupal core theme within the system
  14. * module. The core theme does not add "field__*" classes, while
  15. * the classy theme does. Most other core and third-party themes
  16. * extend the classy theme in order to retain the "field__*"
  17. * classes.
  18. *
  19. * Available variables from Drupal core's FormatterBase:
  20. * - attributes: HTML attributes for the containing element.
  21. * - label_hidden: Whether to show the field label or not.
  22. * - title_attributes: HTML attributes for the title.
  23. * - label: The label for the field.
  24. * - multiple: TRUE if a field can contain multiple items.
  25. * - items: List of all the field items. Each item contains:
  26. * - attributes: List of HTML attributes for each item.
  27. * - content: The field item's content.
  28. * - entity_type: The entity type to which the field belongs.
  29. * - field_name: The name of the field.
  30. * - field_type: The type of the field.
  31. * - label_display: The display settings for the label.
  32. *
  33. * Available variables added by Formatter Suite's list formatters:
  34. * - list_style: The type of list to create. One of: ul, ol, div, or span.
  35. * - list_separator: The separator string to append to each item,
  36. * except the last.
  37. *
  38. * @see template_preprocess_field()
  39. *
  40. * @ingroup formatter_suite
  41. */
  42. #}
  43. {%
  44. set classes = [
  45. 'field',
  46. 'field--name-' ~ field_name|clean_class,
  47. 'field--type-' ~ field_type|clean_class,
  48. 'field--label-' ~ label_display,
  49. ]
  50. %}
  51. {%
  52. set title_classes = [
  53. 'field__label',
  54. label_display == 'visually_hidden' ? 'visually-hidden',
  55. ]
  56. %}
  57. {% set itemsLength = items|length %}
  58. {% set itemsIndex = 1 %}
  59. <div{{ attributes.addClass(classes) }}>
  60. {% if label_hidden == false %}
  61. <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
  62. {% endif %}
  63. {% if list_style == 'ul' %}
  64. <ul class="field__items">
  65. {% for item in items %}
  66. <li{{ item.attributes.addClass(classes, 'field__item') }}>
  67. {{ item.content }}
  68. </li>
  69. {% set itemsIndex = itemsIndex + 1 %}
  70. {% endfor %}
  71. </ul>
  72. {% elseif list_style == 'ol' %}
  73. <ol class="field__items">
  74. {% for item in items %}
  75. <li{{ item.attributes.addClass(classes, 'field__item') }}>
  76. {{ item.content }}
  77. </li>
  78. {% set itemsIndex = itemsIndex + 1 %}
  79. {% endfor %}
  80. </ol>
  81. {% elseif list_style == 'div' %}
  82. <div class="field__items">
  83. {% for item in items %}
  84. <div{{ item.attributes.addClass(classes, 'field__item') }}>
  85. {{ item.content }}
  86. </div>
  87. {% set itemsIndex = itemsIndex + 1 %}
  88. {% endfor %}
  89. </div>
  90. {% elseif multiple == true %}
  91. <div class="field__items">
  92. {% for item in items %}
  93. <span{{ item.attributes.addClass(classes, 'field__item') }}>
  94. {% if itemsIndex == itemsLength %}
  95. {{ item.content }}
  96. {% else %}
  97. {{ item.content }}{{ list_separator }}
  98. {% endif %}
  99. </span>
  100. {% set itemsIndex = itemsIndex + 1 %}
  101. {% endfor %}
  102. </div>
  103. {% else %}
  104. {% for item in items %}
  105. <div{{ item.attributes.addClass(classes, 'field__item') }}>{{ item.content }}</div>
  106. {% endfor %}
  107. {% endif %}
  108. </div>

Related topics