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 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题