Unverified Commit c3c3bc8d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2914174 by larowlan, alexpott, DanielVeza: Improve hook_install() to...

Issue #2914174 by larowlan, alexpott, DanielVeza: Improve hook_install() to include recommendations for config import and pass an $is_syncing argument

(cherry picked from commit c72abf07)
parent 73b1fa59
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -320,7 +320,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
        \Drupal::service('theme_handler')->refreshInfo();

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

        // Record the fact that it was installed.
        \Drupal::logger('system')->info('%module module installed.', ['%module' => $module]);
@@ -342,7 +342,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
        \Drupal::service('router.builder')->rebuild();
      }

      $this->moduleHandler->invokeAll('modules_installed', [$modules_installed]);
      $this->moduleHandler->invokeAll('modules_installed', [$modules_installed, $sync_status]);
    }

    return TRUE;
@@ -354,6 +354,7 @@ public function install(array $module_list, $enable_dependencies = TRUE) {
  public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
    // Get all module data so we can find dependencies and sort.
    $module_data = \Drupal::service('extension.list.module')->getList();
    $sync_status = \Drupal::service('config.installer')->isSyncing();
    $module_list = $module_list ? array_combine($module_list, $module_list) : [];
    if (array_diff_key($module_list, $module_data)) {
      // One or more of the given modules doesn't exist.
@@ -425,7 +426,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) {

      // Uninstall the module.
      module_load_install($module);
      $this->moduleHandler->invoke($module, 'uninstall');
      $this->moduleHandler->invoke($module, 'uninstall', [$sync_status]);

      // Remove all configuration belonging to the module.
      \Drupal::service('config.manager')->uninstall('module', $module);
@@ -511,7 +512,7 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) {
    drupal_get_installed_schema_version(NULL, TRUE);

    // Let other modules react.
    $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list]);
    $this->moduleHandler->invokeAll('modules_uninstalled', [$module_list, $sync_status]);

    // Flush all persistent caches.
    // Any cache entry might implicitly depend on the uninstalled modules,
+40 −4
Original line number Diff line number Diff line
@@ -180,14 +180,22 @@ function hook_module_preinstall($module) {
 *
 * @param $modules
 *   An array of the modules that were installed.
 * @param bool $is_syncing
 *   TRUE if the module is being installed as part of a configuration import. In
 *   these cases, your hook implementation needs to carefully consider what
 *   changes, if any, it should make. For example, it should not make any
 *   changes to configuration objects or entities.
 *
 * @see \Drupal\Core\Extension\ModuleInstaller::install()
 * @see hook_install()
 */
function hook_modules_installed($modules) {
function hook_modules_installed($modules, $is_syncing) {
  if (in_array('lousy_module', $modules)) {
    \Drupal::state()->set('mymodule.lousy_module_compatibility', TRUE);
  }
  if (!$is_syncing) {
    \Drupal::service('mymodule.service')->doSomething($modules);
  }
}

/**
@@ -220,15 +228,26 @@ function hook_modules_installed($modules) {
 * Please be sure that anything added or modified in this function that can
 * be removed during uninstall should be removed with hook_uninstall().
 *
 * @param bool $is_syncing
 *   TRUE if the module is being installed as part of a configuration import. In
 *   these cases, your hook implementation needs to carefully consider what
 *   changes, if any, it should make. For example, it should not make any
 *   changes to configuration objects or entities.
 *
 * @see \Drupal\Core\Config\ConfigInstallerInterface::isSyncing
 * @see hook_schema()
 * @see \Drupal\Core\Extension\ModuleInstaller::install()
 * @see hook_uninstall()
 * @see hook_modules_installed()
 */
function hook_install() {
function hook_install($is_syncing) {
  // Create the styles directory and ensure it's writable.
  $directory = \Drupal::config('system.file')->get('default_scheme') . '://styles';
  \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  if (!$is_syncing) {
    // Modify a configuration value because we're not syncing.
    \Drupal::configFactory()->getEditable('system.file')->set('default_scheme', 'private')->save();
  }
}

/**
@@ -253,14 +272,22 @@ function hook_module_preuninstall($module) {
 *
 * @param $modules
 *   An array of the modules that were uninstalled.
 * @param bool $is_syncing
 *   TRUE if the module is being uninstalled as part of a configuration import.
 *   In these cases, your hook implementation needs to carefully consider what
 *   changes, if any, it should make. For example, it should not make any
 *   changes to configuration objects or entities.
 *
 * @see hook_uninstall()
 */
function hook_modules_uninstalled($modules) {
function hook_modules_uninstalled($modules, $is_syncing) {
  if (in_array('lousy_module', $modules)) {
    \Drupal::state()->delete('mymodule.lousy_module_compatibility');
  }
  mymodule_cache_rebuild();
  if (!$is_syncing) {
    \Drupal::service('mymodule.service')->doSomething($modules);
  }
}

/**
@@ -278,13 +305,22 @@ function hook_modules_uninstalled($modules) {
 * tables are removed, allowing your module to query its own tables during
 * this routine.
 *
 * @param bool $is_syncing
 *   TRUE if the module is being uninstalled as part of a configuration import.
 *   In these cases, your hook implementation needs to carefully consider what
 *   changes, if any, it should make. For example, it should not make any
 *   changes to configuration objects or entities.
 *
 * @see hook_install()
 * @see hook_schema()
 * @see hook_modules_uninstalled()
 */
function hook_uninstall() {
function hook_uninstall($is_syncing) {
  // Remove the styles directory and generated images.
  \Drupal::service('file_system')->deleteRecursive(\Drupal::config('system.file')->get('default_scheme') . '://styles');
  if (!$is_syncing) {
    \Drupal::service('mymodule.service')->removeContent();
  }
}

/**
+2 −2
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
/**
 * Implements hook_install().
 */
function forum_install() {
function forum_install($is_syncing) {
  // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
  module_set_weight('forum', 1);
  // Do not allow to delete the forum's node type machine name.
@@ -19,7 +19,7 @@ function forum_install() {
  $locked['forum'] = 'forum';
  \Drupal::state()->set('node.type.locked', $locked);

  if (!\Drupal::service('config.installer')->isSyncing()) {
  if (!$is_syncing) {
    // Create a default forum so forum posts can be created.
    $term = Term::create([
      'name' => t('General discussion'),
+2 −2
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@
/**
 * Implements hook_install().
 */
function media_library_install() {
  if (!\Drupal::isConfigSyncing()) {
function media_library_install($is_syncing) {
  if (!$is_syncing) {
    foreach (MediaType::loadMultiple() as $type) {
      _media_library_configure_form_display($type);
      _media_library_configure_view_display($type);
+4 −4
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@
/**
 * Implements hook_install().
 */
function demo_umami_content_install() {
  if (!\Drupal::service('config.installer')->isSyncing()) {
function demo_umami_content_install($is_syncing) {
  if (!$is_syncing) {
    \Drupal::classResolver(InstallHelper::class)->importContent();
  }
}
@@ -19,8 +19,8 @@ function demo_umami_content_install() {
/**
 * Implements hook_uninstall().
 */
function demo_umami_content_uninstall() {
  if (!\Drupal::service('config.installer')->isSyncing()) {
function demo_umami_content_uninstall($is_syncing) {
  if (!$is_syncing) {
    \Drupal::classResolver(InstallHelper::class)->deleteImportedContent();
  }
}