hello,
I’m attempting to build a design system page that displays all the blocks used in my CMS.
I’d like to be able to include the block name as it’s display in the panel editor, and the info field text that is displayed in the block editor.
I found this cookbook recipe for blueprints in the frontend. But here it says it}s only for pages, users etc. Blocks aren’t mentioned. Here’s my basic code:
<div class="container">
<h1><?= $page->title() ?></h1>
<?php foreach ($page->modules()->toBlocks() as $block): ?>
<?php $moduleTitle = $block->blueprint()->name() ?>
<div class="moduleBox">
<h2><?= $moduleTitle ?></h2>
<div class="module"><?= $block ?></div>
<p><?= $block->blueprint()->field('info') ?></p>
</div>
<?php endforeach ?>
</div>
Is there any other way I could access that data from the blueprint, without having to create it twice in the blueprint? ie by setting up conventional fields?
Thanks
Anthony
You can access a block blueprint using Blueprint::load()
, e.g. to load the image block blueprint
<?php dump(Kirby\Cms\Blueprint::load('blocks/image')) ?>
Okay, I see thank you.
So what are the steps to
a) dynamically get the block name (…::load('blocks/??'))
in a foreach loop, and
b) get the value of a particular field?
In my case, I want the block name, and the text from an info field.
$block->type()
gives you the block type:
<?php dump(Kirby\Cms\Blueprint::load('blocks/' . $block->type())) ?>
Then look at the dump to check what sort of information you get.
Example for a text block looks like this:
Array
(
[name] => field.blocks.text.name
[icon] => text
[wysiwyg] => 1
[preview] => text
[fields] => Array
(
[text] => Array
(
[type] => writer
[nodes] =>
[placeholder] => field.blocks.text.placeholder
)
)
[title] => Field.blocks.text.name
)
Thank you!
This is great, but I still need a bit of hand-holding, sorry.
What’s the syntax for accessing one of those values in the array?
My guesses are <?php $moduleTitle = Kirby\Cms\Blueprint::load('blocks/' . $block->type()->name()) ?>
or <?= $moduleTitle->name() ?>
, but this returns Call to a member function name() on string
error
You can access array values by their index, for example:
<?php
$blueprint = Kirby\Cms\Blueprint::load('blocks/' . $block->type());
echo $blueprint['name'];
$fields = $blueprint['fields']; // will be an array again, so you can loop through each field here
and so on
thank you, this has got me where I needed to be