5

I have a hook_form_alter in a module, and it should be firing, but it appears it's not. I've cleared caches and rebuilt the registry. Is there someplace in the database or somewhere else where I can see if my hook is registed? FWIW here is my hook:

/**
 * Implements hook_form_alter
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message($form_id);
}
5
  • is your hook in your my_module.module file ? is your my_module module enabled ? are you actually displaying a form (in other words is there a form to alter) ?
    – rtome
    Commented Apr 13, 2015 at 15:59
  • Yes, yes, and yes.
    – user1359
    Commented Apr 13, 2015 at 16:00
  • You can use module_implements to see if the hook is being implemented by your module.
    – sareed
    Commented Apr 13, 2015 at 16:25
  • How drupal hooks work is a taugh question but there's plenty resources out there that have a go at explaining it. See drupal.org/node/292 , stackoverflow.com/questions/4994512/… and search up for tutorials on hooks like : youtube.com/watch?v=5UdFxcAHm0Y Good luck.
    – rtome
    Commented Apr 13, 2015 at 16:28
  • Modules that are enabled are listed in the 'system' table. Hooks themselves aren't stored anywhere just called. In the case of hook_form_alter, it is called when a form is initiated. When a form is initiated it'll call ALL/ANY hook_form_alter. The system will go through enabled modules and call upon all hook_form_alters(). I'm guessing your module is called 'my_module'. If it is then the other question is, are you trying this hook on a page that has a form? If no forms are initiated this hook does not execute.
    – jerrylow
    Commented Apr 21, 2015 at 17:53

2 Answers 2

10
+100

The hook implementations ARE cached inside the database.. this is why it's necessary to clear cache in order for a newly implemented hook to fire.

Taking a look, inside the cache_bootstrap table, there is a row with cid value of module_implements, and it's data value is a very large serialized string. When unserialized, it is an array of the form

'hook_name' => array('module1', 'module2', 'module3', ...)

The hook names have the hook_ beginning lopped off, so hook_block_info() will appear as block_info. I believe the value is the list of modules which implement that hook, so...

'block_info' => array('views', 'webform', 'my_module')

Means that the following hooks are registered and should be fired:

views_block_info()
webform_block_info()
my_module_block_info()
3
  • I think this could be validated as The One True Hook Cache to Rule Them All by finding a hook you know is firing, using php code to select the serialized strong from the db, unserialize, unset the necessary value for that hook, reserialize it and update the cache_bootstrap table with the new serialized value, then confirm the hook is no longer firing.
    – adfaklsdjf
    Commented Apr 21, 2015 at 18:05
  • How do we bust the hook cache from within the function? I have hook_menu_links_discovered_alter() in a custom module that's trying to add a link to the admin toolbar. And when I put a kint() statement in the hook, it only fires when I clear the cache, and I need it to fire every (administrator) page load.
    – ProGrammar
    Commented Jun 12, 2019 at 12:29
  • 1
    I'm not quite sure offhand what would be the best way to accomplish what you're trying to do--I'm not sure if there's a way to force hooks to always run (let alone only on admin pages) which only run during cache rebuild by design. I suspect you need to implement a different hook, and figure out how to get the link definitions which are passed into the hook you're currently using. hook_init() runs on every non-cached page load and would accomplish that part. Bear in mind that implementing hook_init is discouraged for performance reasons.
    – adfaklsdjf
    Commented Jun 12, 2019 at 13:55
2

If you're running drush, you could try fn-hook. That will print out a list of all the modules that are using a given hook. At the very least this will tell you if the Drupal thinks the module is using the hook

3
  • 1
    The question is "Is there someplace in the database or somewhere else where I can see if my hook is registered?"
    – avpaderno
    Commented Apr 22, 2015 at 10:14
  • Is drush actually storing them, or just looking them up from the modules (or 'somewhere else') when this command is run? I am looking for authoritative places where hooks are stored.
    – user1359
    Commented Apr 22, 2015 at 15:38
  • It uses module_implements to get a listing of the hooks, which intern is checking the cache tables. So as you were looking to avoid using that function, my answer is no longer valid for you. Though it might be useful for someone in the future.
    – Jance
    Commented Apr 22, 2015 at 16:01

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.