Skip to main content

Text & Bard

Two of the most common things to want out of a nested field are all of its plain text, and all of its Bard content. Distill has dedicated tags, modifiers and PHP methods for both.


Text

The {{ distill:text }} tag returns the plain text from every text, textarea, bard and markdown field within the source, joined with spaces. Bard fields are run through bard_text and Markdown fields are rendered and stripped of tags, so no markup ends up in the result.

{{ distill:text from="builder" }}

It accepts all of the tag parameters except type, so you can restrict where the text comes from with path, expand or max_depth, or apply a still.

The distill_text modifier does the same thing with no options:

{{ "builder" | distill_text }}
Note

The modifier must be passed the name of the field (as a string), not the field value itself. It needs the underlying field data, which is gone by the time a value reaches a modifier.


Bard

The {{ distill:bard }} tag returns the content of every Bard field within the source combined into a single Bard value, which you can then pass to any of Statamic's bard_* modifiers:

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

The distill_bard modifier does the same thing, and needs the field name just like distill_text:

{{ "builder" | distill_bard | bard_text }}

Combined with bard_items this is a handy way to pull a specific kind of node out of a whole page builder, for example every heading:

{{ headings = { distill:bard from="builder" } | bard_items | where('type', 'heading') }}
{{ headings }}
    <a href="#{{ content | bard_text | slugify }}">{{ content | bard_text }}</a>
{{ /headings }}

In PHP

The Distill::text() and Distill::bard() methods take any value the query builder takes:

use JackSleight\StatamicDistill\Facades\Distill;

$text = Distill::text($entry->augmentedValue('builder'));
$data = Distill::bard($entry->augmentedValue('builder'));

Both modifiers can also be used from PHP or Blade, and here they'll accept the value object directly:

use Statamic\Statamic;

$text = Statamic::modify($entry->augmentedValue('builder'))->distillText()->fetch();

If you've already run a query of your own you can pass its results to Distill::extractText() or Distill::extractBard() instead. The query needs to match the relevant value:* types and have includeSource enabled, so the source field is included if it's a text field itself:

use JackSleight\StatamicDistill\Facades\Distill;

$items = Distill::query($entry)
    ->type('value:bard')
    ->path('builder.*.body')
    ->includeSource(true)
    ->get();

$data = Distill::extractBard($items);