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.

3 Answers 3

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.

1
  • Thank you for the answer. How do I limit this to run only for non-admin pages?
    – rexposadas
    Jun 21, 2012 at 21:15
11

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.

5
  • 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.
    – AKS
    Nov 22, 2014 at 17:34
  • Does hook_page_build run on cached pages?
    – Teegan
    Aug 12, 2015 at 2:46
  • 1
    @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. Aug 13, 2015 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. Aug 13, 2015 at 8:37
  • hook_page_build is also removed in drupal 8, aparently there is now hook_page_bottom and hook_page_top, but they are not independend of the page caching.
    – Andreas
    Aug 31, 2021 at 12:39
0

Now with drupal 8 and 9, the approach outlined in this great blog post from chromatichq.com gives an excellent overview on how to execute code ignoring the cache.

If you require access to the user or session thou, it will also not help, as the http_middleware is very low level.

2
  • Please consider posting the gist of the linked article to your answer in order to remain relevant even if the link breaks in the future.
    – wranvaud
    Feb 7, 2022 at 14:52
  • Hi @wranvaud, thx for the suggestion :) .Please feel free to edit the answer as you see fit
    – Andreas
    Feb 7, 2022 at 15:54

Your Answer

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

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