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
- loads a "router_item" array for generic node access (
SELECT * FROM menu_router WHERE path='node/%'
if you care to inspect). - The
router_item
contains aload_functions
key which for our simple case contains the name of thenode_load
() function. _menu_translate()
is called for therouter_item
which importantly calls_menu_load_objects
which is where ournode_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:
- hook_load() in our case (loading a normal node) this does not apply.
- hook_entity_load() a very generic hook.
- 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:
- hook_view() (node-type-specific)
- field_attach_prepare_view()
- hook_entity_prepare_view()
- field_attach_view()
- hook_node_view()
- hook_entity_view()
- hook_node_view_alter()
- hook_entity_view_alter()
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.
- 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.).
- hook_page_alter() is called via
drupal_alter('page', $page)
. - Finally, the main theme function is started: drupal_render($page) which recursively iterates over each of the array elements, generating HTML code.
Theming hooks are all listed on one (long) page at: Drupal 7 theme function.
Comments
i want a detailed description for all the major hooks that we use mostly.
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.
Add new comment