1

我正在尝试修改 SonataAdmin 模板。我有一个具有路径属性的图像实体。我创建了一个 ImageAdmin 类,并将其集成到 sonataAdmin 中。我想修改 admin-list-view 以将路径包装在 img 标签中,以便实际显示图像。有谁知道我该怎么做?

谢谢!

4

2 回答 2

5

有两种方法可以使用您自己的模板。

在配置文件中:

sonata_doctrine_orm_admin:
    entity_manager: 

    templates:
        form:
            - SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig
        filter:
            - SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig
        types:
            list:
               ...

            show:
                ...
                image: YourBundle:YourFolder:yourtemplate.html.twig

并在字段定义文件中:

<?php
            namespace ...;

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

            class ImageAdmin extends Admin
            {
                protected function configureShowField(ShowMapper $showMapper)
                {
                    $showMapper
                        ...
                        ->add('image', 'image')
                        ...
                    ;
                }
            }
        ?>

或者第二种方式:

    <?php
        namespace ...;

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

        class ImageAdmin extends Admin
        {
            protected function configureShowField(ShowMapper $showMapper)
            {
                $showMapper
                    ...
                    ->add('image', 'string', array('template' => 'YourBundle:YourFolder:yourtemplate.html.twig'))
                    ...
                ;
            }
        }
    ?>

然后将下面的代码复制到您的模板中:

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


{% block field %}
    <img src="{{ asset('uploads/media/') }}{{ value|nl2br }}"/>
{% endblock %}
于 2012-11-10T03:34:50.923 回答
1

与 HuyVu 相同,但我将其用于自定义模板

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

{% block field %}
    {% thumbnail value, 'small' %}
{% endblock %}
于 2013-12-26T22:42:39.320 回答