Creating Custom Block

This tutorial will help you to create a simple block with manageable HTML content.

Note

This tutorial assumes that you’ve already read the section about Blocks and Editables and already have sample CMS Package configured.

Block Configuration

Create a class, that would represent your block configuration. It should extend abstract BlockConfig class:

1
2
3
4
5
6
7
8
9
<?php

namespace MySamplePackage\Blocks;

use Supra\Package\Cms\Pages\Block\Config\BlockConfig;

class MyTextBlock extends BlockConfig
{
}

Override configureAttributes method to define block title and description.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php

namespace MySamplePackage\Blocks;

use Supra\Package\Cms\Pages\Block\Config\BlockConfig;

class MyTextBlock extends BlockConfig
{
    public function configureAttributes(AttributeMapper $mapper)
    {
        $mapper->title('My Text Block')
            ->description('This block provides you WYSIWYG editor.');
    }
}

Create Block Template

Create Twig file named my_text_block.html.twig in Resources/view/blocks/ directory with the following code in there:

1
<div>{{ property('my_content', 'html') }}</div>

This will dynamically create block property named 'my_content' and link CMS WYSIWYG editor to that property.

Note

You may override template name by calling $mapper->template('MyPackage:path/to/file.html.twig') inside BlockConfig::configureAttributes() method.

Register Your Block in CMS

The last but not least step is register the block with CMS. If your package extends AbstractSupraCmsPackage, then just override getBlocks method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php

namespace MySamplePackage;

use Supra\Package\Cms\AbstractSupraCmsPackage;

class MySamplePackage extends AbstractSupraCmsPackage
{
    ...

    public function getBlocks()
    {
        return array(new Blocks\MyTextBlock());
    }
}

Otherwise, this can be done by calling BlockCollection::addConfig() on package initialization finish.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

namespace MySamplePackage;

use Supra\Core\Package\AbstractSupraPackage;

class MySamplePackage extends AbstractSupraPackage
{
    ...

    public function finish(ContainerInterface $container)
    {
        $blockCollection = $container['cms.pages.blocks.collection'];
        /* @var $blockCollection \Supra\Package\Cms\Pages\Block\BlockCollection */

        $blockCollection->addConfig(new MyTextBlock(), $this);
    }
}

That’s all. Your block is now registered and should appear in site block list.