Making your hookables configurable

Sometimes when you are creating a bundle or a reusable template for different hookables, you might want to provide a way to adjust it to a given context. Thanks to the configuration data bag, you are able to achieve it easily.

While using a hookable template, you can access configuration keys via hookable_metadata Twig var using hookable_metadata.configuration.<key_name> or get_hookable_configuration().<key_name>.

Example

index.html.twig
{#
 # we assume there is a `form` variable holding a `FormView` instance passed
 # from the controller
 #}

<div class="container">
    {{ form_start(form) }}
        {{ form_errors(form) }}
        {{ form_widget(form._token) }}
    
        {% raw %}
{% hook 'index.form' with { form } %}
{% endraw %}
    {{ form_end(form, {render_rest: false}) }}
</div>
generic_field.html.twig
<div class="{{ hookable_metadata.configuration.attr.class|default("field) }}">
     {{ form_row(
          hookable_metadata.context.form[hookable_metadata.configuration.field_name]
     ) }}
</div>
twig_hooks.yaml
sylius_twig_hooks:
    hooks:
        'index.form':
            name:
                template: 'generic_field.html.twig'
                configuration:
                    field_name: 'name'
                    attr:
                        class: 'field special-field'

Last updated