Comments

Comments are stream entries representing the comment content and meta information.

Drop-in Integration

The easiest way to display comments and a form to author a comment is to simply drop a partial into your view:

{% include "anomaly.module.comments::comments" with {'subject': subject} %}

The subject must be an object like page or template.post or even user() if the user is logged in.

You can also define a channel. If no channel is provided the default channel will be used.

{% include "anomaly.module.comments::comments" with {'subject': subject, 'channel': 'secret'} %}

Displaying Comments

The fastest way to display comments is to use the comments function and provide your subject and optional channel:

{{ comments(subject, channel) }}

If you would like to simply load the partial yourself you may do so like this:

{% include "anomaly.module.comments::comments/index" with {'subject': subject, 'channel': channel} %}

Manually Displaying Comments

You can manually load comments from a subject object through it's discussion relation.

{% if post.discussion.comments.visible().isEmpty() %}
    There are no comments just yet.
{% else %}

    {% set comments = post.discussion
        .comments() // Return the comments relation
        .sorted()   // Apply default sorting order
        .visible()  // Only show approved / user's pending
        .paginate(100) %}
        
    {% for comment in comments %}
        {{ comment.created_by.username ?: comment.name }} said: {{ comment.content }}
    {% endfor %}
{% endif %}

Displaying Other Channels

You can define which channel of a discussion you would like to display by passing the channel slug to the discussion hook:

{% set comments = post.discussion('private').get() %}

Parsing Comments

It is a good idea to be mindful of the output from your comment content and process it accordingly.

As opposed to the simple output in the example above the below example also:

  1. Parses the output as markdown
  2. Purifies the content through an HTML Purifier
  3. Links @user mentions
  4. Wraps plain URLs with links
  5. Parses emoji shorthand into characters
{{ comment.content|markdown|str_purify|mentions_linkify|str_linkify|emoji|raw }}

Creating Comments

The comment form builder is bound as comment. When using the comment form builder you must define at least a subject:

{{ form('comment').subject(subject)|raw }}

You can also provide an optional channel:

{{ form('comment').subject(subject).channel(channel)|raw }}

Lastly you can also set the default approval for comments in case this is being used in a trusted area:

{{ form('comment').subject(subject).approved(true)|raw }}