我正在尝试修改SonataAdmin模板。我有一个具有路径属性的Image实体。我创建了一个ImageAdmin类,并将其集成到sonataAdmin中。我想修改admin-list-view以将路径包装在img标记中,以便实际显示图像。有谁知道我怎么能这样做?
谢谢!
答案 0 :(得分:4)
有两种方法可以使用自己的模板。
在配置文件中:
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')
...
;
}
}
?>
OR 第二种方式:
<?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 %}
答案 1 :(得分:1)
与HuyVu相同 但我将此用于自定义模板
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{% thumbnail value, 'small' %}
{% endblock %}