Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new Button in the List view of sonata admin Bundle

Maybe the question is too easy to answer, but I searched in the documentation of sonata admin bundle and i didn't find what I need. When you navigate to the List view of a model in sonata admin Bundle you find the Action Button in the upper right and under it you find the add new action. In my case I need that the Add new action to be displayed directly In the View Like in this screenshot : enter image description here

Any one can help me please ?

like image 423
Alouini Khalil Avatar asked May 27 '15 09:05

Alouini Khalil


1 Answers

I know this is an old question, but I'll leave this answer for future reference.

I had to do something similar, this is how I did it.

In your admin class override the configureActionButtons() method

class YourAdmin extends AbstractAdmin
{
    //...
    /**
     * Overriden from (AbstractAdmin)
     */
    public function configureActionButtons($action, $object = null)
    {
        $list = parent::configureActionButtons($action,$object);

        $list['custom_action'] = array(
                'template' =>  'AcmeBundle:YourAdmin:custom_button.html.twig',
        );

        return $list;
    }
    //...

}

And then creating your button in AcmeBundle/Resources/views/YourAdmin/custom_button.html.twig

{# custom_button.html.twig #}    
<a class="sonata-action-element" href="{{ admin.generateUrl('your_route') }}">
   <i class="fa fa-plus-circle"></i>Custom Action
</a>

Of course you can add permissions check afterwards (they were omitted for clarity)

Hope it helps somebody

like image 175
Alex Mantaut Avatar answered Sep 25 '22 01:09

Alex Mantaut