I work on a Symfony project with sonata bundles (admin and media). I need to display a file preview - PDF file - (link to download the file or a link to display it in new tab).
I searched a lot without a good solution.
This is the field in configureListFields
:
->add('cv', null, array('template' => 'admin:list_image.html.twig'))
This is the twig
template code :
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{% if object.cv != null %}
<img src="{{ object.cv.path }}">
{% else %}
<span>No picture</span>
{% endif %}
</div>
{% endblock %}
Finally I found a solution by changing the template.
This is my admin class :
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('cv', null, array(
'template' => 'admin/list_field_cv.html.twig'
));
}
And this is my custom template:
{% extends admin.getTemplate('base_list_field') %}
{% block field %}
{% if value %}
{% set route_name = field_description.options.route.name %}
{% if not field_description.options.identifier|default(false) and
field_description.hasAssociationAdmin and
field_description.associationadmin.hasRoute(route_name) and
field_description.associationadmin.hasAccess(route_name, value) %}
<div class="btn-group">
<a class="btn btn-default btn-sm btn-block"
href="{{ field_description.associationadmin.generateObjectUrl(route_name, value, field_description.options.route.parameters) }}">
{{ value|render_relation_element(field_description) }} : to media
</a>
<a class="btn btn-info btn-sm btn-block"
href="{{ path('sonata_media_download', {'id': (object.cv.id)}) }}">
{{ value|render_relation_element(field_description) }} : Télécharger
</a>
<a class="btn btn-link btn-sm btn-block"
href="{% path object.cv,'reference' %}">
{{ value|render_relation_element(field_description) }} : Ouvrir
</a>
</div>
{% else %}
{{ value|render_relation_element(field_description) }}
{% endif %}
{% endif %}
{% endblock %}
I hope it helps.