Skip to content

Commit

Permalink
feat: add LayoutHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
evolkmann committed Sep 30, 2024
1 parent 9582bab commit bf9fed8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/LayoutHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace VolkmannDesignCode\KirbyUtils;

use Kirby\Cms\Layout;

class LayoutHelpers {

static function hasBlockType(Layout $layout, array $blockTypes): bool
{
foreach ($layout->columns() as $column) {
foreach ($column->blocks() as $block) {
if (in_array($block->type(), $blockTypes)) {
return true;
}
}
}

return false;
}

}
32 changes: 32 additions & 0 deletions tests/LayoutHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

use Kirby\Cms\Layout;
use PHPUnit\Framework\TestCase;
use VolkmannDesignCode\KirbyUtils\LayoutHelpers;

final class LayoutHelpersTest extends TestCase
{
public function testHasBlockType(): void
{
$layout = new Layout([
'columns' => [
[
'blocks' => [
['type' => 'heading'],
['type' => 'text'],
]
],
[
'blocks' => [
['type' => 'heading'],
['type' => 'text'],
]
]
]
]);

$this->assertTrue(LayoutHelpers::hasBlockType($layout, ['heading']));
$this->assertFalse(LayoutHelpers::hasBlockType($layout, ['image']));
}

}

0 comments on commit bf9fed8

Please sign in to comment.