简体   繁体   中英

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.

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

one separated entity, -no user-, where I have added one media, this for me worked

in admin configureListFields ->add('media', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM