jacksleight / statamic-distill

This Statamic addon allows you to filter, fetch or index the individual values, sets and relations within your entries, from both root and deeply nested fields.
https://statamic.com/addons/jacksleight/distill
MIT License
9 stars 1 forks source link

Distill

🚧 Work In Progress: Some things may change/break. Feedback welcome.

This Statamic addon allows you to filter, fetch or index the individual values, sets and relations within your entries, from both root and deeply nested fields. It's useful for things like:

Installation

You can search for this addon in the Tools > Addons section of the Statamic control panel and click install, or run the following command from your project root:

composer require jacksleight/statamic-distill

Examples

Find every image in the page

{{ distill:page type="asset:*" is_image:is="true" unique="true" }}
    {{ url }}
{{ /distill:page }}

Extract all Bard text from a page builder

{{ { distill:bard from="builder" } | bard_text }}

Filter, sort and paginate a Grid field

{{ distill:parts_list depth="1" paginate="10" :category:is="get:category" :sort="get:sort" }}
    {{ items }}
        {{ name }} {{ price }}
    {{ /items }}
    {{ paginate }}
        <a href="https://github.com/jacksleight/statamic-distill/blob/main/{{ prev_page }}">←</a>
        <a href="https://github.com/jacksleight/statamic-distill/blob/main/{{ next_page }}">→</a>
    {{ /paginate }}
{{ /distill:parts_list }}

Add sections of a page to a search index

// config/statamic/search.php
'searchables' => [
    'distill:collection:pages:sections',
],
'fields' => ['heading'],
// app/Stills/Sections.php
namespace App\Stills;

use JackSleight\StatamicDistill\Stills\Still;

class Sections extends Still
{
    public function apply($query, $values)
    {
        $query->path('builder.*')->type('set:section');
    }
}
{{# resources/views/search.antlers.html #}}
{{ search:results }}
    {{ if result_type === 'distill:set:section' }}
        <a href="https://github.com/jacksleight/statamic-distill/blob/main/{{ info:source:url }}#{{ id }}">
            {{ title }}
        </a>
    {{ else }}
        ...
    {{ /if }}
{{ /search:results }}

Usage

Distill recursively walks through the value you provide looking for items that match your criteria. It can find individual paragraphs and field values through to entire sets and references to other content.

For optimal performance you should use the from, path, expand, limit and max_depth parameters to restrict where it goes based on what you're looking for. These options don't just filter the final result, they tell Distill where to look and when to stop.

Distill can find references to other entries, terms, assets and users, but it will not walk into those objects. Additionally Distill will only walk raw values if you provide one, it will not walk into a field's raw value.

Distill Tag

The {{ distill:* }} tag accepts the following parameters:

Each item returned includes an info object that contains the following values: