Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get entity or pass variable to Symfony2 twig form widget?

In my edit.html.twig I have:

{% form_theme edit_form 'MyBundle:Entity:form.html.twig' %}
{% set img_src = asset('120x100.jpg') %}
{{ dump(img_src) }}
{{ dump(entity) }}
{{ form_widget(edit_form, {'form_type': 'horizontal', 'img_src': img_src }) }}

There I have img_src and entity dumped with no problem.
In form.html.twig I have:

{% extends 'MyBundle:Form:bootstrap.html.twig' %}
{% block _entity_field_widget %}
    {{ dump(img_src) }}
    {{ dump(entity) }}
    {% set type = 'hidden' %}
    {{ block('form_widget_simple') }}
{% endblock _channel_media_widget %}

bootstrap.html.twig is just a bootstraped *form_div_layout.html.twig*
And in that widget I have no img_src nor entity.
Any ideas how to get entity in widget? Should it be passed to form widget or is there another way? What am I doing wrong?

like image 321
Karmalakas Avatar asked Feb 08 '13 07:02

Karmalakas


1 Answers

Each symfony form type extents AbstractType class.

AbstactType class has method:

public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->set('img_src', '120x100.jpg');
    $view->set('my_variable', $foo);

}

You can create this method on your form type and next in your twig:

{{ asset(img_src) }}
like image 153
Daniel Korsak Avatar answered Sep 28 '22 01:09

Daniel Korsak