0

I'm using Symfony 4.1.1 and Sonata Admin Bundle 3.35.2.

I want to use a custom template for a field in an admin's list view. The template is ignored. I am using Twig as my templating engine.

In the admin:

# /src/Admin/ImageAdmin.php

protected function configureListFields(ListMapper $listMapper) {
    $listMapper
        ->add('filename', 'string', ['template' => 'list_image.html.twig'])
    ;
}

The template:

# /templates/list_image.html.twig

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

{% block field %}
    <img src="{{ value }}" style="width: 200px" />
{% endblock %}
amacrobert
  • 2,707
  • 2
  • 28
  • 37

3 Answers3

0

Should be

# /templates/list_image.html.twig

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

{% block field %}
 <div>
    <img src="{{ object.filename }}" style="width: 200px" />
 </div>
{% endblock %}

SRC will be just filename - not full path for file, so image will not be printed. Fix that problem also.

The other problem is that, you accesed some mystical value? I don't see where you assign value to it.

You can access getters of object by writing object.fieldname. This one works as printing getter function of your current object.

revengeance
  • 851
  • 7
  • 21
  • The problem is that my template is not used at all (although thanks for catching what would be the next error). Sonata's base_list_field.html.twig is rendered, but not my custom list_image.html.twig. I think it may be an issue with the template location, because if I use a bogus template name it still renders base_list_field instead with no errors. – amacrobert Jul 02 '18 at 01:12
0

I've had same problem (Symfony 4.1), try solution from here Use custom column in Sonata Admin list so change:

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

to:

{% extends '@SonataAdmin/CRUD/base_list_field.html.twig' %}

it did work for me. The problem is that even if your location is right (i got to it after some experiments) and you extend the wrong template you wont get any error.

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
Tom St
  • 908
  • 8
  • 15
0

Might be a bit late but for everyone who comes across with the same problem like me - I´ve solved it by creating a specific path within twig.yaml for admin-templates, simply create a subfolder _admin or try to use 'templates/': 'admin' to keep your files where they are (haven't tested this possibility)

# /config/packages/twig.yaml
twig:
    paths:
        'templates/_admin/': 'admin'


# /src/Admin/ImageAdmin.php
protected function configureListFields(ListMapper $listMapper) {
    $listMapper
        ->add('filename', 'string', ['template' => '@admin/list_image.html.twig'])
    ;
}
Nurickan
  • 196
  • 2
  • 7