function module_set_weight

Same name and namespace in other branches
  1. 9 core/includes/module.inc \module_set_weight()
  2. 8.9.x core/includes/module.inc \module_set_weight()
  3. 10 core/includes/module.inc \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.

12 calls to module_set_weight()
content_moderation_test_resave_install in core/modules/content_moderation/tests/modules/content_moderation_test_resave/content_moderation_test_resave.install
Implements hook_install().
content_translation_install in core/modules/content_translation/content_translation.install
Implements hook_install().
EntityUpdateTest::setUp in core/modules/editor/tests/src/Kernel/EntityUpdateTest.php
forum_install in core/modules/forum/forum.install
Implements hook_install().
install_finished in core/includes/install.core.inc
Performs final installation steps and displays a 'finished' page.

... See full list

File

core/includes/module.inc, line 18

Code

function module_set_weight($module, $weight) {
    $extension_config = \Drupal::configFactory()->getEditable('core.extension');
    if ($extension_config->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.
        $extension_config->set("module.{$module}", (int) $weight)
            ->set('module', module_config_sort($extension_config->get('module')))
            ->save(TRUE);
        // Prepare the new module list, sorted by weight, including filenames.
        // @see \Drupal\Core\Extension\ModuleInstaller::install()
        $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, $extension_config->get('module')));
        $module_filenames = [];
        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;
    }
}

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