As a Drupal Developer you must have aware of one of the contributed project with most Downloaded number is “Views” What are views? I don’t want to focus more on this, drupal.org gives a broader view of that. 

In Drupal, Views enable you to have an user interface in the browser for creating sections of your website that you would normally have to write an SQL query to retrieve. Views generate a SQL query for you.

You would require view : 

  • If you would like to create the default front page view.
  • You want taxonomy/term view, but probably in some sort of order.
  • You want your posts to be customized
  • You want a block with the 5 most recent posts of a particular type.

Like the above, you have many other requirement. All of them you can achieved using view and supportable View UI.

In his article I am not going to discuss about things like configuration, fields, filter, contextual and filter. These details have already been explained in a previous introductory article about Drupal 8 views.

In this blog, I am going to share something really interesting, that is pipeline or flow of view before it gets rendered to the browser. This would be really helpful with programmatic execution of certain tasks to be performed. But it’s really necessary to know when, where and how it works? And we will go through drupal views hooks.

Basic execution order:

  1. hook_views_pre_view
  2. hook_views_pre_build
  3. hook_views_post_build
  4. hook_views_pre_execute
  5. hook_views_post_execute
  6. hook_views_pre_render
  7. hook_views_post_render

hook_views_pre_view
Allows altering a view at the very beginning of views processing, before other tasks..
Adding output to the view can be accomplished by placing text on $view->attachment_before and $view->attachment_after.

hook_views_pre_view(&$view, &$display_id, &$args)


Parameters

$view: The view object which is about to be processed.

$display_id: The machine name of the active display.

$args: An array of arguments passed to the view.


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

hook_views_pre_build(&$view)

Parameters

$view: The view object about to be processed.


hook_views_post_build
This hook is called right after the build process. The query has been fully built now, 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_post_build(&$view)

Parameters

$view: The view object about to be processed.


hook_views_pre_execute
This hook is called right before the execute process. The query has now been 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_execute(&$view)
    
Parameters

$view: The view object about to be processed.


Hook_views_post_execute
This hook is called right after the execute process. The query is now executed, but the pre_render() phase has not yet been executed for handlers.

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.

hook_views_post_execute(&$view)

Parameters
 

$view: The view object about to be processed.


hook_views_pre_render
The drupal views pre render 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_pre_render(&$view)

Parameters

$view: The view object about to be processed.
 


hook_views_post_render
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.

hook_views_post_render(&$view, &$output, &$cache)

Parameters

$view: The view object about to be processed.

$output: A flat string with the rendered output of the view.

$cache: The cache settings.


All the above phases have a generic flow that ‘view’ follows. Altering a view query to get your choice of data or the output which is not feasible to get it from existing views configuration could be very easy by understanding these phases.

 

comments powered by Disqus