Display a (custom) block's name in a template

Hi everybody :slight_smile:

I use custom blocks as “modules” for a client and now I want to create a overview page with all the available custom blocks.
I want to add some headlines to the blocks with their names but I am only able to get the block’s type into the frontend and they don’t look too nice as headlines.
Is there any way to display the block’s name instead of the type? I tried a few things but wasn’t successful.

I am imagining something like this:

<?php foreach ($modules = $page->modules()->toBlocks() as $module): ?>
  <div id="<?= $module->id() ?>">
    <div class="modulecontainer pb-12 pt-24 border-t mt-12">
      <h2 class="h2"><?= $module->name() ?></h2>
    </div>
    <?php snippet('modules/' . $module->type(), ['module' => $module]) ?>
  </div>
  <?php endforeach ?>

With an example for one of my custom blocks:

name: Text Modul
icon: text
label: '{{ headline }}'
preview: fields
wysiwyg: true
tabs:
  content:
    label: Inhalt
    fields:
      overline: fields/overline
      headline: fields/headline
      text: fields/copy
  settings:
    label: Einstellungen
    fields:
      background: fields/background
      anchor: fields/anchor

In that case I want to display “Text Modul” as headline.
Maybe somebody has an idea how to solve this. Appreciate every hint, thanks :slight_smile:
Have a great week!
Best regards,
Lasse

The name is not available as a property on the block objects. So I think you have to options:

  • Read the name from the block blueprint (probably slow)
  • Create an array of key/value pairs in config, and read the title from there

Hi @pixelijn, thanks for your quick answer!

I tried to read the block’s name from the blueprint but was again only able to get the block’s type via the $page->blueprint() method (and then dig further into the object).

But as you suggested I created a key/value array. Should do it’s job for now :slight_smile:

Anyhow, I might keep this question open if you don’t mind. Maybe somebody has another idea :slight_smile:

I meant from the block blueprint, not from the page blueprint, but I don’t think performance-wise this is such a great idea, because you would end up reading a lot of blueprints.

Another idea could be to create block models for each block, but that seems to be overkill for something like this as well.

Ahh!
I have a hard time to access the block’s blueprint.
I tried (as mentioned before) with the $page->blueprint() method
and with looping over $page->modules()->toBlocks() as $module
and then accessing it with $module->blueprint()
but all I get is this:

Kirby\Cms\Field {#739 ▼
  #key: "blueprint"
  #parent: Kirby\Cms\Page {#246 ▶}
  +value: null
  blueprint: null
}

How do I access the block’s blueprint?
The page is for the client only and does not face other users, so performance should not be a big problem.

Thanks again for your help.

<?php 
use Kirby\Cms\Blueprint;

$blueprint = Blueprint::load('blocks/blockname'); 
dump($blueprint);
1 Like

Thank you very much! That did the trick!