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

梦想与她 提交于 2019-12-06 05:54:17

问题


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.


回答1:


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




回答2:


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'))



来源:https://stackoverflow.com/questions/28361347/sonata-admin-bundle-preview-image-from-some-entity-in-list-mapper-without-sonata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!