How to Reduce/Increase module's weight on the module install process?

I'm developing one custom module and I want to increase my module weight when the module is installed.

How can I achieve this? Otherwise, does anyone know in which table the module's weight is stored?

Answers 4

  • Use hook_module_implements_alter() rather than changing the module weight.

    Sample implementation from content_translation.module:

    function content_translation_module_implements_alter(&$implementations, $hook) {
      switch ($hook) {
        // Move our hook_entity_type_alter() implementation to the end of the list.
        case 'entity_type_alter':
          $group = $implementations['content_translation'];
          $implementations['content_translation'] = $group;
        // Move our hook_entity_bundle_info_alter() implementation to the top of the
        // list, so that any other hook implementation can rely on bundles being
        // correctly marked as translatable.
        case 'entity_bundle_info_alter':
          $group = $implementations['content_translation'];
          $implementations = [
            'content_translation' => $group,
          ] + $implementations;

  • There's an API for this now:

    module_set_weight('your_module_name', 10);

    You can also implement the hook as Ivan Jaros said, which allows for more fine-grained control (e.g. first for one hook, last for another, after a specific module for the third). But the module weight should work too.

  • You can use the Modules weight module:

    Sometimes we need to modify modules execution order, and some people could write a code that execute the query to modify the weight of a module in the system table, some one might go straight to his favorite SQL client and modify the record directly. This module provides an interface to reorder the modules weight.

    Disclosure: I'm the maintainer of the module Modules Weight.

Related Questions