Pull first text field from a blocks loop

Hello

I want to be able to pull the first 150 or so characters from the first text field in a set of blocks on a page.

It’s for the preview text in an email newsletter. I’ve been doing it manually so far – ie a specific page level field populates a hidden div at the start of the html body. But this is leading to editor errors when they forget to update it.

The first block in the layout is usually an image block, so I want to skip over that and find the text content to extract the pre-header text. But there may be times when there is no image.

You can filter each block collection by block type:

$firstTextBlock = $page->myBlocksField()->toBlocks()->filterBy('type', 'text')->first();

Thank you! I’ve realised that I’m using custom blocks here. So should the filterBy type be the customer block file name? In my case is filterBy('type', 'text-block')

Then to get only the first 150 characters, is the excerpt field method the thing to use here?

Yes, that should work.

Thank you