To modify existing services, implement a class extending ServiceProviderBase
and the alter()
method. If you want to extend existing services to add functionality, you may also consider decorating the service.
Note that if you want this service alteration to be recognized automatically, the name of this class is required to be a CamelCase version of your module's machine name followed by ServiceProvider
, it is required to be in your module's top-level namespace Drupal\your_module_name, and it must implement \Drupal\Core\DependencyInjection\ServiceModifierInterface
(which ServiceProviderBase
does).
Example how to override user.auth service:
Also you can override core service using mymodule.services.yml
A simpler way to override core service class without creating your custom service provider class.
Following example will override core service named "user.auth
".