简体   繁体   中英

Disable SonataUserBundle sonata.user.admin.group service

I'm working with SonataAdminBundle and SonataUserBundle.

SonataUserBundle registers a service sonata.user.admin.group which is automatically detected by SonataAdminBundle to set links in the admin dashboard to group CRUD operations.

How can I disable sonata.user.admin.group ? I've been following that recipes in Symfony2 documentation:

So far, I have the following code in my bundle definition to add a compiler pass:

public function build(ContainerBuilder $container)
{
  parent::build($container);

  $container->addCompilerPass(new CompilerPass());
}

And here it is the compiler pass:

<?php

namespace NS\Service\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
       $container->removeDefinition('sonata.user.admin.group');
    }
}

I thought that this should work but no. Symfony is throwing an exception telling me that sonata.user.admin.group service does not exist. But it exists, and if I do $container->getDefinition('sonata.user.admin.group') the actual definition is return.

Thanks

Try marking the service as abstract and set its public property to false eg

#in any services.yml
services:
    sonata.user.admin.group:
      abstract: true
      public: false
    #...

Addition to completeness:

And add to the CompilerPass:

$container->getDefinition('sonata.user.admin.group')->setSynthetic(true);

You've removed the service definition but it's still used on the dashboard. That's why Symfony complains (dashboard tries to access it). It's not an optional service.

You could try to overwrite the dashboard template and avoid using the service? This way service wouldn't be called and you wouldn't have to remove it. If service is not used it's never created.

Alternative would be overloading the service with your implementation.

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