10

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?

6

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;
  }
}
2
  • Okay. I'll do that. My question is where I can see these values in drupal database?. – user15837 Jan 12 '16 at 7:38
  • 2
    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). – wizonesolutions Jan 12 '16 at 10:45
5

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.

2
  • But which has higher priority, high or low weights? – RaisinBranCrunch Nov 4 '16 at 21:09
  • 5
    Low comes first: -10....-1....1....10 and so on. – Berdir Nov 6 '16 at 14:09
4

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
  • 2
    This only works if you also alter the order of the modules in the list. They should be ordered by module weight, ascending. – marcvangend Apr 9 '18 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"? – Pierre.Vriens Oct 1 '17 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 ;-) – Adrian Cid Almaguer Oct 1 '17 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 Mar 26 '18 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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