It is noted that modules implementing ServiceProviderBase to alter defined services can 'lose' to other modules overriding the same service as there is no way to know which one should overtake in that situation.

Is this 'fixable' by making your modules weight higher than the others?

  • I haven't needed this but I would guess that altering the weights in core.extension config might do the trick but it might not since this is a symfony thing and I do not know if order of module is being considered in this case. – user21641 Jul 9 '17 at 20:20
  • If you create a module that overrides a service - its best to do it this way and not in the services yml as that can lead to errors in deployment – Kevin Jul 10 '17 at 12:30
  • Sometimes decoration is better than overtake and solves this problem. – Smartsheet eng Oct 2 at 15:48
up vote 5 down vote accepted

Yes, the serviceProviderClasses are discovered in DrupalKernel::discoverServiceProviders(), I've stopped the code there and the list was sorted by module weight and modules with the same weight alphabetically.

The answer by 4k4 is correct, I'd like to share some an additional note about changing the module weight...

If you add a module_set_weight() in a hook_install() it won't have any immediate effect on the order inside DrupalKernel::discoverServiceProviders() (as this would have already run during the install process). You will need to clear Drupal cache's first.

Here's the hook_install I'm using, with some code copied over from drupal_flush_all_caches() avoiding doing a second full cache clear in the same request.

/**
 * Implements hook_install().
 */
function mymodule_install() {
  module_set_weight('mymodule', 1);

  $module_handler = \Drupal::moduleHandler();
  $module_data = system_rebuild_module_data();
   // Rebuild and reboot a new kernel. A simple DrupalKernel reboot is not
  // sufficient, since the list of enabled modules might have been adjusted
  // above due to changed code.
  $files = [];
  foreach ($module_data as $name => $extension) {
    if ($extension->status) {
      $files[$name] = $extension;
    }
  }
  \Drupal::service('kernel')->updateModules($module_handler->getModuleList(), $files);

}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.