0

I think I need to use hook_cron() to cause my new module to be run each time cron runs, but in what file and directory do I place this code? Is it in my new module or some other place.

Logically, it would seem that it should go some place other than my module, but I don't know where even though I've looked at documentation from various web sources. It seems like the "chicken & the egg" problem. If I put hook_cron() in my module how does Drupal know to tell cron to run my module? Maybe it's magic?

I'm using Drupal 8.

  • I'd recommend reading the canonical reference on hooks, it explains all this and lots more: drupal.stackexchange.com/questions/26290/… – Clive Dec 27 '16 at 14:51
  • Reading that again, especially the chicken and egg reference...you're not expecting Drupal to set up the crontab job itself are you? It won't do that, it couldn't, that's something you or your server admin need to do. There's a "poor man's" cron built in, which can run the cron job in a request from a visitor, if the right amount of time has passed since the last execution, but generally speaking it's bad for performance, inconsistent and shouldn't be used for anything other than low traffic brochure-like sites with little to no dynamic content or functionality. A proper crontab job is better – Clive Dec 27 '16 at 14:56
2

All hooks by default go in the .module file, so it should go in the .module file.

It's not clear what you mean by "run my module", as modules aren't 'run', so I can't really give any more info.

  • 2
    Adding to this. When you clear Drupal's cache, it scans through the module files looking for patterns. (that is why hooks have specific names). It detects that your module implements this hook and will call it when needed (when hook_cron is invoked). So place it in the module file and clear the cache. Drupal takes care of the rest. Also be aware that you must rename the hook with the name of your module. So it becomes mymodule_cron() for the pattern to match. – Neograph734 Dec 27 '16 at 14:36
  • The discovery itself is pretty basic too: for any given hook, loop through all the modules installed, see if a function called [modulename]_[hookname] exists. Cache the list for later consumption, clear that cache when appropriate. – Clive Dec 27 '16 at 14:48
  • Where does it get the hookname from though? There is no central registry of hook names that I know of. Does it look for all module_invoke(), module_invoke_all(), and drupal_alter() calls? – Jaypan Dec 27 '16 at 14:55
  • 1
    No, the calling code provides it @Jaypan, it's very low-tech. So the process is: Code calls module_invoke_all('foo'). Drupal checks cache to see if it knows what modules implement hook_foo. If the cache item exists, that gets returned. If not it does the basic discovery process I mentioned, and caches those results for the next call. – Clive Dec 27 '16 at 15:02
  • Makes sense, thanks. So it doesn't actually happen when the cache is cleared, it happens when the hook is invoked. – Jaypan Dec 27 '16 at 15:10

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.