Sonata User Bundle custom batch action

712 views Asked by At

so as the title suggests, I need to add custom batch action to SonataUserBundle.

With this action, the operator can send a message to all of the users (or selected ones). Therefore it needs to extend SonataAdminBundle in order to be able to add a custom view for this action.

The problem is, ApplicationSonataUserBundle.php is overriding SonataUserBundle:

class ApplicationSonataUserBundle extends Bundle
{
    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return 'SonataUserBundle';
    }
}

and if I change it, it will break the Bundle.

Is there any solutions that I can add this batch action to the bundle?

1

There are 1 answers

0
SebastianCastro On

What you need is editing the sonata_user config to use you own Controller instead of the default Sonata Admin CRUD Controller.

So in your config.yml add the following:

sonata_user:
    admin:           
        user:
            controller:     MyAppMyBundle:UserAdmin

And create your own controller that extend CRUDController

namespace MyApp\MyBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;

class UserAdminController extends Controller
{
}

now you can add your own batch actions like this

class UserAdminController extends Controller
{
   public function batchActionSendMail(ProxyQueryInterface $selectedModelQuery) 
   {      
      $selectedModels = $selectedModelQuery->execute();

      // Your stuff here

      return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
   }
}

Hope it will help someone :)