简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM