2

In my custom Drupal 7 module I am using CUSTOM_MODULE_NAME_init() function. But it is not triggered while page execution. Any suggestions?

2
  • 1
    you'll get better answers if we can see your code. try gist.github.com or drupalbin.com Aug 23, 2011 at 11:39
  • I'm not sure if this will apply to you, I am running Drupal 6, but my problems with this were fixed when I adjusted the module weight in the system table in the DB.
    – AllisonC
    May 25, 2018 at 13:21

3 Answers 3

6

Hook implementations are cached in Drupal 7. After implementing a new hook, you need to clear the cache. See What method is used to clear caches in the Drupal? for ways to do that.

1
  • Yes. I cleared the cache. But not invoked
    – Vinoth
    Aug 23, 2011 at 10:28
3
  1. Make sure that the module is enabled (don't laugh, I forget that sometimes).
  2. Make sure that the page is not served from cache. Use hook_boot if you need to perform setup tasks for cached pages.
  3. Use a debugger to step through the execution. hook_init is invoked at the end of _drupal_bootstrap_full.
2
  • How to debug the execution?
    – Vinoth
    Aug 23, 2011 at 10:28
  • Caveman debugging: put print_r statements at relevant places of the code to get feedback about which parts of the code are executed and what values the variables hold. Because this is tedious if done often, a better solution is to install a debugging extension on the server, e.g. Xdebug and using an IDE that can controll the debugger, e.g. Eclipse PDT.
    – Oswald
    Aug 23, 2011 at 10:40
2

As reported in the documentation for hook_init(), the hook is not invoked on cached pages. This means that, if the content of a page is found in cache, the hook implemented by your module is not invoked.

This hook is run at the beginning of the page request. It is typically used to set up global parameters which are needed later in the request. when this hook is called, all modules are already loaded in memory.

This hook is not run on cached pages.

To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

Your Answer

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

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