How to pass variables to sonata admin custom list action?

wilsonrufus picture wilsonrufus · Mar 11, 2014 · Viewed 10.4k times · Source

How would I pass an array of variables to SonataMediaBundle:MediaAdmin:list_image.html.twig?
There does not seem to be any option available.

class MediaAdmin extends Admin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('image', 'string', array(
                'template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig',
                array('data'=>'something')<<- This is not working
            ))
            ->add('custom', 'string', array(
                'template' => 'SonataMediaBundle:MediaAdmin:list_custom.html.twig'
            ))
        ;
    }
}

Template:

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

{% block field%}
    {{dump(data)}}     <<- This is not working
{% endblock %}

I just want to pass a variable value to SonataMediaBundle:MediaAdmin:list_image.html.twig.

Answer

Michall picture Michall · Sep 11, 2014

Just elaborating Tigran's answer.

class MediaAdmin extends Admin
{
    public $data = '';

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('custom', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_custom.html.twig'))
        ;
        $this->data = 'something';
    }
}

and in your template:

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

{% block field %}
    <div>
        {{ admin.data }}
    </div>
{% endblock %}

and if you have an array indexed by object's id, you can access it this way:

  {{ attribute( admin.data, object.id) }}