Symfony: Sonata Media Preview

I am working on a symfony project with sonata packages (admin and media), I need to display the file file -PDF file (link to download the file or link to display it in a new tab. I searched a lot without a good solution. This is fild in configureListFields:

        ->add('cv', null, array('template' => 'admin:list_image.html.twig'))

this is the template 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 %}

any help please, thanks in advance!

+4
source share
1 answer

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 %}

Hope this helps!

0
source

Source: https://habr.com/ru/post/1679321/


All Articles