Add button in CRUD (sonata admin)

1,216 views
Skip to first unread message

Aline Macena

unread,
Nov 18, 2016, 11:11:37 AM11/18/16
to sonata-users
Hi,

I would like to add a button in screen of CRUD corresponding to edit. I am using the following:

src/Acme/DemoBundle/Admin/EntityAdmin.php:

protected function configureRoutes(\Sonata\AdminBundle\Route\RouteCollection $collection)
{
    $collection
        ->add('button_1',
            'button_1/{id}',
            array('_controller' => 'AcmeDemoBundle:Default:button_1'),
            array('id' => '\d+')
        )
    ;
}

src/Acme/HelloBundle/Controller/DefaultController.php:

/**
    @Route("/button_1/{id}", name="button_1",
requirements={"id" = "\d+"} ) @Template("AcmeDemoBundle:Default:button_1.html.twig")
*/
public function button_1Action($id)
{ return(array( 'id' => $id )); }

app/Resources/SonataAdminBundle/views/CRUD/base_edit_form.html.twig:

{% block form %}
    ...
    {% else %}
        ...
        {% block formactions %}
            ...
            {% else %}
                ...
                {% if admin.id(object) %}
                    ...
                    {% if admin.hasroute('button_1') %}
<a class="btn" target="_blank" href="{{ admin.generateObjectUrl('button_1', object) }}">{% trans from 'SonataAdminBundle' %}link_button_1{% endtrans %}</a>
{% endif %} ...

But the button doesnt't appear and the controller doesn't called. What would be the correct way to do this action, please?

Thanks,
Aline.

Aline Macena

unread,
Nov 23, 2016, 5:09:13 AM11/23/16
to sonata-users
People,

I was able to resolve the question:

1- How to add button on CRUD. 
    It´s required to copy twig from vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_edit_form.html.twig to app/Resources/SonataAdminBundle/views/CRUD. And it´s edit the following part of de code int then {% block formations %}:
         {% if admin.hasroute('<inform_name_route>') %}
    <a class="btn btn-success" href="{{ admin.generateObjectUrl('<inform_name_route>', object) }}">View</a>
    {% endif %}
2- This is route will call a controller. You must create it.

3- So that de button created becomes available a especific CRUD screen, it´s required:
    public function configureRoutes(RouteCollection $collection)
    {
         $collection->add('<inform_name_route>', '<inform_path_route>', array(
            '_controller' => '<inform_defaults_route>'
        ), array(
            '<inform_required_parameter>' => '\d+'
        ));
    }
   

Thanks,
Aline.
Reply all
Reply to author
Forward
0 new messages