function module_set_weight

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.

9 calls to module_set_weight()
block_install in drupal/core/modules/block/block.install
Implements hook_install().
DrupalUnitTestBaseTest::testEnableModulesFixedList in drupal/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php
Tests that the module list is retained after enabling/installing/disabling modules.
field_test_install in drupal/core/modules/field/tests/modules/field_test/field_test.install
Implements hook_install().
forum_install in drupal/core/modules/forum/forum.install
Implements hook_install().
install_finished in drupal/core/includes/install.core.inc
Performs final installation steps and displays a 'finished' page.

... See full list

File

drupal/core/includes/module.inc, line 325
API for loading and interacting with Drupal modules.

Code

function module_set_weight($module, $weight) {

  // Update the module weight in the config file that contains it.
  $module_config = config('system.module');
  if ($module_config
    ->get("enabled.{$module}") !== NULL) {
    $module_config
      ->set("enabled.{$module}", $weight)
      ->set('enabled', module_config_sort($module_config
      ->get('enabled')))
      ->save();

    // Prepare the new module list, sorted by weight, including filenames.
    // @see module_enable()
    $module_handler = Drupal::moduleHandler();
    $current_module_filenames = $module_handler
      ->getModuleList();
    $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
    $current_modules = module_config_sort(array_merge($current_modules, $module_config
      ->get('enabled')));
    $module_filenames = array();
    foreach ($current_modules as $name => $weight) {
      $module_filenames[$name] = $current_module_filenames[$name];
    }

    // Update the module list in the extension handler.
    $module_handler
      ->setModuleList($module_filenames);
    return;
  }
  $disabled_config = config('system.module.disabled');
  if ($disabled_config
    ->get($module) !== NULL) {
    $disabled_config
      ->set($module, $weight)
      ->save();
    return;
  }
}