Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

symfony - Add custom button to edit page of sonata admin bundle

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(SonataAdminBundleRouteRouteCollection $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 %}
                    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...