You are here

function drush_multiversion_uninstall in Multiversion 8.2

Same name and namespace in other branches
  1. 8 multiversion.drush.inc \drush_multiversion_uninstall()

Implements drush_hook_COMMAND().

File

./multiversion.drush.inc, line 54

Code

function drush_multiversion_uninstall() {
  $extension = 'multiversion';
  $uninstall = TRUE;
  $extension_info = drush_get_extensions();
  $required = drush_drupal_required_modules($extension_info);
  if (in_array($extension, $required)) {
    $info = $extension_info[$extension]->info;
    $explanation = !empty($info['explanation']) ? ' ' . dt('Reason: !explanation.', [
      '!explanation' => strip_tags($info['explanation']),
    ]) : '';
    drush_log(dt('!extension is a required extension and can\'t be uninstalled.', [
      '!extension' => $extension,
    ]) . $explanation, LogLevel::INFO);
    $uninstall = FALSE;
  }
  elseif (!$extension_info[$extension]->status) {
    drush_log(dt('!extension is already uninstalled.', [
      '!extension' => $extension,
    ]), LogLevel::INFO);
    $uninstall = FALSE;
  }
  elseif (drush_extension_get_type($extension_info[$extension]) == 'module') {
    $dependents = [];
    foreach (drush_module_dependents([
      $extension,
    ], $extension_info) as $dependent) {
      if (!in_array($dependent, $required) && $extension_info[$dependent]->status) {
        $dependents[] = $dependent;
      }
    }
    if (count($dependents)) {
      drush_log(dt('To uninstall !extension, the following extensions must be uninstalled first: !required', [
        '!extension' => $extension,
        '!required' => implode(', ', $dependents),
      ]), LogLevel::ERROR);
      $uninstall = FALSE;
    }
  }
  if ($uninstall) {
    drush_print(dt('Multiversion will be uninstalled.'));
    if (!drush_confirm(dt('Do you really want to continue?'))) {
      return drush_user_abort();
    }
    drush_log('*** ' . dt('The uninstall process can take a few minutes, it depends by the number of entities on the site. Please be patient.'), LogLevel::WARNING);
    try {
      \Drupal::service('multiversion.manager')
        ->disableEntityTypes();

      // Delete workspace entities before uninstall.
      $storage = \Drupal::entityTypeManager()
        ->getStorage('workspace');
      $entities = $storage
        ->loadMultiple();
      $storage
        ->delete($entities);
      drush_module_uninstall([
        'multiversion',
      ]);
    } catch (Exception $e) {
      drush_log($e
        ->getMessage(), LogLevel::ERROR);
    }

    // Inform the user of final status.
    drush_log(dt('!extension was successfully uninstalled.', [
      '!extension' => $extension,
    ]), LogLevel::INFO);
  }
}