Sacred Socks
Sacred Socks

Reputation: 402

Symfony2 & Twig - theming file upload widget

I have an Symfony2 edit form for an entity and one of the fields is for the user's profile picture.

I'm trying to theme the Twig file upload widget so that the current picture set for the field is displayed above the file input.

So far, I have:

{% form_theme edit_form _self %}
{% block field_widget %}
    {% set type = type|default('text') %}

    {% if type == 'file' %}
        <img src="{{ value }}" /> 
    {% endif %}

    {{ block('form_widget_simple') }}
{% endblock %}

All works well except that the value variable is blank (which makes sense I guess).

My question is how can I get hold of the path to the file? Is there a way to pick it out of the form values for the field? Could I perhaps pass it through as an option to the field?

Srz if this is a dumb question, still pretty new to Symfony and Twig..

Upvotes: 8

Views: 11423

Answers (3)

Baishu
Baishu

Reputation: 1591

Just pointing to the new url of the solution in a more obvious way :

http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html

Upvotes: 3

Richard P&#233;rez
Richard P&#233;rez

Reputation: 1487

A better way to customize the file widget will be

{% block file_widget %}
    {% spaceless %}
     //Your custom content here
    {% endspaceless %}
{% endblock file_widget %}

For example:

{% block file_widget %}
    {% spaceless %}
     <td>{% set type = type|default('file') %}
         <input type="{{ type }}" {{ block('widget_attributes') }} />
     </td>
    {% endspaceless %}
{% endblock file_widget %}

Remember that "{% spaceless %}" is very important

Upvotes: 5

Sacred Socks
Sacred Socks

Reputation: 402

So it looks I'm terrible at googling for Symfony stuff. I eventually found this part of the Symfony cookbook: http://symfony.com/doc/master/cookbook/form/form_customization.html

Symfony2 calls it a Form Type Extension and they actually use adding a image tag to the file widget as their example.

Upvotes: 0

Related Questions