I'm currently including a .inc file in hook_init(), but now need to call a function from that include inside of hook_node_access(). I've just discovered that, because hook_node_access() runs very early on, my file is not included. I then tried to do it inside of hook_boot() because the documentation suggests that it runs sooner, but it's still not available to me. I'm temporarily including the file in my .module outside of any function via a simple

require_once DRUPAL_ROOT . '/sites/all/modules/...

but would prefer not to do it this way.

  • Is there any reason you're prefer not to do it that way? It's pretty standard practice as far as I've seen (and use myself)
    – Clive
    Jan 11 '13 at 22:57
  • I always use module_load_include() because it returns the path in a more reliable way, i.e. it might not be in /sites/all/modules/mymodule, but could be in /sites/example.com/all/modules/custom/mymodule or something like that, and it's nice to just be able to rely on Drupal knowing the right path. However, it's more lean to do the require_once myself, so there's a benefit there, too. Given your comment and the answer below, I'll stick with the require_once :) Jan 13 '13 at 0:55
  • One last thing, I've actually used an alternative index-ajax.php before which elevates Drupal to a lower bootstrap level to serve custom ajax functions faster (say, if I only need the session bootstrapped), so there are benefits to putting things in hook_init() and hook_boot() so they're really only needed at the appropriate time, and don't add cruft to requests that don't need them. Jan 13 '13 at 0:58

First, using the require include to load your file is perfectly sound, I think. Alternatively, you could add the include to the .info file of your module and allow it to be loaded on every page.

However, to answer your question: We could implement hook_hook_info() from a custom module and add node_access as a group for hook_node_access(). Now, in our module itself, we can have our access callback in a mymod.node_access.inc file.

This is probably not recommended as we are dabbling with core (in a small yet unreliable way) with no real value at all but is a way by which you could do it.

Once you have done this, I'd suggest running the entire Drupal core test suite once (at least node module's) to make sure that we are okay.


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.