Drupal Views is one of the most powerful modules in Drupal. A Drupal view allows you to create dynamic lists of content without writing SQL queries manually. Whether it is a blog listing page, a taxonomy-based listing, or a block showing recent posts, Drupal views handle the heavy lifting.
However, when business requirements extend beyond UI configuration, developers rely on Drupal view hooks to modify how a view behaves. Understanding the execution order of a view helps you decide where and how to apply custom logic without breaking performance or caching.
Why Understanding View Execution Order Matters
Most developers start with the Views UI. It works well for filtering, sorting, contextual filters, and display modes. But sometimes, you need more control:
Add dynamic conditions to the query
Modify results before rendering
Inject custom output
Adjust arguments programmatically
The High-Level Lifecycle of a Drupal View
When a Drupal view runs, it passes through three main phases:
1. Build Phase
The system prepares the query and attaches display handlers.
2. Execute Phase
The SQL query runs, and the results are fetched.
3. Render Phase
The results are formatted and rendered into HTML.
Each phase has specific Drupal views hooks that allow you to intervene safely.
The High-Level Lifecycle of a Drupal View
When a Drupal view runs, it passes through three main phases:
1. Build Phase
The system prepares the query and attaches display handlers.
2. Execute Phase
The SQL query runs, and the results are fetched.
3. Render Phase
The results are formatted and rendered into HTML.
Each phase has specific Drupal views hooks that allow you to intervene safely.
Execution order:
hook_views_pre_view
hook_views_pre_build
hook_views_post_build
hook_views_pre_execute
hook_views_post_execute
hook_views_pre_render
hook_views_post_render
Build Phase Explained
hook_views_pre_view
This is the earliest hook. It runs before processing begins.
You can:
1. Modify arguments
2. Change display ID
3. Add attachments before or after the view
This hook is useful when you need to dynamically change contextual filters.
hook_views_pre_build
Called before the query is fully built.
You can:
1. Adjust exposed filters
2. Modify display settings
hook_views_post_build
Runs after the query is built but before execution.
This is helpful when you want to inspect or make minor adjustments to the query object before it runs.
Execute Phase Explained
hook_views_pre_execute
The query has been built but not executed.
Use this when:
1. Adding additional conditions to the query
2. Modifying joins
3. Changing query logic
This is one of the most common Drupal view hooks for advanced developers.
hook_views_post_execute
The query has run, and results are available in $view->result.
You can:
1. Modify result rows
2. Remove unwanted records
3. Add calculated values
Be careful not to break pagination. Improperly altering results can cause mismatched page counts.
Render Phase Explained
hook_views_pre_render
At this stage:
1. Data is available
2. Handlers have processed results
3. Rendering has not started
You can:
1. Modify fields
2. Add custom markup
3. Change output formatting
Themes often use this hook.
hook_views_post_render
This hook runs after rendering.
You receive:
1. $output as rendered HTML
2. Cache metadata
It is useful when:
1. Replacing tokens
2. Injecting dynamic content
3. Adjusting final markup
Choosing the Right Hook for the Right Task
Understanding when to use which hook makes development easier.
Requirement | Recommended Hook |
Change contextual arguments | hook_views_pre_view |
Modify SQL query | hook_views_pre_execute |
Alter query results | hook_views_post_execute |
Adjust field output | hook_views_pre_render |
Modify final HTML | hook_views_post_render |
What about Drupal hook_views_data?
The Drupal hook_views_data is slightly different. It allows you to define custom tables and relationships for Views. If you create a custom entity or table, you use hook_views_data() to expose it to the Views system. This is not part of the execution lifecycle but extends what Views can access.
Drupal hook node view vs Drupal Views Hooks
Developers often confuse Drupal hook_node_view with Drupal views hooks. They serve completely different purposes.
1. A Drupal hook_node_view modifies how a single node is rendered.
2. Drupal views hooks modify how a list of content is queried and displayed.
Performance and Caching Considerations
When using Drupal view hooks, always think about performance.
1. Avoid heavy logic in post_render.
2. Modify queries in pre_execute rather than altering results later.
3. Respect cache metadata.
4. Do not bypass caching unnecessarily.
Views support caching at the query level and render level. Misusing a hook can disable the benefits of caching.
Debugging Drupal Views Execution
To debug a Drupal view:
1. Enable SQL preview in Views UI.
2. Inspect $view->result.
3. Log $view->build_info.
4. Use debugging tools like Kint.
Understanding which phase you are in helps avoid trial-and-error coding.
Common Mistakes Developers Make
1. Altering results instead of modifying the query.
2. Breaking pagination by removing items post-execution.
3. Ignoring access checks.
4. Modifying rendered output when earlier hooks are more appropriate.
5. Confusing hook view stages.
Using the right Drupal view hooks at the right time prevents these issues.
This is where Drupal views hooks become essential. If you alter data at the wrong stage, you may break pagination, caching, or even performance. Knowing the order ensures clean and predictable customizations.
As a Drupal Developer, you must be aware of one of the contributed projects with a 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 a 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 requirements, which you have achieved using view and supportable View UI.
In this article, I am not going to discuss 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 a 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:
- hook_views_pre_view
- hook_views_pre_build
- hook_views_post_build
- hook_views_pre_execute
- hook_views_post_execute
- hook_views_pre_render
- 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 execution 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 executing 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 rendering 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.
Final Thoughts
A Drupal view follows a clear execution pipeline: build, execute, and render. Each stage provides specific extension points through Drupal views hooks.
By understanding this lifecycle, developers can:
1. Customize queries safely
2. Improve performance
3. Maintain caching
4. Avoid unintended side effects
Whether you are mastering Drupal 8 view hooks and Drupal hook views, you gain full control over how your views behave.