Comment
Whenever you’re dealing with a comment in your template, you’re working with a Comment object. You get one from a comment query, or as comment inside the default comment templates.
{% set comments = craft.comments.fetch().owner(entry).all() %}
{% for comment in comments %}
<p>{{ comment.author.fullName }}, {{ comment.getTimeAgo() }}</p>
<p>{{ comment.comment }}</p>
{% endfor %}
Properties
The ID of the comment.
The ID of the element the comment was made on, such as an entry or asset.
The ID of the site of the element the comment was made on.
The element the comment was made on, such as an entry or asset. It’s null if that element has been deleted. This is the same as calling getOwner().
The ID of the user who posted the comment. It’s null for a guest.
The user (opens new window) who posted the comment. For a guest, it’s a new user holding the name and email address they entered, and for a user who has since been deleted, it’s a new user named “[Deleted User]”. This is the same as calling getAuthor().
The comment this one replies to. It’s null for a top-level comment.
The depth of the comment in its thread. Top-level comments are at level 1 and replies to them are at level 2.
The comment’s status, which is approved, pending, spam or trashed.
The name a guest entered when posting the comment. It’s null for comments from logged-in users, so use author.fullName to show a name for every comment.
The email address a guest entered when posting the comment. Take care not to output it on public pages.
The comment’s text.
The URL of the GIF attached to the comment. It’s null when the comment has no GIF.
The URL of the page the comment was posted from.
The IP address the comment was posted from.
The user agent of the browser the comment was posted from.
The date the comment was posted.
Methods
Returns the user who posted the comment. For a guest or a deleted user, it returns a new user, as described under author.
Returns the element the comment was made on, or null if it has been deleted.
<a href="{{ comment.getOwner().url }}">{{ comment.getOwner().title }}</a>Returns: string|null
Returns the start of the comment’s text, cut at the last whole word before the length limit and ending in .... A comment shorter than the limit is returned in full.
{{ comment.getExcerpt(0, 50) }}Returns how long ago the comment was posted, as a short phrase such as “2 min ago”.
Returns whether the comment was posted by a guest rather than a logged-in user.
Returns the number of times the comment has been flagged.
Returns whether the comment has reached the number of flags set by the flaggedCommentLimit setting.
Returns whether the current user, or guest session, has already flagged the comment.
Returns the comment’s score, which is its upvotes minus its downvotes.
Returns the total number of votes on the comment, counting upvotes and downvotes together.
Returns the number of upvotes on the comment.
Returns the number of downvotes on the comment.
Returns whether the comment has reached the number of downvotes set by the downvoteCommentLimit setting.
Permission Methods
These methods check what the current user, or guest, can do with the comment, so you can show only the controls that will work.
Returns whether the current user can reply to the comment. It checks the same rules as posting a comment, plus the maxReplyDepth setting.
Returns whether the current user can edit the comment, which is only allowed for its logged-in author.
Returns whether the current user can delete the comment, which is only allowed for its logged-in author.
Returns whether the current user can vote on the comment. Voting needs to be enabled, guests need guest voting enabled, and a poorly rated comment can hide voting when hideVotingForThreshold is on.
Returns whether the current user can flag the comment. Flagging needs to be enabled, and guests need guest flagging enabled.
Returns: bool
Returns whether the current user can perform an action on the comment, as a shorthand for the methods above. Pass reply, edit, trash, vote or flag. You can also pass the name of a plugin setting, which returns that setting’s value as true or false.
{% if comment.can('reply') %}
<a href="#reply-{{ comment.id }}">Reply</a>
{% endif %}Deleting a Comment
You can delete a comment using a POST request to an action controller, and the following template code. You must supply a commentId and siteId in your form.
<form role="form" method="post" accept-charset="UTF-8">
<input type="hidden" name="action" value="comments/comments/trash">
<input type="hidden" name="siteId" value="{{ comment.siteId }}">
<input type="hidden" name="commentId" value="{{ comment.id }}">
{{ csrfInput() }}
<button type="submit">{{ 'Delete' | t('comments') }}</button>
</form>