jacksleight / statamic-bard-mutator

This Statamic addon allows you to modify the data and tags rendered by the Bard fieldtype, giving you full control over the final HTML.
https://statamic.com/addons/jacksleight/bard-mutator
MIT License
19 stars 3 forks source link

List_item not working #7

Closed pitcher-maarten closed 2 years ago

pitcher-maarten commented 2 years ago
Mutator::data('list_item', function ($data) {
    ...
});

It seems like the list_item data mutator is not getting triggered. All mutator:tags work like aspected.

jacksleight commented 2 years ago

Are other data mutators working or is it just list_item? If none are working are you using the tag? (https://jacksleight.github.io/statamic-bard-mutator/rendering.html) That's required to use data mutators.

pitcher-maarten commented 2 years ago

Aha we are using the addon along the Content API so that sounds logic. Is there any way to accomplish this with the tag mutator?

jacksleight commented 2 years ago

Outside of antlers you can use the Mutator::render method to do the same thing (https://jacksleight.github.io/statamic-bard-mutator/rendering.html#direct). eg via a custom API resource (https://statamic.dev/rest-api#customizing-resources) you can do something like:

class CustomEntryResource extends EntryResource
{
    public function toArray($request)
    {
        return [
            'my_content' => Mutator::render($this->resource->augmentedValue('my_content'));
        ];
    }
}
jacksleight commented 2 years ago

Is there any way to accomplish this with the tag mutator?

What are you trying to do, can you share the full mutator code?

pitcher-maarten commented 2 years ago

Well what I want to achieve is that all Bard fields, no matter in what data structure, get cleaned up. One of the things I want to clean up is removing the paragraph nodes inside list items.

So the best way to remove them would be by using the Mutator::tag I think?

jacksleight commented 2 years ago

Removing nodes is a mutation to the structure, so a data mutator is usually the right way to go. Many mutations aren’t possible via tag mutators which is why data mutators exist. However, removing the paragraphs from list items is actually possible with a tag mutator, albeit not officially supported.

If you can’t use a data mutator this should do it instead:

Mutator::tag('list_item', function ($tag, $data) {
    if (($data->content[0]->type ?? null) === 'paragraph') {
        $data->content = $data->content[0]->content;
    }
    return $tag;
});

As you can see this isn't actually mutating the tag it's mutating the child data.