Add custom button to edit page of sonata admin bundle

16,750

You should hint the parameter if the file is in other namespace, and the add() method should work, but then you have to overwrite the Sonata's CRUD template to be able to display an other button/link.
Additionally you can define the controller and action which will be called.

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

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

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

/**
    @Route("/dummy/{id}", name="dummy",
        requirements={"id" = "\d+"}
    )
    @Template("AcmeDemoBundle:Default:dummy.html.twig")
*/
public function dummyAction($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('dummy') %}
                        <a class="btn" target="_blank" href="{{ admin.generateObjectUrl('dummy', object) }}">{% trans from 'SonataAdminBundle' %}link_dummy{% endtrans %}</a>
                    {% endif %}
                    ...
Share:
16,750

Related videos on Youtube

alpcanaydin
Author by

alpcanaydin

I'm a developer who loves PHP and Python!

Updated on September 14, 2022

Comments

  • alpcanaydin
    alpcanaydin about 1 year

    As you know, sonata admin bundle comes with three buttons in edit page which are "Add new, update and delete". I can remove delete button with this:

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection
            ->remove('delete')
        ;
    
    }
    

    But I want to also add "Send message to User" button in edit of UserAdmin. How can I do this? I can't find any documentation about that in sonata docs.

  • GBRocks
    GBRocks about 10 years
    is it possible to do the same for embedded admins?
  • Lionel
    Lionel about 9 years
    can't we override the "app/Resources/SonataAdminBundle/views/CRUD/base_edit_form.h‌​tml.twig" to avoid to change the sonata original files?
  • wtfzdotnet
    wtfzdotnet about 8 years
    @Lionel by placing files in app/Resources you are overriding them :)