12

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?

4 Answers 4

9

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'];
      unset($implementations['content_translation']);
      $implementations['content_translation'] = $group;
      break;

    // 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;
      break;
  }
}
3
  • Okay. I'll do that. My question is where I can see these values in drupal database?.
    – user15837
    Commented Jan 12, 2016 at 7:38
  • 3
    Unless the module you are trying to outweigh also uses hook_module_implements_alter() (actually happened to me, I think with one of the i18n modules). Commented Jan 12, 2016 at 10:45
  • 1
    Using hook_module_implements_alter is not the same thing as changing module weight. This shouldn't be the accepted answer.
    – Elin Y.
    Commented Jun 13, 2023 at 9:55
10

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.

4
  • But which has higher priority, high or low weights? Commented Nov 4, 2016 at 21:09
  • 6
    Low comes first: -10....-1....1....10 and so on.
    – Berdir
    Commented Nov 6, 2016 at 14:09
  • module_set_weight() will be deprecated: drupal.org/project/drupal/issues/2968232 Commented Feb 1, 2023 at 14:17
  • @AntonínSlejška, this is only a proposal. It's unclear if and when it will be deprecated.
    – 4uk4
    Commented Jul 27, 2023 at 19:08
6

If you use import/export configs, you can change module's weight in core.extension.yml file, number after module's name is weight.

1
  • 4
    This only works if you also alter the order of the modules in the list. They should be ordered by module weight, ascending. Commented Apr 9, 2018 at 15:29
4

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.

3
  • Not to hijack your answer, but what are some examples of why "sometimes we need to modify execution order"? Commented Oct 1, 2017 at 14:13
  • @Pierre.Vriens Good question, I'm really don't know :-O . I think that sometimes you need to be sure that you code is the first or the last executed. I never have this problem before, I just see the module in Drupal 7 without a Drupal 8 version and I start the port ;-) Commented Oct 1, 2017 at 17:29
  • 2
    @Pierre.Vriens in my case, D8 has Dependency Injection feature, this enables you to change anything in services.yml in your custom module. The problem arises when what you want to overrides actually in higher priority (aka. module weight), what i mean is when your custom module is executed first than the module services.yml you want to change. Hence, you need to change your custom module to be executed after.
    – kororo
    Commented Mar 26, 2018 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.