List Images in sonata admin bundle

会有一股神秘感。 提交于 2019-12-06 09:49:53

问题


I am new to Symfony & trying to list images in sonata admin bundle. But i am bit confuse how to get an object in twig file so that i can get my exact path for image source.

here is my code

protected function configureListFields(ListMapper $listMapper)
    {
//        $image = $this->getSubject();
        $listMapper
            ->addIdentifier('caption')
                ->add('image','string', array('template' => 'swaamImageUploaderBundle:Admin:list_image.html.twig'))
        ;
    }

and here is my list_image.html.twig file

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field%}

    <img src="{{ 'uploads/images/822b23a922f43bb664cb58ca57de6cccccc962e5.jpeg'}}">

     {#<img src="{{  asset(image.getthumbWebPath) }}">#}


{% endblock %}

in my image source tag i have given a hard code path for my testing. but don't know how to get path from db.

plus, when i write only ->add('image') in controller i get my exact path from db displayed in back end.

I have an entity image.

any one who can help me ?


回答1:


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



来源:https://stackoverflow.com/questions/24381179/list-images-in-sonata-admin-bundle

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