Sylius Stack
  • Sylius Stack Documentation
  • Getting started
  • Cookbook
    • How to customize your admin panel
      • Basic operations
      • Customizing your grids
      • Customizing the logo
      • Customizing the menu
      • Configuring the security access
      • Customizing the page titles
    • How to use in a DDD architecture
      • Architecture overview
      • Resource configuration
      • Basic operations
      • Operation using a grid
  • Admin UI
    • Getting started
  • Bootstrap Admin UI
    • Getting started
  • Resource
    • Resource Bundle documentation
      • Installation
      • Create new resource
      • Configure your resource
      • Configure your operations
      • Validation
      • Redirect
      • Resource factories
      • Providers
      • Processors
      • Responders
      • Legacy Resource Documentation
        • Configuration
        • Services
        • Routing
        • Forms
        • Getting a Single Resource
        • Getting a Collection of Resources
        • Creating Resources
        • Updating Resources
        • Deleting Resources
        • Configuring a state machine
        • Configuration Reference
  • Grid
    • Grid Bundle documentation
      • Installation
      • Creating your first grid
      • Configuring Fields
      • Field types
      • Creating a custom Field Type
      • Creating a custom Action
      • Creating a custom Bulk Action
      • Filters
      • Creating a custom Filter
      • Advanced configuration
      • Configuration Reference
  • 🍀Twig Extra
    • Getting started
  • 🌱Twig Hooks
    • Getting started
    • Passing data to your hookables
    • Making your hookables configurable
    • Autoprefixing feature
    • Composable Layouts with a predictable structure
    • Advanced
      • Ergonomic work with hooks
      • Metadata objects
      • Multiple hooks inside a single template
      • Overriding hookables
Powered by GitBook
On this page
  1. Twig Hooks

Passing data to your hookables

PreviousGetting startedNextMaking your hookables configurable

Last updated 1 year ago

One of the most powerful aspects of hooks & hookables is an ability to pass their data down to the children. We can have two sources of the context data:

  • Hook-level defined data

  • Hookable-level defined data

The context data from these two sources are merged and passed with the metadata to the hookable template or component, so we can use them.

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) }}
    
        {% hook 'index.form' with { form } %}
    {{ form_end(form, {render_rest: false}) }}
</div>

So, as we see at line 8 we define the index.form hook. But also, we pass the form with using the with keyword. Thanks to it, we are able to pass multiple data to hookables that will hook into the index.form hook.

with { form } is a short-hand for with { form: form }, so the key for our FormView in the context data bag will be form.

Now let's create a Twig template rendering some field from our form, and let's make it a hookable.

index/some_field.html.twig
<div class="field">
     {{ form_row(hookable_metadata.context.form.some_field) }}
     
     {# we can also write it like #}
     
     {% set context = hookable_metadata.context %}
     
     {{ form_row(context.form.some_field) }}
     
     {# or #}
     
     {% set context = get_hookable_context() %}
     
     {{ form_row(context.form.some_field) }}
</div>

You can access the context data in multiple ways, so you can pick the one you like the most. Available options are:

  • getting it directly from the hookable_metadata object like hookable_metadata.context.<data_key>

  • getting the context data bag via the Twig function like get_hookable_context().<data_key>

Sometimes you might want to override some data that are defined at the hook-level. It is possible by defining the same context data key on the hookable level. If the same context data key is defined at both hook-level and hookable-level the hookable-level one is used.

🌱