Rectangle 27 0

php How to get images listed in sonata admin bundle backend?


->add('path', null, array('template' => 'AcmeBundle:Admin:list_image.html.twig'))
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field%}
    <div>
        <img src="{{ object.webPath | imagine_filter('gallery_element_admin') }}" />
        {# or whatever to create src of image #}
    </div>
{% endblock %}

The simplest way is to create custom admin field template. In configureListFields method add:

View page source, find image tag and check image src address (try to open) or <img> tag is missing? You should also notice {# #} is a commnet in twig.

extremely sorry , but as i am new to symfony even php, still not sure how to get make path in twig, can u please show me any stuff example. i am searching but unable to get any solution.

i have updated the code, with your code, and i didn;t get any error, still i didn't get any picture there.

thank you.. i followed the same , but didn't work for me !!

what is the result / error?

Note
Rectangle 27 0

php How to get images listed in sonata admin bundle backend?


->add('culture', "string", array(
            'template' => 'AEWBackendBundle:CRUD:list_culture_image.html.twig'
        ))
<?php

namespace AEW\FrontendBundle\Services\Extension;

use \Twig_Filter_Function;
use \Twig_Filter_Method;

class TwigExtension extends \Twig_Extension
{

    /**
     * Return the functions registered as twig extensions
     * 
     * @return array
     */
    public function getFunctions()
    {
        return array(
            'file_exists' => new \Twig_Function_Function('file_exists'),
        );
    }

    public function getName()
    {
        return 'twig_extension';
    }
}
parameters:
    aew.twig_extension.class: AEW\FrontendBundle\Services\Extension\TwigExtension

services:
    aew.twig_extension:
        class: %aew.twig_extension.class%
        tags:
            - { name: twig.extension }
twig:
    globals:
        web_path: %web_path%
web_path: %kernel.root_dir%/../web/
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field%}
    <div>
        {% set flag = web_path ~ 'images/flags/' ~ object.culture.name(object) ~ '.png' %}
        {% if file_exists(flag) %}
            <img id="culture_image" title="{{ object.culture }}" src="/images/flags/{{ object.culture.name }}.png" height="15" />
        {% else %}
            <img id ="culture_image" title="en" src="/images/flags/default.png" height="15" />
        {% endif  %}
    </div>
{% endblock %}

Added the config lines in config.yml for the new extension:

Created the TwigExtension.php file to use the file_exists PHP function as a twig extension

Hope this helps you or anyone else who is looking for this info in the future.

I have added tbe template in the configureListFields method from the admin class:

I have created the template in the view:

I needed something similar and this is my result after a while of searching the internet.

Note