Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables to sonata admin custom list action?

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.

like image 633
wilsonrufus Avatar asked Mar 11 '14 19:03

wilsonrufus


2 Answers

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) }}
like image 164
Michall Avatar answered Nov 14 '22 21:11

Michall


class MediaAdmin extends Admin

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

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

{% block field %}
    {{field_description.options.data}}
{% endblock %}
like image 23
Azam Alvi Avatar answered Nov 14 '22 23:11

Azam Alvi