Add custom button to edit page of sonata admin bundle

前端 未结 1 1345
广开言路
广开言路 2020-12-16 03:59

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:

protec         


        
1条回答
  •  星月不相逢
    2020-12-16 04:11

    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') %}
                            {% trans from 'SonataAdminBundle' %}link_dummy{% endtrans %}
                        {% endif %}
                        ...
    

    0 讨论(0)
提交回复
热议问题