简体   繁体   中英

Symfony3.1 & Sonata Admin - list field's template is ignored

I use Sonata Admin 3.13 with Symfony 3.1 and want to display the uploaded image in the listview. I have a PaintingAdmin with the following ListFields:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name', null, ['label' => 'Name'])
        ->add('category', null, ['label' => 'Kategorie'])
        ->add('size', null, ['label' => 'Größe'])
        ->add('imageFilename', null, [
            'template' => 'sonata:imagepreview.html.twig',
            'label' => 'Bild'
        ]);
    ;
}

And a template at app/Resources/views/sonata/imagepreview.html.twig

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
    <div>
        {% if object.imageFilename != null %}
            <img src="{{ asset('uploads/images/' ~ object.imageFilename) }}" class="img-responsive" />
        {% else %}
            <div class="warn">Kein Bild</div>
        {% endif %}
    </div>
{% endblock %}

but the template is completly ignored, it shows only the value imageFilename . Everything else works fine (ie the label is shown as Bild )

You need to tell Sonata that you are using a custom template.

You can do it in your admin service declaration:

librinfo_crm.admin.organism:
        class: Librinfo\CRMBundle\Admin\OrganismAdmin
        arguments: [~, Librinfo\CRMBundle\Entity\Organism, LibrinfoCRMBundle:OrganismAdmin]
        tags:
            -   name: sonata.admin
                manager_type: orm
                group: Customers Relationship Management
                label: librinfo.crm.organism_admin.label
                label_translator_strategy: blast_core.label.strategy.librinfo
        calls:
            - [ setTemplate, [list, LibrinfoCRMBundle:OrganismAdmin:list.html.twig]]

or you can override the $templates array() of your admin class.

If you want to use 'classic' symfony template inheritance your custom template should have the same path ant name than the original one so if you are trying to replace SonataAdminBundle:CRUD:base_list_field.html.twig your custom template should be in app/Resources/view/CRUD/base_list_field.html.twig

I suggest that you use full path to template: app/Resources/views/sonata/imagepreview.html.twig

    ->add('imageFilename', null, [
        'template' => 'sonata\imagepreview.html.twig',
        'label' => 'Bild'
    ]);

So, as you can sonata\\imagepreview.html.twig is relative to app/Resources/views/ folder.

In symfony of version < 4.x we have two ways of placing the twig tempates:

  1. Inside the bundle

     RealPath: `src\\AppBundle\\Resources\\views\\MyCustomFolder\\my_file.html.twig` Path: `AppBundle::MyCustomFolder\\my_file.html.twig` 
  2. Outside the bundle in app folder

     RealPath: `app\\Resources\\views\\MyCustomFolder\\my_file.html.twig` Path: `MyCustomFolder\\my_file.html.twig` 

You can read more in Official Symfony documantation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM