Sonata Admin: Add custom triggers/actions to list/edit action

12,566

To add an action for edit form

Add to your admin class:

protected function configureSideMenu(MenuItemInterface $menu, $action, Admin $childAdmin = null)
{
    if (!$childAdmin && !in_array($action, array('edit'))) {
        return;
    }
    $admin = $this->isChild() ? $this->getParent() : $this;
    $id = $admin->getRequest()->get('id');
    $menu->addChild('My action', array('uri' => 'http://google.com?id=' . $id));
}

It will create left side menu for actions like /admin/acme/videos/x/edit/. Having id for current item allows you to build any custom URL.

To add an action for list: In your admin file add

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('_action', 'actions', array(
            'actions' => array(
                'act' => array('template' => 'AcmeBundle:Video:my_temp.html.twig'),
            )
        ))
    ;
}

It will add a column with links, then you need to create a template for your column, something like

<a href="{{ admin.generateObjectUrl('delete', object) }}" class="delete_link" title="{% trans from 'SonataAdminBundle' %}action_delete{% endtrans %}">
    <img src="{{ asset('bundles/sonataadmin/famfamfam/delete.png') }}" alt="{% trans from 'SonataAdminBundle' %}action_delete{% endtrans %}" />
</a>

All examples are taken from link that you provided. Hope it helps

Share:
12,566
Teo.sk
Author by

Teo.sk

A full-stack developer working with PHP (Symfony) and modern JavaScript/TypeScript. Recently moving into gamedev field (Unity, C#). 2019 has been the first year I worked more on games, than on webs. I also used to mentor students in programming in various educational projects. I'm a musician, singer in a heavy rockband, experimental composer. My imagination of hell is a place where music doesn't exist.

Updated on June 05, 2022

Comments

  • Teo.sk
    Teo.sk almost 2 years

    I'm using SonataAdminBundle for managing entities in my application. The admins of the site can add videos, and some of them first need to be approved by their speakers. There is an authorization system working already - I have working code which will generate a special link and notify the speaker, who can approve or disapprove the video, and notify back the admins automatically.

    I'd like to customize my admin section, so there will be a button ask for authorization next to the videos. I'm okay having it either in the list action ( /admin/acme/videos/list ) or in the edit action somewhere in the right-nav ( /admin/acme/videos/x/edit/ )

    What's the best approach to do this? The documentation says very little about blocks customization, but I found this example which may be the thing I'm looking for, but I couldn't figure out how to use it.

    One option is to use the preUpdate hook, and add a checkbox to the edit action, but a button would be much nicer.

  • Serge Kvashnin
    Serge Kvashnin over 8 years
    configureSideMenu is deprecated. Use configureTabMenu instead.