Skip to content

Kirby 6.0.0

Block models

Block models are registered with the blockModels extension.

Block models extend Kirby's default Kirby\Cms\Block object. Methods that you define in a block model are available everywhere in Kirby where you work with a block of the extended type.

Creating a custom block model

/site/plugins/block-models/index.php
use Kirby\Cms\Block;

class HeadingBlock extends Block
{
    public function toHtml(): string
    {
        return '<h1>Custom HTML for headings</h1>';
    }
}

Kirby::plugin(
    name: 'my/blockModels',
    extends: [
        'blockModels' => [
            'heading' => HeadingBlock::class
        ]
    ]
);

You may also load your model class from a different file. Be aware you have to add the file to the classloader manually:

/site/plugins/block-models/models/heading.php
class HeadingBlock extends Block
{
    public function toHtml(): string
    {
        return '<h1>Custom HTML for headings</h1>';
    }
}
/site/plugins/block-models/index.php
load([
    'HeadingBlock' => __DIR__ . '/models/heading.php'
]);

Kirby::plugin(
    name: 'my/blockModels',
    extends: [
        'blockModels' => [
            'heading' => HeadingBlock::class
        ]
    ]
);

Overwriting the default block class

You can also overwrite the default Kirby\Cms\Block model with a custom class.

/site/plugins/block-models/index.php
use Kirby\Cms\Block;

class DefaultBlock extends Block
{
    public function id(): string
    {
        return 'block-' . parent::id();
    }
}

Kirby::plugin(
    name: 'my/blockModels',
    extends: [
        'blockModels' => [
            'default' => DefaultBlock::class,
        ]
    ]
);