Search code examples
drupal-8

how to use module_set_weight function in drupal 8?


In module_set_weight($module, $weight) function, what is the representation of the weight parameter? Does the higher of the number, and the lower for the execute order of the module? Can any one offer an example for me?


Solution

  • From documentation:

    int $weight: An integer representing the weight of the module.

    In Drupal, weight of the modules will determine the order in which the hook functions (which is implemented within them) are executed. The one with less weight will be executed first. For modules of equal weight, the alphabetical order of module name will be used.

    For example, we have module_a (weight = 0) and module_b (weight = 1), both of them have the hook_entity_update implementation inside:

    module_a.module

    function module_a_entity_update($entity) {
        // some logic
    }
    

    module_b.module

    function module_b_entity_update($entity) {
        // some logic
    }
    

    So when the hook_entity_update hook functions are called, module_a_entity_update() will be executed before module_b_entity_update().