I want to define a new twig function "form_field" which renders form_label
, form_widget
and form_errors
.
I don't want to override form_row
because I already did that for a different purpose: form_row
also wraps <div/>
container required for my layout. Specifically, I add bootstrap classes.
Why do I need this? Normally I use form_row
but I have lots of form rows for which I want to place two ore more fields in the same line (e.g. "first name" and "last name"). In those cases I define my wrapping <div/>
manually, but I don't want to write the three function calls (form_label
, form_widget
and form_errors
) everytime.
I tried to specify a custom function, but I get an exception when calling the twig functions there. For this solution I need to know, how to call Twig functions from a custom function.
My second attempt was to adapt Symfony's solution for its own form helpers. I found this line in vendor/symfony/symfony/src/Symfony/Bridge/Twig/Extension/FormExtension.php:
new \Twig_SimpleFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
Looks like a function which actually just renders a block. When I adapted that line in my AppBundle, an exception was thrown in which Symfony claimed that a block named "form_field" is undefined. However, I defined this block in my custom form template:
{% use 'bootstrap_3_horizontal_layout.html.twig' %}
{%- block form_field -%}
{{- form_label(form) -}}
{{- form_widget(form) -}}
{{- form_errors(form) -}}
{%- endblock form_field -%}
<div ...>form_field(form.firstName)</div><div ...>form_field(form.lastName)</div>
– fishbone