Testing if a block type exists in a layout

I’ve got a layout field and I just want to test if a certain block type exists.
Is there an ‘easier’ way to test instead of having to loop through layouts > columns > blocks?
Possibly with isset() or property_exists()?

Suggestions are appreciated, thx!

I’m doing this to get a block by id, you could adapt this to just return a collection of blocks or the value you are looking for.

	'fieldMethods' => [
		'findBlockById' => function($field, $id) {
			$blocks = new Kirby\Cms\Blocks();
			return $field->toLayouts()->map( static function($layout) use($blocks){
				return $layout->columns()->map( static function($column) use($blocks){
					$blocks->add($column->blocks());
				});
			})->findBy('id', $id);
		}
	]

Maybe there is an easier way to achieve this :woman_shrugging:

If you don’t know, who will …
But with some stack overflow inspiration the following I understand a littlebit better.
What do you think?

function blocksearch($blocktype, $layout) {
  foreach ($layout as $block) {
    if ($block === $blocktype) {
      return true;
    } elseif (is_array($block)) {
      $layout = blocksearch($blocktype, $block);
      if ($layout == true) {
      return true;
      }
    }
  }
return false;
};

// use:

$layout = json_decode($page->layout(), true);
echo blocksearch("gallery", $layout) ? 'yup' : 'nope'; 

Was doing this before :smirk:

// using loops
    <?php foreach ($page->layout()->toLayouts() as $layout): ?>
      <?php foreach ($layout->columns() as $column): ?>
        <?php foreach ($column->blocks() as $block): ?>
          <?php if ($block->type() == 'gallery' && $block->isNth(1)): ?>
              'yup'
           <?php else: ?>
              'nope'
          <?php endif ?>
      <?php endforeach?>
      <?php endforeach?>
   <?php endforeach?>