Skip to content

Create custom Page block

In addition to existing blocks which you can use in a Page, you can also create custom blocks.

To do this, add block configuration in a YAML file, under the ezplatform_page_fieldtype key, for example in config/packages/ezplatform_page_builder.yaml.

The following example shows how to create a block that showcases an event.

Configure block

First, add the following YAML configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ezplatform_page_fieldtype:
    blocks:
        event:
            name: Event Block
            category: Custom
            thumbnail: /bundles/ibexaplatformicons/img/all-icons.svg#date
            attributes:
                name:
                    type: text
                    name: Event name
                    validators:
                        not_blank:
                            message: Please provide a name   
                category:
                    type: select
                    name: Select a category
                    value: visual
                    options:
                        multiple: true
                        choices:
                            'Music': music
                            'Visual arts': visual
                            'Sports': sports
                event:
                    type: embed
                    name: Event
                    validators:
                        not_blank:
                            message: Please select an event

event is the internal name for the block, and name indicates the name under which the block is available in the interface. You also set up the category in the Elements panel that the block appears in. In this case, it doesn't show with the rest of the built-in blocks, but in a separate "Custom" category. The thumbnail for the block can be one of the pre-existing icons, like in the example above, or you can use a custom SVG file.

A block can have multiple attributes that you edit when adding it to a Page. In this example, you configure three attributes: name of the event, category it belongs to, and an event Content item that you select and embed.

For a list of all available attribute types, see Page block attributes.

Each attribute can have validators. The not_blank validators in the example ensure that the user fills in the two block fields.

Add block templates

A block can have different templates that you select when adding it to a Page.

To configure block templates, add them to block configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ezplatform_page_fieldtype:
    blocks:
        event:
            views:
                default:
                    template: '@ezdesign/blocks/event/template.html.twig'
                    name: Default view
                    priority: -255
                featured:
                    template: '@ezdesign/blocks/event/featured_template.html.twig'
                    name: Featured view
                    priority: 50

Provide the templates in the indicated folder, in this case in templates/themes/<your_theme>/blocks/event.

For example the featured_template.html.twig file can look like this:

1
2
3
4
5
6
<h1>{{ name }}</h1>
<p>{{ category }}</p>
{{ render(controller('ez_content::viewAction', {
    'contentId': event,
    'viewType': 'embed'
})) }}

The templates have access to all block attributes, as you can see above in the name, category and event variables.

Priority of templates indicates the order in which they are presented in Page Builder. The template with the greatest priority is used as the default one.

Add edit templates

You can also customize the template for the block settings modal. Do this under the configuration_template key:

1
2
3
4
5
6
7
ezplatform_page_fieldtype:
    blocks:
        event:
            name: Event Block
            category: Custom
            thumbnail: /bundles/ibexaplatformicons/img/all-icons.svg#date
            configuration_template: '@ezdesign/blocks/event/config.html.twig'

Place the edit template in templates/themes/<your_theme>/blocks/event/config.html.twig':

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{% extends '@EzPlatformPageBuilder/page_builder/block/config.html.twig' %}

{% block basic_tab_content %}
    <div class="ez-block-config__fields">
        {{ form_row(form.name) }}
        {% if attributes_per_category['default'] is defined %}
            <ol>
                {% for identifier in attributes_per_category['default'] %}
                    {% block config_entry %}
                        <li>
                            {{ form_row(form.attributes[identifier]) }}
                        </li>
                    {% endblock %}
                {% endfor %}
            </ol>
        {% endif %}
    </div>
{% endblock %}