You are here

comment.html.twig in Drupal 10

Bartik's theme implementation for comments.

Available variables:

  • author: (optional) Comment author. Can be a link or plain text.
  • content: The content-related items for the comment display. Use {{ content }} to print them all, or print a subset such as {{ content.field_example }}. Use the following code to temporarily suppress the printing of a given child element:

  {{ content|without('field_example') }}
  
  • created: (optional) Formatted date and time for when the comment was created. Preprocess functions can reformat it by calling DateFormatter::format() with the desired parameters on the 'comment.created' variable.
  • changed: (optional) Formatted date and time for when the comment was last changed. Preprocess functions can reformat it by calling DateFormatter::format() with the desired parameters on the 'comment.changed' variable.
  • permalink: Comment permalink.
  • submitted: (optional) Submission information created from author and created during template_preprocess_comment().
  • user_picture: (optional) The comment author's profile picture.
  • status: Comment status. Possible values are: unpublished, published, or preview.
  • title: (optional) Comment title, linked to the comment.
  • attributes: HTML attributes for the containing element. The attributes.class may contain one or more of the following classes:

    • comment: The current template type; e.g., 'theming hook'.
    • by-anonymous: Comment by an unregistered user.
    • by-{entity-type}-author: Comment by the author of the parent entity, eg. by-node-author.
    • preview: When previewing a new or edited comment.

    The following applies only to viewers who are registered users:

    • unpublished: An unpublished comment visible only to administrators.
  • title_prefix: Additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.
  • title_suffix: Additional output populated by modules, intended to be displayed after the main title tag that appears in the template.
  • title_attributes: Same as attributes, except applied to the main title tag that appears in the template.
  • content_attributes: List of classes for the styling of the comment content.
  • threaded: A flag indicating whether the comments are threaded or not.

These variables are provided to give context about the parent comment (if any, optional):

  • parent_comment: Full parent comment entity (if any).
  • parent_author: Equivalent to author for the parent comment.
  • parent_created: Equivalent to created for the parent comment.
  • parent_changed: Equivalent to changed for the parent comment.
  • parent_title: Equivalent to title for the parent comment.
  • parent_permalink: Equivalent to permalink for the parent comment.
  • parent: A text string of parent comment submission information created from 'parent_author' and 'parent_created' during template_preprocess_comment(). This information is presented to help screen readers follow lengthy discussion threads. You can hide this from sighted users using the class visually-hidden.

These two variables are provided for context:

  • comment: Full comment object.
  • commented_entity: Entity the comments are attached to.

File

core/themes/bartik/templates/comment.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Bartik's theme implementation for comments.
  5. *
  6. * Available variables:
  7. * - author: (optional) Comment author. Can be a link or plain text.
  8. * - content: The content-related items for the comment display. Use
  9. * {{ content }} to print them all, or print a subset such as
  10. * {{ content.field_example }}. Use the following code to temporarily suppress
  11. * the printing of a given child element:
  12. * @code
  13. * {{ content|without('field_example') }}
  14. * @endcode
  15. * - created: (optional) Formatted date and time for when the comment was
  16. * created. Preprocess functions can reformat it by calling
  17. * DateFormatter::format() with the desired parameters on the
  18. * 'comment.created' variable.
  19. * - changed: (optional) Formatted date and time for when the comment was last
  20. * changed. Preprocess functions can reformat it by calling
  21. * DateFormatter::format() with the desired parameters on the
  22. * 'comment.changed' variable.
  23. * - permalink: Comment permalink.
  24. * - submitted: (optional) Submission information created from author and
  25. * created during template_preprocess_comment().
  26. * - user_picture: (optional) The comment author's profile picture.
  27. * - status: Comment status. Possible values are:
  28. * unpublished, published, or preview.
  29. * - title: (optional) Comment title, linked to the comment.
  30. * - attributes: HTML attributes for the containing element.
  31. * The attributes.class may contain one or more of the following classes:
  32. * - comment: The current template type; e.g., 'theming hook'.
  33. * - by-anonymous: Comment by an unregistered user.
  34. * - by-{entity-type}-author: Comment by the author of the parent entity,
  35. * eg. by-node-author.
  36. * - preview: When previewing a new or edited comment.
  37. * The following applies only to viewers who are registered users:
  38. * - unpublished: An unpublished comment visible only to administrators.
  39. * - title_prefix: Additional output populated by modules, intended to be
  40. * displayed in front of the main title tag that appears in the template.
  41. * - title_suffix: Additional output populated by modules, intended to be
  42. * displayed after the main title tag that appears in the template.
  43. * - title_attributes: Same as attributes, except applied to the main title
  44. * tag that appears in the template.
  45. * - content_attributes: List of classes for the styling of the comment content.
  46. * - threaded: A flag indicating whether the comments are threaded or not.
  47. *
  48. * These variables are provided to give context about the parent comment (if
  49. * any, optional):
  50. * - parent_comment: Full parent comment entity (if any).
  51. * - parent_author: Equivalent to author for the parent comment.
  52. * - parent_created: Equivalent to created for the parent comment.
  53. * - parent_changed: Equivalent to changed for the parent comment.
  54. * - parent_title: Equivalent to title for the parent comment.
  55. * - parent_permalink: Equivalent to permalink for the parent comment.
  56. * - parent: A text string of parent comment submission information created from
  57. * 'parent_author' and 'parent_created' during template_preprocess_comment().
  58. * This information is presented to help screen readers follow lengthy
  59. * discussion threads. You can hide this from sighted users using the class
  60. * visually-hidden.
  61. *
  62. * These two variables are provided for context:
  63. * - comment: Full comment object.
  64. * - commented_entity: Entity the comments are attached to.
  65. *
  66. * @see template_preprocess_comment()
  67. */
  68. #}
  69. {%
  70. set classes = [
  71. 'comment',
  72. 'js-comment',
  73. status != 'published' ? 'comment--' ~ status,
  74. comment.owner.anonymous ? 'by-anonymous',
  75. author_id and author_id == commented_entity.getOwnerId() ? 'by-' ~ commented_entity.getEntityTypeId() ~ '-author',
  76. 'clearfix',
  77. ]
  78. %}
  79. <article role="article"{{ attributes.addClass(classes)|without('role') }}>
  80. {#
  81. Hide the "new" indicator by default, let a piece of JavaScript ask the
  82. server which comments are new for the user. Rendering the final "new"
  83. indicator here would break the render cache.
  84. #}
  85. <span class="hidden" data-comment-timestamp="{{ new_indicator_timestamp }}"></span>
  86. {% if submitted %}
  87. <footer class="comment__meta">
  88. {{ user_picture }}
  89. <p class="comment__author">{{ author }}</p>
  90. <p class="comment__time">{{ created }}</p>
  91. <p class="comment__permalink">{{ permalink }}</p>
  92. {#
  93. Indicate the semantic relationship between parent and child comments
  94. for accessibility. The list is difficult to navigate in a screen
  95. reader without this information.
  96. #}
  97. {% if parent %}
  98. <p class="visually-hidden">{{ parent }}</p>
  99. {% endif %}
  100. </footer>
  101. {% endif %}
  102. <div{{ content_attributes.addClass('comment__content') }}>
  103. {% if title %}
  104. {{ title_prefix }}
  105. <h3{{ title_attributes }}>{{ title }}</h3>
  106. {{ title_suffix }}
  107. {% endif %}
  108. {{ content }}
  109. </div>
  110. </article>