如何在sonata admin bundle后端中列出图像

时间:2014-06-23 11:47:11

标签: php symfony doctrine-orm doctrine sonata-admin

我想在列表区域中列出我的图片缩略图,我不知道该怎么做,任何人都可以帮助我。我的后端有我的标题。我正在使用Sonata adim bundle并遵循其官方文档。

这是我的imageAdmin.php

namespace swaam\ImageUploaderBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class ImageAdmin extends Admin
{

// Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('caption', 'ckeditor', array('label' => 'Ca   ption'))
//            ->add('file', 'entity', array('class' => 'swaam\ImageUploaderBundle\Entity\image'))
            ->add('file', 'file', array('data_class' => 'Symfony\Component\HttpFoundation\File\File'
            ,'property_path' => 'file'
            ))

            ->end(); //if no type is specified, SonataAdminBundle tries to guess it
        ;
    }

// Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('caption')
            ->add('thumbpath')
        ;
    }

// Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('caption')
//            ->add('thumbpath')
              ->add('thumbpath')
        ;
    }
}

这是我更新后的configureListFields方法

 protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
//            ->addIdentifier('caption')
//            ->add('thumbpath')
//              ->add('thumbpath')
            ->add('thumbpath', null, array('template' => 'swwamImageUploaderBundle:Admin:list_image.html.twig'))
//            ->add('thumbpath', null, array('template' => 'swwamImageUploaderBundle:Admin:list_image.html.twig'))

        ;
    }

这是我的树枝

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

{% block field%}

    <div>
        {#<img src="{{ image.webPath | imagine_filter('gallery_element_admin') }}" />#}

        //<img src="{{ app.request.scheme ~ '://' ~ app.request.host ~ '/' ~  image.getthumbWebPath }}" />
        {# or whatever to create src of image #}

    </div>
{% endblock %}

2 个答案:

答案 0 :(得分:2)

最简单的方法是创建自定义管理字段模板。 在 configureListFields 方法中添加:

->add('path', null, array('template' => 'AcmeBundle:Admin:list_image.html.twig'))

使用内容创建文件 AcmeBundle / Resources / views / Admin / list_image.html.twig

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

{% block field%}
    <div>
        <img src="{{ object.webPath | imagine_filter('gallery_element_admin') }}" />
        {# or whatever to create src of image #}
    </div>
{% endblock %}

答案 1 :(得分:0)

我需要类似的东西,这是我在搜索互联网一段时间后的结果。

我在admin class中的configureListFields方法中添加了tbe模板:

        ->add('culture', "string", array(
            'template' => 'AEWBackendBundle:CRUD:list_culture_image.html.twig'
        ))

我在视图中创建了模板:

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

{% block field%}
    <div>
        {% set flag = web_path ~ 'images/flags/' ~ object.culture.name(object) ~ '.png' %}
        {% if file_exists(flag) %}
            <img id="culture_image" title="{{ object.culture }}" src="/images/flags/{{ object.culture.name }}.png" height="15" />
        {% else %}
            <img id ="culture_image" title="en" src="/images/flags/default.png" height="15" />
        {% endif  %}
    </div>
{% endblock %}

在parameters.yml中:

web_path: %kernel.root_dir%/../web/

在config.yml中:

twig:
    globals:
        web_path: %web_path%

创建TwigExtension.php文件以使用file_exists PHP函数作为树枝扩展名

<?php

namespace AEW\FrontendBundle\Services\Extension;

use \Twig_Filter_Function;
use \Twig_Filter_Method;

class TwigExtension extends \Twig_Extension
{

    /**
     * Return the functions registered as twig extensions
     * 
     * @return array
     */
    public function getFunctions()
    {
        return array(
            'file_exists' => new \Twig_Function_Function('file_exists'),
        );
    }

    public function getName()
    {
        return 'twig_extension';
    }
}

在config.yml中为新扩展添加了配置行:

parameters:
    aew.twig_extension.class: AEW\FrontendBundle\Services\Extension\TwigExtension

services:
    aew.twig_extension:
        class: %aew.twig_extension.class%
        tags:
            - { name: twig.extension }

希望这有助于您或将来寻找此信息的任何其他人。