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:
This is what I did in Twig (not really nice):
And in UserAdminController:
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.