Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Its not possible to print out the number of comments that are written for a node.
{{content.comment['#items'].comment_count}}
is available when a node is viewed in page mode, but not in teaser mode
Proposed resolution
create a variable {{ node.comment_count }}
thats available both in teaser & page
Comments
Comment #2
dawehnerWell, what you can do is
{{node.field_comment.comment_count}}
which is the only logical thing if you have mulitple comment types.Are you sure that doesn't work?
Comment #3
aburrows CreditAttribution: aburrows as a volunteer commented@dawahner I can make it so it displays as {{ node.comment_count }}, are we agreed on that?
Comment #4
mortendk CreditAttribution: mortendk as a volunteer commented@dawehner yup i tested n tested again + it came up during badcamp, where we also tested it
kint( {{node.field_comment.comment_count}} )
gives me a null in a node.html.twigand for the record the number of comments is printed out in the link template, but thats a complete cluster to get through
Comment #5
larowlanWe can't use a single property, there could be more than one comment field
Comment #6
dawehnerHaters gonna hate, I give an atom about them, and this was a clear instance of that.
What actually works is
{{ node.comment.comment_count }}
because 'comment' is the fieldname by default.Comment #7
larowlanThat sounds like a closed works as designed to me
Comment #8
BerdirYes, I think so. That's exactly how you access that information, through the field property.
Comment #10
drase15 CreditAttribution: drase15 commentedthanks #6 works
just a heads up, "comment" is the machine name of your "comment type", not the field machine name.
so if you use another comment type just replace it for that machine name and you are ready to go