Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

I was wondering how should i approach this problem. In sonata admin dashboard i have users and i would like to preview users profile pictures in thumbnails of a ListMapper. I'm new to symfony and still a bit confused trying to wrap my head around these concepts.

like image 487
3ND Avatar asked Dec 08 '22 04:12

3ND


1 Answers

You need to create a custom template where you display the image of your user, i will assume that your Entity User as a Picture Entity which has a path method that gives the image URL :

picture.html.twig

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
    {% if object.picture != null %}
    <img src="{{ object.picture.path }}">
    {% else %}
    <span>No picture</span>
    {% endif %}
</div>
{% endblock %}

You know have to use this template in your list

class UserAdmin extends Admin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('picture', null, array(
                'template' => 'ApplicationSonataAdminBundle:User:picture.html.twig'
            ));
    }
}

Documentation is available here : http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/list_field_definition.html#custom-template

like image 189
HypeR Avatar answered Dec 10 '22 23:12

HypeR