Drupal core itself uses register()
, in one of its modules. See LanguageServiceProvider::register()
.
// The following services are needed only on multilingual sites.
if ($this->isMultilingual()) {
$container->register('language_request_subscriber', 'Drupal\language\EventSubscriber\LanguageRequestSubscriber')
->addTag('event_subscriber')
->addArgument(new Reference('language_manager'))
->addArgument(new Reference('language_negotiator'))
->addArgument(new Reference('string_translation'))
->addArgument(new Reference('current_user'));
$container->register('path_processor_language', 'Drupal\language\HttpKernel\PathProcessorLanguage')
->addTag('path_processor_inbound', array('priority' => 300))
->addTag('path_processor_outbound', array('priority' => 100))
->addArgument(new Reference('config.factory'))
->addArgument(new Reference('language_manager'))
->addArgument(new Reference('language_negotiator'))
->addArgument(new Reference('current_user'))
->addArgument(new Reference('language.config_subscriber'))
->addMethodCall('initConfigSubscriber');
}
The code is quite similar to the code you would use: It checks a condition is verified at run-time, and then adds the necessary services. In this case, the alternative would be having two services that check if the site is multi-lingual and do nothing in the case it is not.
I take the note you quoted as meaning don't implement the register()
method if there are other alternative implementations for what you are trying to achieve. In your case, the alternative would be checking from the service if the other module is installed, which is what the service implemented by the Drupal core module tries to avoid, or making your module dependent from the other module, which is what I would avoid, since (from what I understand) the module is not dependent from the other one, and it would work even without it being installed.