7

I would like to run a check on each page load. This only concerns the front end and not the admin dashboard.

Basically what I would like to do is: 1) User hits a URL in the drupal site. 2) A server-side check is ran. If the check passes, run some logic.

How would I accomplish this?

I'm guessing that I can use a hook, but what hook would that be?

Thanks.

14

hook_init() is run on every page load except for cached pages, so depending on your use case that might be an option.

function MYMODULE_init() {
  // Custom logic
}

hook_boot() is run on every page, including cached pages, so might be more useful (again depending on your use case). However if you have complex logic to process this will obviously slow down the build of every page, even those that are cached.

function MYMODULE_boot() {
  // Custom logic
}

If you want to check whether or not the current path falls under '/admin' you can use a check like this in either function:

if (arg(0) == 'admin') {
  // ...
}

This won't work for node edit pages, for example, who's path doesn't have '/admin' in it; but short of invoking hook_admin_paths() and testing the current URL against each of the paths returned, I'm not sure how you'd reliably test for that anyway.

  • Thank you for the answer. How do I limit this to run only for non-admin pages? – rexposadas Jun 21 '12 at 21:15
9

I wanted to contribute something additionally to this. I am not an advocate of hook_init() or hook_boot() for doing tasks like this but tend to look for specific hooks that accomplish what you want. These two hooks are very broad and while they do they job they are also sometimes called multiple times per page load.

An alternative I would propose is hook_page_build().

This will run on page builds and you don't have to worry about running things twice when you may not need to. I made a script for development sites where it would simply inject a bot restricting header line site-wide on pages. Likewise you could do the same as a check on each page load.

function my_module_page_build(&$page) {
  drupal_add_http_header('X-Robots-Tag', 'noindex,nofollow');
}

Similarly you could include a check for administrative pages

function my_module_page_build(&$page) {
  if (path_is_admin()) {
    foo();
  } 
  else {
    bar();
  }
}

I prefer path_is_admin() as opposed to checking arbitrary arguments via arg(). Just my preference. Feels less hackish that way IMO.

  • hook_page_build/alters are fine to modify the content (headers, content, etc). But OP asked to run this on every page load, which includes Ajax calls that do not run hook_page_* hooks. – AyeshK Nov 22 '14 at 17:34
  • Does hook_page_build run on cached pages? – Teegan Aug 12 '15 at 2:46
  • @AyeshK -- Keep in mind the user was asking about users hitting pages - not AJAX requests. There is almost never a good reason to use hook_init(). It has been removed from Drupal 8 - so relying on this is not recommended. – ElusiveMind Aug 13 '15 at 8:33
  • @Teegan -- I am not honestly sure and without digging into Drupal's core at the moment, I would guess that it does - possibly not if using Boost or some other static page caching. – ElusiveMind Aug 13 '15 at 8:37

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.