You are here

function thunder_updater_update_8001 in Thunder 8.2

Switch to update_helper module instead of thunder_updater.

File

modules/thunder_updater/thunder_updater.install, line 14
Thunder Updater install hooks.

Code

function thunder_updater_update_8001() {

  /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
  $module_handler = \Drupal::moduleHandler();

  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  $module_installer = \Drupal::service('module_installer');

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::configFactory();

  // We let exception bubble in this case, since it's not possible to continue
  // with execution of update hooks without "update_helper.updater" service.
  $module_installer
    ->install([
    'update_helper',
  ]);

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');
  $update_logger = $updater
    ->logger();

  // We need database connection in order to migrate Thunder updates correctly.
  $database = \Drupal::database();

  // Existing module states should be preserved!
  $has_thunder_updater_checklist = $module_handler
    ->moduleExists('checklistapi') && !empty($config_factory
    ->get('checklistapi.progress.thunder_updater')
    ->getRawData());

  // Migrate existing Thunder update entities and checklist config.
  if ($has_thunder_updater_checklist) {
    try {
      $module_installer
        ->install([
        'update_helper_checklist',
      ]);

      /** @var \Drupal\update_helper_checklist\UpdateChecklist $update_checklist */
      $update_checklist = \Drupal::service('update_helper_checklist.update_checklist');
      $update_checklist
        ->markAllUpdates(FALSE);
      $update_checklist
        ->markUpdatesSuccessful([
        'thunder' => [
          'thunder_updater_update_8001',
        ],
      ]);
    } catch (Exception $e) {
      $update_logger
        ->warning('Unable to install "update_helper_checklist".');
    }

    // Remove update entities created on "update_helper_checklist" install.

    /** @var \Drupal\update_helper_checklist\Entity\Update[] $all_updates */
    $all_updates = Update::loadMultiple();
    foreach ($all_updates as $update) {
      try {

        // We want to keep status for this update hook.
        if ($update
          ->id() === 'thunder:thunder_updater_update_8001') {
          continue;
        }
        $update
          ->delete();
      } catch (EntityStorageException $e) {
        $update_logger
          ->warning(sprintf('Unable to delete Update entity (%s) for Update Helper Checklist.', $update
          ->id()));
        $update_checklist
          ->markUpdatesFailed([
          'thunder' => [
            'thunder_updater_update_8001',
          ],
        ]);
      }
    }
    $thunder_updater_updates = $database
      ->query('SELECT * FROM {thunder_updater_update};')
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($thunder_updater_updates as $thunder_updater_update) {
      $thunder_updater_update['id'] = 'thunder:' . $thunder_updater_update['id'];
      $update = Update::create($thunder_updater_update);
      try {
        $update
          ->save();
      } catch (EntityStorageException $e) {
        $update_logger
          ->warning(sprintf('Unable to create Update entity (%s) for Update Helper Checklist.', $update
          ->id()));
        $update_checklist
          ->markUpdatesFailed([
          'thunder' => [
            'thunder_updater_update_8001',
          ],
        ]);
      }
    }

    // Migrate Checklist API configuration.
    $update_helper_checklist_definition = checklistapi_get_checklist_info('update_helper_checklist');
    if ($update_helper_checklist_definition && isset($update_helper_checklist_definition['#storage']) && $update_helper_checklist_definition['#storage'] == 'state') {

      /** @var \Drupal\checklistapi\Storage\ConfigStorage $old_config_storage */
      $old_config_storage = \Drupal::service('checklistapi_storage.config');
      $thunder_updater_progress = $old_config_storage
        ->setChecklistId('thunder_updater')
        ->getSavedProgress();

      /** @var \Drupal\checklistapi\Storage\StateStorage $new_state_storage */
      $new_state_storage = \Drupal::service('checklistapi_storage.state');
      $update_helper_progress = $new_state_storage
        ->setChecklistId('update_helper_checklist')
        ->getSavedProgress();
      if (!empty($thunder_updater_progress) && is_array($thunder_updater_progress['#items'])) {
        foreach ($thunder_updater_progress['#items'] as $update_id => $update_info) {
          $update_helper_progress['#items']['thunder:' . $update_id] = $update_info;
        }
      }
      $update_helper_progress['#completed_items'] = count($update_helper_progress['#items']);
      $new_state_storage
        ->setChecklistId('update_helper_checklist')
        ->setSavedProgress($update_helper_progress);
    }
    else {
      $thunder_updater_checklist = $config_factory
        ->getEditable('checklistapi.progress.thunder_updater');
      $update_helper_checklist = $config_factory
        ->getEditable('checklistapi.progress.update_helper_checklist');
      $items = $thunder_updater_checklist
        ->get('progress.#items');
      if (is_array($items)) {
        foreach ($items as $update_id => $update_info) {
          $update_helper_checklist
            ->set('progress.#items.thunder:' . $update_id, $update_info);
        }
      }
      $update_helper_checklist
        ->set('progress.#completed_items', $update_helper_checklist
        ->get('progress.#completed_items') + $thunder_updater_checklist
        ->get('progress.#completed_items'));
      $update_helper_checklist
        ->save();
    }

    // Remove Thunder Updater checklist configuration.
    $config_factory
      ->getEditable('checklistapi.progress.thunder_updater')
      ->delete();
  }

  // We want to remove all migrated updates, so that uninstall can work.
  $database
    ->truncate('thunder_updater_update')
    ->execute();

  // Finally remove Thunder Updater!
  $module_installer
    ->uninstall([
    'thunder_updater',
  ]);
  return $update_logger
    ->output();
}