 

Drupal・2 min read・Last update on July 30, 2020

# Fetch all the results of a View | Drupal Views

 

 

 

 ![Fetch all the results of a View | Drupal Views](/sites/default/files/remediation-media/64e7a2b1a761286a5d69808c_rsz_blog_drupal.avif) 

 



 

   Table of contents  - [Approach 1: Function views\_get\_view\_result():](#approach-1-function-views-get-view-result)
- [Approach 2: Executing corresponding SQL Query:](#approach-2-executing-corresponding-sql-query)
- [Final Solution:](#final-solution)
 
  

 

There are scenarios when **outside of a view** we need to fetch results of any particular view. This is a very specific case when we just want the records compiled by Drupal Views. In that case obviously views api or views hooks are of no use, as we are not looking for event driven activities. It’s just the results needs to be fetched using views because of the complexity of the criterion on which these results are computed.

We won’t go for this approach when we want simple results like all the nodes of a specific content type sorted alphabetically. In that case, simply db\_select would be better choice again depending on various project specific factors. In general, we can’t actually tag any approach as the best or optimal for general purpose as these are scenario specifics.

In our case, the scenario is we have a very complex view having good amount of filters or simply i would say the corresponding sql query is complex. Now in that case outside of the view we have two options to get results:

1. **Function views\_get\_view\_result()**
2. **Executing corresponding SQL Query**

### Approach 1: Function views\_get\_view\_result():

 ```

// To get the result of a view.
views_get_view_result($name, $display_id = NULL)  // views.module

```

This looks promising, provided by [views module](https://api.drupal.org/api/views/views.module/function/views_get_view_result/7). Accepts views name and display\_id as parameter and simply fetches you the result. What we wanted is accomplished.

**Limitation:** In case of pagination, probably we want to get all the results and this approach fails there. We cannot fetch all the results if the corresponding view limits results to specific number per page. So, what should we do next!

Let’s look at our second approach, would that be useful in our case?

### Approach 2: Executing corresponding SQL Query:

We can directly execute static sql query, which is a good solution for this scenario. But when we want to enjoy further flexibilities of this approach, say we want to change the query a bit then what? It is possible with this approach but the method is not recommendable. Infact best way to proceed further using this approach is to convert static query into dynamic drupal query which is a tedious process. So, how to achieve this?

### Final Solution:

Looking at how the views work, we got a very promising structured way of solving all our related problems. Views provide several methods for views object which can be used to *get all the results with/without customization* of a particular view. Let’s see how:

 ```

$view = views_get_view(‘example_view’);
$view->build($display_id);
$view->query->limit = 0;
$view->execute();
$results = $view->result;

```

So, we finally have all the results of a view. As far as the above implementation is concerned, we basically are *fetching a view*, then *building a specific display* of that view and after *customization* we are finally *executing* it to get the results. In simple language, we are creating a similar temporary instance to get the desired results.  
Hope this helps you

 



Written by

Purushotam Rai

 

 

 

 

 

 

 ## We'd love to talk about your business objectives

 [ Contact Us  ](/contact)