简体   繁体   中英

Sonata User Bundle custom batch action

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?

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 :)

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