Skip to content

Field Type template

Defining your Field Type template

You need to define a template containing a block dedicated to the Field display in order to be used by ez_render_field() Twig helper. Only with it it can be correctly displayed.

This block consists of a piece of template receiving specific variables you can use to make the display vary.

You will find examples with built-in Field Types in EzPublishCoreBundle/Resources/views/content_fields.html.twig

Template for a Field Type with myfieldtype identifier

1
2
3
{% block myfieldtype_field %}
{# Your code here #}
{% endblock %}

By convention, your block must be named <fieldTypeIdentifier>_field.

Exposed variables

Name Type Description
field eZ\Publish\API\Repository\Values\Content\Field The field to display
contentInfo eZ\Publish\API\Repository\Values\Content\ContentInfo The ContentInfo to which the field belongs to
versionInfo eZ\Publish\API\Repository\Values\Content\VersionInfo The VersionInfo to which the field belongs to
fieldSettings mixed Settings of the field (depends on the Field Type)
parameters hash Options passed to ez_render_field() under the parameters key
attr hash The attributes to add the generate the HTML markup. Contains at least a class entry, containing -field

Reusing blocks

To ease Field Type template development, you can take advantage of all defined blocks by using the block() function.

You can for example use simple_block_field, simple_inline_field or field_attributes blocks provided in content_fields.html.twig.

Caution

To be able to reuse built-in blocks, your template must inherit from EzPublishCoreBundle::content_fields.html.twig.

Registering your template

To make your template available, you must register it in the system.

app/config/ezplatform.yml

1
2
3
4
5
6
7
8
ezpublish:
    system:
        my_siteaccess:
            field_templates:
                -
                    template: "AcmeTestBundle:fields:my_field_template.html.twig"
                    # Priority is optional (default is 0). The higher it is, the higher your template gets in the list.
                    priority: 10

You can define these rules in a dedicated file instead of app/config/ezplatform.yml. Read the cookbook recipe to learn more about it.

Back Office templates

Back Office view template

You can use a separate template for previewing the Field in the Back Office by specifying:

1
2
3
4
5
ezpublish:
    systems:
        admin_group:
            field_templates:
                - { template: 'AcmeTestBundle:adminui/field:my_field_view.html.twig', priority: 10 }

Field edit template

To use a template for the Field edit form in the Back Office, you need to specify it in configuration under the twig.form_themes key:

1
2
3
twig:
    form_themes:
        - 'AcmeTestBundle:adminui/field:my_field_template.html.twig'
Read the Docs