Advanced Thread Filtering

Developers
Version 1.5.8+
5 min read

You are viewing the documentation for Meerkat version 1 for Statamic 2. The latest version is Meerkat 2 for Statamic 3.

Read v2 docs

The content of this guide requires version 1.5.85 or higher of Meerkat for Statamic 2.

This guide will show-case some of the more powerful options for dynamically filtering Meerkat threads. The contents of this guide assume a new site helper file has been created, however.

Creating the Site Helper File

In your site's site/helpers/ directory, create a new Meerkat.php (casing is important; the full relative path would be /site/helpers/Meerkat.php) with the following initial content:

<?php

use Statamic\Addons\Meerkat\Support\Facades\Comments;


While it doesn't look too special just yet, we've just indicated to Meerkat that we will have stuff here that it can use, and also given ourselves access to the Comments helper, which we will use in just a moment.

Creating Filter Groups

The filter syntax can get quite long if your chaining multiple filters, or be annoying to keep updated if you're using them in many places (and not using Statamic partials). To help with this, you can define a filter group like so:

<?php

use Statamic\Addons\Meerkat\Support\Facades\Comments;

Comments::filterGroup('myGroup', 'user:has_auth(true)|thread:in(*current*)');

Then, in your Antler's template, you may use all the filters in your new group by referencing it with the @ prefix:

{{ meerkat:all-comments filter="@myGroup" }}

{{ /meerkat:all-comments }}

Creating Custom Filters

You may also register custom filters by using the filter method:

<?php

use Statamic\Addons\Meerkat\Support\Facades\Comments;

/**
 * Only returns comments that have at least 5 characters.
 */
Comments::filter('myCustomFilter', function ($comments) {
   return $comments->filter(function ($comment) {
       // Filter comments that have at least 5 characters.

       return mb_strlen($comment['comment']) >= 5;
   });
});

Then, in your template you can use it just like a normal filter:

{{ meerkat:all-comments filter="myCustomFilter" }}

{{ /meerkat:all-comments }}

You may also reference custom filters within a group:

Comments::filterGroup('myGroup', 'user:has_auth(true)|thread:in(*current*)|myCustomFilter');

Supplying Input to Filters

As we saw in the Filtering Threads guide, we can supply input to some filters:

{{ meerkat:responses filter="user:in(9d697ec3-9e55-4a91-b20c-3ae3c558e925, 38fda2fb-2d86-46bf-a4ab-18e2ae67ffec)" }}

{{ /meerkat:responses }}

When there are only a few values, this can be maintained easily within the template. However, with Meerkat comment filters, you can inject values into the filter. Within the /site/helpers/Meerkat.php site helper, we can teach Meerkat how to resolve a filter value like so:

<?php

use Statamic\Addons\Meerkat\Support\Facades\Comments;

Comments::resolve('usersToInclude', function () {
    // Whatever PHP code you want to generate the
    // list of user ID's you are interested in.

    return [
        '*current*', // The *current* shortcut works here, too :)
        '9d697ec3-9e55-4a91-b20c-3ae3c558e925',
        '38fda2fb-2d86-46bf-a4ab-18e2ae67ffec',
        '07ec3f6b-ec1b-4b5c-a63a-efe05640b6d6'
    ];
});


Then, in our Antlers template, we can let Meerkat know we want to use the return value of usersToInclude as filter input by prefixing it with the $ symbol:

{{ meerkat:responses filter="user:in($usersToInclude)" }}

{{ /meerkat:responses }}

When Meerkat processes the user:in filter, it will supply the return value of the usersToInclude resolvable as the input to the filter. Neat!

This feature also works if you have PHP disabled in your Antlers templates, so it can be quite powerful!

Registering Filters from Meerkat Addons

If you are developing a Meerkat Addon, you may wish to add your own filters to the filtering system. When Meerkat starts gathering filters when preparing a comment thread, it emits the Meerkat.theme.filters event. The first, and only, argument supplied to the event handler will be a shared instance of Statamic\Addons\Meerkat\Extend\ThemeFilters.

The following demonstrates how to register a filter from an addon's listener:


<?php

namespace Statamic\Addons\MyMeerkatAddon;

use Statamic\Extend\Listener;

class MyMeerkatAddonListener extends Listener
{

    public $events = [
        'Meerkat.theme.filters' => 'registerFilters',
    ];

    public function registerFilters($filters)
    {
    	$filters->filter('myaddon:filter_name', function ($comments) {
			return $comments->filter(function ($comment) {
			       // Filter comments that have at least 5 characters.

			       return mb_strlen($comment['comment']) >= 5;
			   });
    	});
    }
}

When registering addon filters, its a good idea to add a filter namespace prefix (myaddon: in the previous example) to help keep things organized.

Looking for some simpler default options? Check out the Filtering Threads guide.