Drupal 7 page loading process explained

In looking for an answer to this question, most posts say something like: Drupal is almost entirely modular, and driven by lots of "hooks" and what code is actually executed when is deep Drupal Voodo. Or they say RTFM. I think the F stands for Fantastic, but it could also be Fragmented. This is my attempt to understand what hooks get called when in the delivery of a simple page (like this one).

Dealing with the page request

index.php does a bootstrap and calls menu_execute_active_handler(), which importantly calls menu_get_item() for the path you entered, and delivers the result. menu_get_item() does as follows

  1. loads a "router_item" array for generic node access (SELECT * FROM menu_router WHERE path='node/%' if you care to inspect).
  2. The router_item contains a load_functions key which for our simple case contains the name of the node_load() function.
  3. _menu_translate() is called for the router_item which importantly calls _menu_load_objects which is where our node_load() function is finally called.

The first set of useful hooks are when the node is loaded.

node_load($nid) is ultimately a wrapper function and the work is done by a controller class for Node objects, NodeController.  It deals with loading all the data from the database, offering the following hooks along the way:

  1. hook_load() in our case (loading a normal node) this does not apply.
  2. hook_entity_load() a very generic hook.
  3. hook_node_load() can add to, but not change, loaded data.

This loaded node, with all its attached fields, is then present in the $router_item[ 'map' ][1] and this is returned by menu_get_item back up to menu_execute_active_handler, which then calls the page_callback function, node_page_view(). This sets node->title and returns the output of node_show() which in turn passes on to node_view_multiple()...

The next set of useful hooks are when the node is viewed.

This list is from the Node API docs:

At the end of this lot you're left with a render array structure like this (Nb. 123=node id):

Array( 'nodes' => Array(
              123 => Array(
                     'body' => Array(
                               '#entity_type' => 'node',
                               '#node' => {node object 123}
                               'type' => {node type, e.g. article}
                    'field_yourfieldname' => Array( ... ),
                     '#node' => {node object 123}

Finally, theme the page

Finally,  menu_execute_active_handler() calls drupal_deliver_page which offers  hook_page_build() calls dupal_deliver_html_page which essentially calls drupal_render_page is where all the themeing comes in.

  1. hook_page_build() (again?!) Note that this includes the block module's block_page_build() which rearranges the render array into regions ('content', 'sidebar_first' etc.).
  2. hook_page_alter()  is called via drupal_alter('page', $page).
  3. Finally, the main theme function is started: drupal_render($page) which recursively iterates over each of the array elements, generating HTML code.
Note: the structured array that is passed around changes purpose/form; it is a "render array" by this point. After block_page_build() it is keyed by regions, and by the time it gets to theme hooks like hook_process_page() all that stuff is demoted under a top level 'page' key. References are used a lot so there is more than one path to some of the values. Render arrays ignore keys prefixed with #, which is why these can be used for meta data and useful objects, e.g. top level key #node stores the un-rendered node object.

Theming hooks are all listed on one (long) page at: Drupal 7 theme function.




i want a detailed description for all the major hooks that we use mostly.

abc replied on

Well I guess the ones that get used the most will depend a lot on the type of projects you usually undertake. The Drupal API site http://api.drupal.org/ is pretty good.

Rich replied on

Add new comment