Drupal - How to inject configuration values into services?

You can use a factory for your app.mailer service. The factory take cares of retrieving the configuration for the service. The service can stay decoupled from the configuration service and does not need to know how the config parameters are named.

services:
  app.mailer:
    class:       Drupal/mail_module/Mailer
    factory:      Drupal/mail_module/MailerFactory:create
    arguments:    ['@config.factory']


class MailerFactory {
  static function create($config) {
    return new Mailer($config->get('mail.config')->get('transport'));
  }
}

class Mailer {
  public function __construct($transport) {
    $this->mailTransport = $transport;
  }
}

That's the way to do it. Configuration can change at runtime, the service definition is usually persisted and rebulding it is expensive. Assuming it is configuration that you want users to change.

If it is not, then you can use parameters, just like the symfony example. Then you can put your configuration in services.yml in sites/default. But you can only change it by changing the code and rebuilding the container.


You can use a factory or configurator service that is aware of how the config factory works etc, that keeps your service decoupled from the config factory. The HTTP client in core has a configurator if you need an example. See https://symfony.com/doc/current/service_container/configurators.html#using-the-configurator for docs.

Tags:

8