function ModuleInstaller::getModulesToUninstall

File

core/lib/Drupal/Core/Extension/ModuleInstaller.php, line 518

Class

ModuleInstaller
Default implementation of the module installer.

Namespace

Drupal\Core\Extension

Code

public function getModulesToUninstall(array $module_list, bool $uninstall_dependents = TRUE) : array {
  // Get all module data so we can find dependents and sort.
  $module_data = \Drupal::service('extension.list.module')->getList();
  $module_list = $module_list ? array_combine($module_list, $module_list) : [];
  if ($missing = array_diff_key($module_list, $module_data)) {
    throw new UnknownExtensionException(sprintf('Unable to uninstall modules %s due to unknown modules %s.', implode(', ', $module_list), implode(', ', $missing)));
  }
  // Only process currently installed modules.
  $installed_modules = \Drupal::config('core.extension')->get('module') ?: [];
  if (!$module_list = array_intersect_key($module_list, $installed_modules)) {
    return [];
  }
  if ($uninstall_dependents) {
    $theme_list = \Drupal::service('extension.list.theme')->getList();
    // Add installed dependent modules to the list. Themes that depend on a
    // module are left to the uninstall validators.
    foreach ($module_list as $module) {
      foreach (array_keys($module_data[$module]->required_by) as $dependent) {
        if (!isset($module_data[$dependent]) && !isset($theme_list[$dependent])) {
          throw new UnknownExtensionException("Unable to uninstall module '{$module}' due to unknown dependent '{$dependent}'.");
        }
        if (isset($installed_modules[$dependent])) {
          $module_list[$dependent] = $dependent;
        }
      }
    }
  }
  // Sort the module list by their weights.
  $module_list = array_map(fn($module) => $module_data[$module]->sort, $module_list);
  asort($module_list);
  return array_keys($module_list);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.