In my custom Drupal 7 module I am using CUSTOM_MODULE_NAME_init()
function. But it is not triggered while page execution. Any suggestions?
-
1you'll get better answers if we can see your code. try gist.github.com or drupalbin.com– Capi EtherielAug 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.– AllisonCMay 25, 2018 at 13:21
3 Answers
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.
- Make sure that the module is enabled (don't laugh, I forget that sometimes).
- Make sure that the page is not served from cache. Use hook_boot if you need to perform setup tasks for cached pages.
- Use a debugger to step through the execution. hook_init is invoked at the end of _drupal_bootstrap_full.
-
-
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.– OswaldAug 23, 2011 at 10:40
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.