1

I want to show a message when a module is being enabled. I used hook_modules_enabled and it shows me the message while enabling a new module.

Now I need the currently enabled module machine name in the hooks argument. But hook_modules_enabled($modules) returns an array of modules in the argument.

Eg:

When views is enabled, I need to set a message as:

"Views Module is enabled"

Here I need "views" (i.e. the module name) as argument in hook_modules_enabled in order to print it in message but it returns me an array.

How can I achieve this. Is there any other hook for this?

3 Answers 3

0

The argument $modules in hook_modules_enabled($modules) returns an array of modules enabled. You should loop through the array to print the message.

Note: But only machine name of the module will be in the array.

For eg:

function MY_MODULE_modules_enabled($modules) {
  foreach ($modules as $module) {
    // To get Module Name 
    $module_info = system_get_info('module', $module);
    $module_name = $module_info['name'];
    //Loop through the modules and set the message. 
    $msg= "$module_name is enabled";
    drupal_set_message(t($msg), 'status', FALSE);
  }
}
0

hook_module_enable() should do the job unless I'm missing something from your question.

Perform necessary actions after module is enabled.

The hook is called every time the module is enabled. It should be implemented in the module's .install file. The implementation can rely on the .module file being loaded.

1
  • I updated my question
    – user46121
    Commented May 26, 2015 at 12:03
0

I don't see any other hooks that you could invoke; you could keep track of modules yourself, compare it with what you get in hook_modules_enabled() and then do whatever you want for each of the enabled/installed modules.

Here are general steps:

  1. Upon installation of your custom module (or in update hook), save the snapshot of what is installed in the system at the moment. This would go into .install file and you could use part of the code from this answer. Querying directly the {system} table might even work. Store everything as an array using variable_set().

  2. Use hook_modules_enabled (or hook_modules_installed) to compare the new list with the old one. Store differences in a separate array (e.g. array_diff()).

  3. For each of the newly installed/enabled modules perform your action.

  4. Update the custom variable for storing the list of modules using variable_set().

Let me know if I misunderstood the question.

2
  • do you mean I have to take the difference between the module list before installing a new one and after it. In order to get the currently installed module name?
    – user46121
    Commented May 26, 2015 at 12:05
  • Yes, that's exactly what I meant. Commented May 26, 2015 at 12:52

Your Answer

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