Reference

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

id

The ID of the comment.

ownerId

The ID of the element the comment was made on, such as an entry or asset.

ownerSiteId

The ID of the site of the element the comment was made on.

owner

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().

userId

The ID of the user who posted the comment. It’s null for a guest.

author

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().

parent

The comment this one replies to. It’s null for a top-level comment.

level

The depth of the comment in its thread. Top-level comments are at level 1 and replies to them are at level 2.

status

The comment’s status, which is approved, pending, spam or trashed.

name

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.

email

The email address a guest entered when posting the comment. Take care not to output it on public pages.

comment

The comment’s text.

gifUrl

The URL of the GIF attached to the comment. It’s null when the comment has no GIF.

url

The URL of the page the comment was posted from.

ipAddress

The IP address the comment was posted from.

userAgent

The user agent of the browser the comment was posted from.

commentDate

The date the comment was posted.

Methods

getAuthor()

Returns the user who posted the comment. For a guest or a deleted user, it returns a new user, as described under author.

getOwner()

Returns the element the comment was made on, or null if it has been deleted.

<a href="{{ comment.getOwner().url }}">{{ comment.getOwner().title }}</a>

getExcerpt()

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) }}

getTimeAgo()

Returns how long ago the comment was posted, as a short phrase such as “2 min ago”.

isGuest()

Returns whether the comment was posted by a guest rather than a logged-in user.

getFlags()

Returns the number of times the comment has been flagged.

isFlagged()

Returns whether the comment has reached the number of flags set by the flaggedCommentLimit setting.

hasFlagged()

Returns whether the current user, or guest session, has already flagged the comment.

getVotes()

Returns the comment’s score, which is its upvotes minus its downvotes.

getAllVotes()

Returns the total number of votes on the comment, counting upvotes and downvotes together.

getUpvotes()

Returns the number of upvotes on the comment.

getDownvotes()

Returns the number of downvotes on the comment.

isPoorlyRated()

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.

canReply()

Returns whether the current user can reply to the comment. It checks the same rules as posting a comment, plus the maxReplyDepth setting.

canEdit()

Returns whether the current user can edit the comment, which is only allowed for its logged-in author.

canTrash()

Returns whether the current user can delete the comment, which is only allowed for its logged-in author.

canVote()

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.

canFlag()

Returns whether the current user can flag the comment. Flagging needs to be enabled, and guests need guest flagging enabled.

can()

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>