Score:1

How do I get the comment ID in a template file?

in flag

I want to show the comment title linked to the comment permalink. This is the content of the comment.html.twig file.

 <div{{ content_attributes.addClass('comment__content') }}>
    {% if title %}
      {{ title_prefix }}
      <h3{{ title_attributes }}><a href="#comment-{{comment.id}}">{{ title | render | striptags}}</a></h3>
      {{ title_suffix }}
    {% endif %}
    {{ content }}    
  </div>

{{comment.id}} didn't work. How do I get the comment ID?

No Sssweat avatar
ua flag
Call it as a function `{{ comment.id() }}`
Score:1
us flag

template_preprocess_comment() sets the following value, which is essentially the string you want to obtain, apart the hash at the beginning.

$variables['attributes']['id'] = 'comment-' . $comment->id();

The template file can use that value with {{ attributes.id }}.

<div{{ content_attributes.addClass('comment__content') }}>
  {% if title %}
    {{ title_prefix }}
    <h3{{ title_attributes }}><a href="#{{ attributes.id }}">{{ title|render|striptags }}</a></h3>
    {{ title_suffix }}
  {% endif %}
  {{ content }}
</div>

Reference

Karthik avatar
in flag
Thank you so much
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.