hasBlockType() not available for individual $layout?

I’m using Layout Settings to allow for a background colour to be added to individual layouts. I’d like to ignore this setting/force a value in some cases where a certain block is used:

    <?php foreach ($page->layout()->toLayouts() as $layout): ?>
        <?php
        $palette = $layout->attrs()->bg()->yaml();
        $bg = $palette['key']; #(string)
        if($layout->hasBlockType('sectionheader') == true){
            $bg = 'shade-100';
        }
        ?>
        <section class="grid" data-cols="<?= $layout->columns()->count() ?>" data-bg="<?= $bg ?>">
        ...

This doesn’t work though, as $layout->hasBlockType doesn’t appear to be possible. I tried check the columns() for the block, but that also doesn’t work. Is there a way to do this that I’m missing?

There is no $layout->hasBlockType() method, $layouts->hasBlockType() available.

I saw that, but is there a way to check if a block exists in an individual layout? Checking the whole layouts collection is not very useful for what I want to do. It also doesn’t exist for $layout->columns(). Is what I’m trying to do even possible?

You can create your own custom layout methods:

Check out this sample:

Ah, I missed those, thanks! I got it working by adding all of the block types to an array, then checking if the name of the block I want to exclude is in the array. Maybe not the best way, but working is better than perfect!

<?php
Kirby::plugin('jamiehunter/layout-methods', [
    'layoutMethods' => [
        'layoutToBlocks' => function ($layout) {
            $blocks = [];
            foreach ($layout->columns() as $column) {
                foreach ($column->blocks() as $block) {
                    $blocks[] = $block->type();
                }
            }
            return $blocks;
        }
    ]
]);
?>
if (in_array('sectionheader',$layout->layoutToBlocks($layout))) {
    $bg = 'shade-100';
} 

AFAIK You don’t need to pass $layout object:

<?php

Kirby::plugin('jamiehunter/layout-methods', [
    'layoutMethods' => [
        'layoutToBlocks' => function () {
            $blocks = [];
            foreach ($this->columns() as $column) {
                foreach ($column->blocks() as $block) {
                    $blocks[] = $block->type();
                }
            }
            return $blocks;
        }
    ]
]);


if (in_array('sectionheader', $layout->layoutToBlocks())) {
    //
}