You are here

timefield.html.twig in Timefield 1.0.x

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.

File

templates/timefield.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. *
  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. label_display == 'inline' ? 'clearfix',
  47. ]
  48. %}
  49. {%
  50. set title_classes = [
  51. 'field__label',
  52. label_display == 'visually_hidden' ? 'visually-hidden',
  53. ]
  54. %}
  55. {% if label_hidden %}
  56. {% if multiple %}
  57. <div{{ attributes.addClass(classes, 'field__items') }}>
  58. {% for item in items %}
  59. <div{{ attributes.addClass('field__item field__timefield_time') }}>{{ item.value }}</div>
  60. {% if settings.totime %}
  61. <div{{ attributes.addClass('field__item field__timefield_totime') }}>{{ item.value2 }}</div>
  62. {% endif %}
  63. {% if settings.weekly_summary or settings.weekly_summary_with_label %}
  64. <div{{ attributes.addClass('field__item field__timefield_days') }}>{{ item.days|join(', ') }}</div>
  65. <div{{ attributes.addClass('field__item field__timefield_label') }}>{{ item.label }}</div>
  66. {% endif %}
  67. {% endfor %}
  68. </div>
  69. {% else %}
  70. {% for item in items %}
  71. <div{{ attributes.addClass('field__item field__timefield_time') }}>{{ item.value }}</div>
  72. {% if settings.totime %}
  73. <div{{ attributes.addClass('field__item field__timefield_totime') }}>{{ item.value2 }}</div>
  74. {% endif %}
  75. {% if settings.weekly_summary or settings.weekly_summary_with_label %}
  76. <div{{ attributes.addClass('field__item field__timefield_days') }}>{{ item.days|join(', ') }}</div>
  77. <div{{ attributes.addClass('field__item field__timefield_label') }}>{{ item.label }}</div>
  78. {% endif %}
  79. {% endfor %}
  80. {% endif %}
  81. {% else %}
  82. <div{{ attributes.addClass(classes) }}>
  83. {% if multiple %}
  84. <div class="field__items">
  85. {% endif %}
  86. {% for item in items %}
  87. <div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
  88. <div{{ attributes.addClass('field__item field__timefield_time') }}>{{ item.value }}</div>
  89. {% if settings.totime %}
  90. <div{{ title_attributes.addClass(title_classes) }}>{{ 'To Time'|t }}</div>
  91. <div{{ attributes.addClass('field__item field__timefield_totime') }}>{{ item.value2 }}</div>
  92. {% endif %}
  93. {% if settings.weekly_summary or settings.weekly_summary_with_label %}
  94. <div{{ title_attributes.addClass(title_classes) }}>{{ 'Days'|t }}</div>
  95. <div{{ attributes.addClass('field__item field__timefield_days') }}>{{ items.days|join(', ') }}</div>
  96. <div{{ title_attributes.addClass(title_classes) }}>{{ 'Label' }}</div>
  97. <div{{ attributes.addClass('field__item field__timefield_label') }}>{{ item.label }}</div>
  98. {% endif %}
  99. {% endfor %}
  100. {% if multiple %}
  101. </div>
  102. {% endif %}
  103. </div>
  104. {% endif %}