I have module A installed that defines some custom hooks. Module A wants to be notified when another module B is installed, so that it can inspect and see if module B implements its hooks (for building a cache of some info for example).

Is there a hook like "hook_module_enabled($module_name)" ?

migrated from stackoverflow.com Aug 4 '11 at 4:45

This question came from our site for professional and enthusiast programmers.


You should do it the other way around.

When your module is enabled, it should check for other modules that has implemented it's hook and call them to build up the cache you need.

Then you could create an API function for other modules to call when their module is activated.

It makes more sense to build it like that. You never know when the cache is reset anyways, so you can never rely on having a complete cache data all the time anyways.

Another solution would be to build the cache when it's needed. It doesn't make sense to build the cache data before it's actually needed. The normal thing to do with cache in drupal is to check if the cache data is there, if it's not create it and save it in the cache for next time. That would spare you the headache of trying to know all.

To answer your question, there is to my knowledge not a way to hook into the process of modules being installed / enabled.

For Drupal 7 there have actually been created a hook to react on modules being enabled: hook_modules_enabled()

  • Thanks for the detail googletrop. For the drupal 7 hook_modules_enabled() i saw an issue about that but didn't know it was commited ! drupal.org/node/836748 I guess your second suggestion is the way things like views work right ? Since caches are flushed after module installation, i might go with this approach (lazy cache) – redben Dec 13 '10 at 13:39

You should generally check for modules implementing hooks when you actually call the hooks, not before, by calling module_implements() (to get a list of modules that implement the hook) or module_envoke_all() (to directly run the hook on all implementing modules).

That said, you can also tell modules that implement your hook that they need to notify your module when they're installed. content_notify() does this for CCK, for example.

  • Yeah i thought about doing it that way (like CCK's content_notify) but i wanted my "core" module to be active in the process (not the implementing module) – redben Dec 13 '10 at 13:44

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.