Content element

Focusing on the main goal for Toolkit there is an easy way to create lightweight decoupled content elements.


If you’re developing for Contao 4.5 you might look at the `fragments support`_. If you have to stay on Contao LTS 4.4 you maybe want to read further.

Interface ContentElement

The only requirement for an content element supported by Toolkit is that the interface ContentElement is implemented. It extends the Component interface.

The Main difference between Contao content element and the interface is that the data attributes has to be accessible by the explicit get and set methods. Magic __get and __set are not recommended.

Register a content element

Since Toolkit supports dependency injection for content elements you have to create factories which creates them. A factory has to implement the ComponentFactory interface and must be registered as a tagged service.

Provided tags:

Tag your factory service with this tag so that toolkit will use it to create the elements.
Tag your factory with this tag to provide information about the supported content elements. You have to define the category and type attribute as well.

// src/ExampleFactory.php
class ExampleFactory implements Netzmacht\Contao\Toolkit\Component\ComponentFactory
    // ...

// src/Example.php
class Example implements Netzmacht\Contao\Toolkit\Component\ContentElement\ContentElement
    // ...
// services.yml
    class: ExampleFactory
        - { name: 'netzmacht.contao_toolkit.component.content_element', category: 'texts', type: 'example' }


It’s possible to create multiple types with a factory. Just add multiple tags.

You don’t have to register you content element in the config.php. Toolkit will do it for you.

Extending AbstractContentElement

To simplify creating a new content element implementing the provided interface you can use the AbstractContentElement which itself is a subclass of AbstractComponent.

There are several extension points where you can hook customize the behaviour. Some of the methods are just empty placeholders which doesn’t have to be called when being overriden.

The name of the template. Same as strTemplate in Contao
deserializeData(array $row)
Method deserialize the given raw data coming form the database entry or model. You should call the parent method when overriding this one. Deserialization of the headline is done here.
Is called to decide if content element should be generated. You should call the parent to keep the default behaviour.
Is called before the template data are prepared.
prepareTemplateData(array $data)
Prepares the data which are passed to the template.
Is triggered after the content element is parsed.