Symfony SonataAdmin Templates

元气小坏坏 提交于 2019-12-09 23:34:02

问题


I am trying to modify the SonataAdmin templates. I have an Image entity that has a path property. I created an ImageAdmin class and this is integrated into sonataAdmin. I would like to modify the admin-list-view to wrap the path in an img tag so that the image is actually displayed. Does anyone know how I can do this?

Thanks!


回答1:


There are 2 ways to use your own templates.

In the config file:

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

and in the field definition file:

<?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')
                        ...
                    ;
                }
            }
        ?>

OR the 2nd way:

    <?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'))
                    ...
                ;
            }
        }
    ?>

And then copy the code below to your template:

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


{% block field %}
    <img src="{{ asset('uploads/media/') }}{{ value|nl2br }}"/>
{% endblock %}



回答2:


Same as HuyVu But I use this for the custom template

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

{% block field %}
    {% thumbnail value, 'small' %}
{% endblock %}


来源:https://stackoverflow.com/questions/10254316/symfony-sonataadmin-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!