4

In Drupal 8, during module install, is hook_install() called before or after loading the .yml files in config/install?

I've across a module that's using both (which seems like a bad idea on its own) and I'm trying to run down the cause of an inconsistency in the configuration right after the module is installed.

6

When Drupal install a module, first it installs the default configuration of the module, then it invokes its hook_install(). This is evident from ModuleInstaller::install(), which contains the following code.

  // Install default configuration of the module.
  $config_installer = \Drupal::service('config.installer');
  if ($sync_status) {
    $config_installer
    ->setSyncing(TRUE)
      ->setSourceStorage($source_storage);
  }
  \Drupal::service('config.installer')->installDefaultConfig('module', $module);

  // If the module has no current updates, but has some that were
  // previously removed, set the version to the value of
  // hook_update_last_removed().
  if ($last_removed = $this->moduleHandler->invoke($module, 'update_last_removed')) {
    $version = max($version, $last_removed);
  }
  drupal_set_installed_schema_version($module, $version);

  // Ensure that all post_update functions are registered already.
  /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
  $post_update_registry = \Drupal::service('update.post_update_registry');
  $post_update_registry->registerInvokedUpdates($post_update_registry->getModuleUpdateFunctions($module));

  // Record the fact that it was installed.
  $modules_installed[] = $module;

  // Drupal's stream wrappers needs to be re-registered in case a
  // module-provided stream wrapper is used later in the same request. In
  // particular, this happens when installing Drupal via Drush, as the
  // 'translations' stream wrapper is provided by Interface Translation
  // module and is later used to import translations.
  \Drupal::service('stream_wrapper_manager')->register();

  // Update the theme registry to include it.
  drupal_theme_rebuild();

  // Modules can alter theme info, so refresh theme data.
  // @todo ThemeHandler cannot be injected into ModuleHandler, since that
  //   causes a circular service dependency.
  // @see https://www.drupal.org/node/2208429
  \Drupal::service('theme_handler')->refreshInfo();

  // In order to make uninstalling transactional if anything uses routes.
  \Drupal::getContainer()->set('router.route_provider.old', \Drupal::service('router.route_provider'));
  \Drupal::getContainer()->set('router.route_provider', \Drupal::service('router.route_provider.lazy_builder'));

  // Allow the module to perform install tasks.
  $this->moduleHandler->invoke($module, 'install');

The first lines I quoted are for installing the default configuration, while the last one is for invoking hook_install().

  • in d7 module inside mymodule_enable() I had variable_set('installed_timestamp',REQUEST_TIME); but I couldn't get how to set same in drupal8. REQUEST_TIME is reprecated in > 8.3 , also tried in hook_install $timestamp = REQUEST_TIME; // also tried \Drupal::time()->getRequestTime(); \Drupal::configFactory()->getEditable('mymodule.settings')->set('installed_timestamp',$timestamp)->save(); but doesn't work. – kiranking Mar 3 '18 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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