Where is Drupal's Module Hook Calling Started?

2.7k views Asked by At

In a Drupal 7 (and Drupal 6?) system, what "kicks off" the hook calling process, or where are "top level" hook calls located?

As I currently understand the Drupal module system, any module is capable of creating a hook for another module to implement. That means much of Drupal's execution is modules implementing hooks for other modules, which in turn provide hooks for other modules to implement.

What I'm not clear on is if there's a initial, top level hook that gets called in the bootstrap to kick this process off, or if there's several non-module calls that kick-off the hook invoking process, or something else (apologies is this is vague and newbish, but like I said I don't understand)

I looked in the _drupal_bootstrap_full function, and at the end there was a promising

module_invoke_all('init');

However, my search of the modules/ folder only turned up one "init" hook function, which didn't appear to be a kick off point

system/system.api.php
1737:function hook_init() {

function hook_init() {
  drupal_add_css(drupal_get_path('module', 'book') . '/book.css');
}

So, that says to me something outside the module systems kicks this whole things off. Does this happen in a single place, or multiple places. And where are these places?

I'm not currently a heavy Drupal user. My end goal of all this is understanding Drupal's module system in isolation, so that I can then investigate and understand how Drupal uses it's modules the build the application most people think of as Drupal. Any/all explanations are welcome, but I'm trying to understand things from an architectural point of view. I understand you don't need this knowledge to use Drupal, but my brain is broken and won't let me move forward until I know what the base PHP code is doing.

2

There are 2 answers

1
Berdir On BEST ANSWER

The hook system is one separated system inside of Drupal. It is not responsible for the bootstrapping. hook_init() is just the hook that is called at the end of the bootstrap process. As the other answer said, module_invoke_all() can be called anytime, anywhere in the process.

Put simply, in Drupal 7, the following two lines in index.php are responsible for the very basic flow of a request:

<?php
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();
?>

Which can be translated into two steps:

  1. Bootstrap the system. This includes loading all modules and necessary include files, database connection and so on.

  2. Look for the menu router item responsible for this request and execute it.

Someone started a series of blog post to describe it in more details, see http://becircle.com/blog_topics/line_line.

4
Tyler Eaves On

module_invoke_all is where it all happens.

From the doc: Invoke a hook in all enabled modules that implement it.

Init probably isn't a good one as very few define it. Also, remember that hooks are called and not hook.

Edit:

/**
 * Deletes a node type from the database.
 *
 * @param $type
 *   The machine-readable name of the node type to be deleted.
 */
function node_type_delete($type) {
  $info = node_get_types('type', $type);
  db_query("DELETE FROM {node_type} WHERE type = '%s'", $type);
  module_invoke_all('node_type', 'delete', $info);
}

This is in D6 node.module. This is an example of invoking a hook from module code, in this case hook_node_type, with two arguments.