->excerpt() for editor field

Hi, is it possible to trim text of editor field?

My goal is to show trimmed text in meta description from body (editor) field.

Trying this, but getting nothing:

$desc = strip_tags($page->body()->blocks()->excerpt(90));

Hi,

Try $desc = Str::excerpt($page->body()->blocks(), 90);. Tags should be stripped by default too.

Thanks a lot! It’s working this way.

Maybe it’s also possible to do the same thing for blueprint? I am showing body (editor) field content as placeholder of meta field, so that user knows what will be printed if he leaves field empty:

      metaDescription:
        ....
        placeholder: "{{ page.body.blocks }}"

You could use a page method (or a model, depending on use case):

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'bodyBlockExcerpt' => function () {
            return Str::excerpt($this->body()->blocks(), 90);
        }
    ]
]);

In blueprint:

placeholder: "{{ page.bodyBlockExcerpt }}"

Thank you, trying to do this, not sure if I do it right. I created file in /site/plugins/page-methods/index.php with this code:

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'bodyBlockExcerpt' => function () {
            return Str::excerpt($this->body()->blocks(), 90);
        }
    ]
]);

also changed blueprint, but getting empty placeholder.

I had a typo above, using bodyBlockExcerpt as method and calling as bodyBlocksExcerpt, maybe you have copied the error?

Ah, yes, didn’t notice, changed both to same: bodyBlockExcerpt and now it’s working, thanks a lot!