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);
}