function ModuleWeight::set

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Extension/ModuleWeight.php \Drupal\Core\Extension\ModuleWeight::set()

Sets weight of a particular module.

The weight of uninstalled modules cannot be changed.

Parameters

string $module: The name of the module (without the .module extension).

int $weight: An integer representing the weight of the module.

File

core/lib/Drupal/Core/Extension/ModuleWeight.php, line 27

Class

ModuleWeight
Class that manages weights.

Namespace

Drupal\Core\Extension

Code

public function set(string $module, int $weight) : void {
  $extensionConfig = $this->configFactory
    ->getEditable('core.extension');
  if ($extensionConfig->get("module.{$module}") !== NULL) {
    // Pre-cast the $weight to an integer so that we can save this without
    // using schema. This is a performance improvement for module
    // installation.
    $extensionConfig->set("module.{$module}", (int) $weight)
      ->set('module', $this->sort($extensionConfig->get('module')))
      ->save();
    // Prepare the new module list, sorted by weight, including filenames.
    // @see \Drupal\Core\Extension\ModuleInstaller::install()
    $currentModuleFilenames = $this->moduleHandler
      ->getModuleList();
    $currentModules = array_fill_keys(array_keys($currentModuleFilenames), 0);
    $currentModules = $this->sort(array_merge($currentModules, $extensionConfig->get('module')));
    $moduleFilenames = [];
    foreach ($currentModules as $name => $weight) {
      $moduleFilenames[$name] = $currentModuleFilenames[$name];
    }
    // Update the module list in the extension handler.
    $this->moduleHandler
      ->setModuleList($moduleFilenames);
    return;
  }
}

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