1

I'm building a module that needs to do stuff wherever the user is browsing, but I used hook_nodeapi(), which is ordinarily great but it is not coping with non-node pages (such as views, and user profile pages). Does anyone have any idea on which hook should I run in such circumstances?

2

You have three options:

  • hook_init() This hook is not run on cached pages - though CSS or JS added this way will be present on a cached page.
  • hook_boot() Only use this hook if your code must run even for cached page views.
  • hook_exit() This hook is run at the end of each page request. It is often used for page logging and printing out debugging information. Only use this hook if your code must run even for cached page views.

Performancewise, you will want to avoid hook_boot() and hook_exit(), since they will run regardless of caching. Having one module that implements those hooks brings the familiar warning on the performance page, "The following enabled modules are incompatible with aggressive mode caching and will not function properly: …"

If all you need to do, is add some Javascript, static HTML, or CSS, hook_init() is the best option: the JavaScript and CSS you add will be added to the cache and be present.

If, however, you need to run actual PHP that is not needed for the rest of the request, e.g. to write a record in a database, or log a line to some file, then hook_exit() is the best option: it will only be called after the page is already served.

If you need to run some setup: e.g. establish a connection with a service, perform access-check and such, then hook_exit() is the best option.

1
  • Sorry. That was a really stupid and confusing copy-paste mistake. Fixed! Sorry again! – berkes Aug 25 '11 at 8:41
0

If you're looking for something to run on every page request, one option is to use hook_init().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy