List Images in sonata admin bundle

梦想的初衷 提交于 2019-12-04 16:50:56

I suppose you're using Sonata Media Bundle

you must use the entity and you can find needed helpers here: http://sonata-project.org/bundles/media/2-2/doc/reference/helpers.html

If you need to get the full path in controller (can happen, very rarely but can happen) you have to use the media service

$mm = $this->container->get('sonata.media.manager.media');
$pr = $this->container->get('sonata.media.provider.image');
$media = $mm->findOneBy(array('id' => $idImage));
$format = $pr->getFormatName($media, 'default');
$path = $pr->generatePublicUrl($media, $format);

or use the twig helper inside the controller

$format = 'default'; //the format you want to show
$path=$this->get('sonata.media.twig.extension')->path($image, $format);

if you want to add the image to the list field of Sonata Admin (your case) you can use this code

{% block field %}

        {% thumbnail object.image, 'thumb' %}

{% endblock %}

where image is the image getter method of your entity (es getImage() )

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