列出奏鸣曲管理包中的图像 [英] List Images in sonata admin bundle

查看:25
本文介绍了列出奏鸣曲管理包中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Symfony 的新手 &试图在奏鸣曲管理包中列出图像.但是我有点困惑如何在树枝文件中获取对象,以便我可以获得图像源的确切路径.

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.

这是我的代码

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

这是我的 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 %}

在我的图像源标签中,我为我的测试提供了一个硬代码路径.但不知道如何从db获取路径.

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.

另外,当我只在控制器中写入 ->add('image') 时,我会从后端显示的 db 获取我的确切路径.

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

我有一个实体图片.

谁能帮帮我?

推荐答案

我想你正在使用 Sonata Media Bundle

I suppose you're using Sonata Media Bundle

您必须使用实体,您可以在此处找到所需的助手:http://sonata-project.org/bundles/media/2-2/doc/reference/helpers.html

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

如果您想将图像添加到 Sonata Admin(您的情况)的列表字段中,您可以使用此代码

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 %}

其中 image 是实体的图像 getter 方法(es getImage() )

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

这篇关于列出奏鸣曲管理包中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆