Drupal module views hook sequence

2011
Drupal module views hook sequence

Views is an amazing module not only because of its UI for query generating but also as it provides comprehensive hooks.

Sometimes these hooks confuse beginners with their sequence.

Here is the processing sequence of these hooks for your convenience.

  • hook_views_pre_view (&$view)

    /** * This hook is called at the very beginning of views processing, * before anything is done. * * Adding output to the view can be accomplished by placing text on * $view->attachment_before and $view->attachment_after. */

  • hook_views_pre_build (&$view)

    /** * This hook is called right before the build process, but after displays * are attached and the display performs its pre_execute phase. * * Adding output to the view can be accomplished by placing text on * $view->attachment_before and $view->attachment_after. */

  • hook_views_query_alter (&$view, &$query)

    /** * Stub hook documentation * * This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. * This must either be in the same directory as the .module file or in a subdirectory * named 'includes'. * */

  • hook_views_pre_execute (&$view)

    /** * This hook is called right before the execute process. The query is * now fully built, but it has not yet been run through db_rewrite_sql. * * Adding output to the view can be accomplished by placing text on * $view->attachment_before and $view->attachment_after. */

  • hook_views_pre_render (&$view)

    /** * This hook is called right before the render process. The query has * been executed, and the pre_render() phase has already happened for * handlers, so all data should be available. * * Adding output to the view can be accomplished by placing text on * $view->attachment_before and $view->attachment_after. Altering the * content can be achieved by editing the items of $view->result. * * This hook can be utilized by themes. */

  • hook_views_post_render (&$view)

    /** * Post process any rendered data. * * This can be valuable to be able to cache a view and still have some level of * dynamic output. In an ideal world, the actual output will include HTML * comment based tokens, and then the post process can replace those tokens. * * Example usage. If it is known that the view is a node view and that the * primary field will be a nid, you can do something like this: * * * * And then in the post render, create an array with the text that should * go there: * * strtr($output, array('', 'output for FIELD of nid 1'); * * All of the cached result data will be available in $view->result, as well, * so all ids used in the query should be discoverable. * * This hook can be utilized by themes. */

Leave a comment