dongpao2871 2014-06-24 07:50
浏览 41
已采纳

在sonata管理包中列出图像

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条回答 默认 最新

  • douran7929 2014-06-24 08:37
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)