r1pp3rj4ck's tech blog

Because software is like sex… it's better when it's free – and even better with a penguin.

Custom bulk actions in SonataAdminBundle

Just like Emacs is a great operating system lacking only a decent editor, so is SonataAdminBundle a great tool lacking only a decent documentation, which is understandable, as it’s constantly evolving (and let’s face it, nobody likes to write docs :). Well, at least when I first had to use it, it was. Now the bundle is pretty well-documented, but of course Rande and the contributors can’t think of everything your customers may want (and we all know they want pretty fucked up things sometimes). So there are loads of “Easter Eggs” in SonataAdminBundle – I call them Easter Eggs because they are not documented, but the code kinda supports them.

So let’s say you have Users and Groups and you want to put Users to Groups. Reasonable assumption is that you have thousands of users and tens of groups and you want to put those selected users to the groups with a bulk action. So here is the official documentation for custom bulk actions: http://sonata-project.org/bundles/admin/2-0/doc/reference/batch_actions.html

Unfortunately we need an “Easter Egg” here. So this is what I did (troll stuff) in Admin:

http://pastebin.com/0PdkyFRa

This is what I did in Twig (not really nice):

http://pastebin.com/Rsh6juE3

And in UserAdminController:

http://pastebin.com/SyazaHxD

This is not really a nice way, it’s kinda hacking. Maybe someday I’ll figure out a clean way and contribute it to SonataAdminBundle.

Cheers!

Advertisements

Single Post Navigation

2 thoughts on “Custom bulk actions in SonataAdminBundle

  1. Excellent site, thanks for share this article with us

  2. Marvoh on said:

    Thank you for your tutorial, your requirements are same as mine and so it worked perfectly except when I select the batch action, i get a Sonata\AdminBundle\Controller\CRUDController::batchActionAddToGroup method must be created error. Is it too late to get an aswer?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: