You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
3
+
One of the most powerful aspects of hooks & hookables is the ability to pass data down to children elements. We can have two sources of context data:
4
4
5
5
* Hook-level defined data
6
6
* Hookable-level defined data
7
7
8
-
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.
8
+
Context data from these two sources is merged and passed to the **hookable** template or component together with the metadata , so we can access them.
| <p>Let's assume we want to render a form in our <code>index.html.twig</code> template via a <code>form</code> variable containing a <code>FormView</code> instance.</p><p>Here, we define an <strong><code>index.form</code></strong> hook, and we can pass it the form's context data thanks to the <code>with</code> keyword.</p> |
# we assume there is a `form` variable holding a `FormView` instance passed
22
-
# from the controller
23
-
#}
24
-
25
21
<div class="container">
26
22
{{ form_start(form) }}
27
-
{{ form_errors(form) }}
28
-
{{ form_widget(form._token) }}
23
+
{{ form_errors(form }}
24
+
{{ form_widget(form._token) }}
25
+
26
+
{% hook 'index.form' with { form } %}
29
27
30
-
{% raw %}
31
-
{% hook 'index.form' with { form } %}
32
-
{% endraw %}
33
-
{{ form_end(form, {render_rest: false}) }}
28
+
{{ form_end(form, {render_rest: false} }}
34
29
</div>
35
30
```
36
31
{% endcode %}
37
32
38
-
So, as we see at `line 11` 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.
39
-
40
33
{% hint style="info" %}
41
34
`with { form }` is a short-hand for `with { form: form }`, so the key for our `FormView` in the context data bag will be `form.`
42
35
{% endhint %}
43
36
44
-
Now let's create a Twig template rendering some field from our form, and let's make it a hookable.
37
+
Now let's create a Twig template that renders a field from our form and let's make it a hookable. We have 3 possible options to do this : 
{% include "../.gitbook/includes/less-than-div-class-field-greater-than-for....md" %}
67
40
68
41
{% hint style="info" %}
69
42
You can access the context data in multiple ways, so you can pick the one you like the most. Available options are:
@@ -72,6 +45,10 @@ You can access the context data in multiple ways, so you can pick the one you li
72
45
* getting the context data bag via the Twig function like `get_hookable_context().<data_key>`
73
46
{% endhint %}
74
47
48
+
### Override behavior
49
+
50
+
When the same context data key is defined at both the **hook** and **hookable** levels, the **hookable-level** value takes precedence.
51
+
75
52
{% hint style="info" %}
76
-
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.
53
+
You can use this to override hook-level data by redefining the key at the hookable level.
0 commit comments