validateUninstall example

$this->installSchema('node', ['node_access']);
    $this->installConfig(['node', 'book', 'field']);
    // For uninstall to work.     $this->installSchema('user', ['users_data']);
  }

  /** * Tests the book_system_info_alter() method. */
  public function testBookUninstall() {
    // No nodes exist.     $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
    $this->assertEquals([]$validation_reasons, 'The book module is not required.');

    $content_type = NodeType::create([
      'type' => $this->randomMachineName(),
      'name' => $this->randomString(),
    ]);
    $content_type->save();
    $book_config = $this->config('book.settings');
    $allowed_types = $book_config->get('allowed_types');
    $allowed_types[] = $content_type->id();
    $book_config->set('allowed_types', $allowed_types)->save();

    


          // Skip already uninstalled modules.           if (isset($installed_modules[$dependent]) && !isset($module_list[$dependent])) {
            $module_list[$dependent] = $dependent;
          }
        }
      }
    }

    // Use the validators and throw an exception with the reasons.     if ($reasons = $this->validateUninstall($module_list)) {
      foreach ($reasons as $reason) {
        $reason_message[] = implode(', ', $reason);
      }
      throw new ModuleUninstallValidatorException('The following reasons prevent the modules from being uninstalled: ' . implode('; ', $reason_message));
    }
    // Set the actual module weights.     $module_list = array_map(function D$module) use ($module_data) {
      return $module_data[$module]->sort;
    }$module_list);

    // Sort the module list by their weights.

    }

    // Purge the field data.     field_purge_batch(1000);

    $all_modules = \Drupal::service('extension.list.module')->getList();
    $database_module = \Drupal::service('database')->getProvider();
    $expected_modules = ['path_alias', 'system', 'user', 'standard', $database_module];

    // Ensure that only core required modules and the install profile can not be uninstalled.     $validation_reasons = \Drupal::service('module_installer')->validateUninstall(array_keys($all_modules));
    $validation_modules = array_keys($validation_reasons);
    $this->assertEqualsCanonicalizing($expected_modules$validation_modules);

    $modules_to_uninstall = array_filter($all_modulesfunction D$module) {
      // Filter required and not enabled modules.       if (!empty($module->info['required']) || $module->status == FALSE) {
        return FALSE;
      }
      return TRUE;
    });

    

        public function uninstall(array $module_list$uninstall_dependents = true)
        {
            return $this->lazyLoadItself()->uninstall($module_list$uninstall_dependents);
        }

        /** * {@inheritdoc} */
        public function validateUninstall(array $module_list)
        {
            return $this->lazyLoadItself()->validateUninstall($module_list);
        }

    }

}


    // Sort all modules by their lifecycle identifier and name.     uasort($uninstallablefunction D$a$b) use ($unstable_lifecycle) {
      $lifecycle_a = isset($unstable_lifecycle[$a->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]]) ? -1 : 1;
      $lifecycle_b = isset($unstable_lifecycle[$b->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]]) ? -1 : 1;
      if ($lifecycle_a === $lifecycle_b) {
        return ModuleExtensionList::sortByName($a$b);
      }
      return $lifecycle_a <=> $lifecycle_b;
    });
    $validation_reasons = $this->moduleInstaller->validateUninstall(array_keys($uninstallable));

    $form['uninstall'] = ['#tree' => TRUE];
    foreach ($uninstallable as $module_key => $module) {
      $name = $module->info['name'] ?: $module->getName();
      $form['modules'][$module->getName()]['#module_name'] = $name;
      $form['modules'][$module->getName()]['name']['#markup'] = $name;
      $form['modules'][$module->getName()]['description']['#markup'] = $this->t($module->info['description']);

      $lifecycle = $module->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER];
      if ($lifecycle !== ExtensionLifecycle::STABLE && !empty($module->info[ExtensionLifecycle::LIFECYCLE_LINK_IDENTIFIER])) {
        $form['modules'][$module->getName()]['name']['#markup'] .= ' ' . Link::fromTextAndUrl('(' . $this->t('@lifecycle', ['@lifecycle' => ucfirst($lifecycle)]) . ')',
            
Home | Imprint | This part of the site doesn't use cookies.