Search code examples
imagesymfonypreviewsonata-adminsymfony-sonata

Add preview image in form sonata admin of symfony


I'm try to add a preview of a image field in edit form:

/**
* @ORM\Column(name="firma",type="string", length=500,nullable=true)
*/
private $image;

and de Profile:

public function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('image',null,array(
                'label'=>'forms.labels.firma'
            ))

In the show method it's simple, create template, but i don't found the solution for this, Thanks!

Sonata Admin Bundle Version: 2.0


Solution

  • You can override the base_edit.html template of SonataAdmin. To do this you have to add in your entity Admin:

    public function getEditTemplate()
    {
        return 'YourBundle:Sonata:base_edit.html.twig';
    }
    

    Then, copy in your new template, the same of the original base_edit.html template. The place where the form is printed is:

    {% for name, form_group in admin.formgroups %}
        <fieldset {% if form_group.collapsed %}class="sonata-ba-fielset-collapsed"{% endif %}>
            <legend>
                {% if form_group.collapsed %}
                    <a href="" class="sonata-ba-collapsed" title="{% trans from 'SonataAdminBundle' %}link_expand{% endtrans %}">{{ name|trans({}, admin.translationdomain) }}</a>
                {% else %}
                    {{ name|trans({}, admin.translationdomain) }}
                {% endif %}
            </legend>
    
            <div class="sonata-ba-collapsed-fields">
                {% for field_name in form_group.fields %}
                    {% if admin.formfielddescriptions[field_name] is defined %}
                        {{ form_row(form[field_name])}}
                    {% endif %}
                {% endfor %}
            </div>
        </fieldset>
    {% endfor %}
    

    Then, you can add your own code inside the form. The variable name is the name of the each legend of the form, so you can put your own code and the legend you want ;)