Module weight alter by hook function

Alter a module weight and order of execution of a hook without changing system{table} weight.

Hook:=> hook_module_implements_alter

There are many scenarios in which for a particular hook you need to execute  your module first before the same hook from any other module gets executed. Normally in Drupal 6 what we will address this by either increase/decrease the weight of the module.

In Drupal 7, a hook called 'hook_module_implements_alter' which allows to modify the order of a particular hooks execution.

Example:

Current sequence of  modules in which a particular hook is implemented:

1. my_module_cache

2. flush_page_cache

3. admin_menu_source

4. esi

5. token

6. admin_menu

7. admin_menu_toolbar

Required sequence of modules in which a particular hook is implemented:

1. flush_page_cache

2. admin_menu_source

3. esi

4. token

5. admin_menu

6. admin_menu_toolbar

7. my_module_cache

Below code shows how the order is changed for a specific case of hook = 'admin_menu_output_alter'. This can be extended for any hook.

function my_module_module_implements_alter(&$implementations, $hook) { if ($hook == 'admin_menu_output_alter') { dpm($implementations, 'before'); $module = 'my_module_cache'; $group = array($module => $implementations[$module]); unset($implementations[$module]); $implementations = $implementations + $group; dpm($implementations, 'after'); } }

once my_module_module_implements_alter is executed then for the sequence of modules for hook('admin_menu_output_alter') is altered and organised as required.