Drupal - Get View results in PHP code

Kevin's picture

[b]Q:[/b] How can I programmatically invoke a view and retrieve its result/output data in PHP?
[b]A:[/b] Use the new(ish) views function - views_get_view_result(), and access the result data in the array that it returns.

Sometimes instead of just displaying the output of a view in a page or a block etc., it might be handy to be able to call the view from PHP and get access to the data that it finds without the need to display it. This type of functionality could allow you to do some post processing or filtering on the data before displaying it, and it could also reduce the need to write custom database queries for programmatically retrieving node and user data.

Well that’s where the views function views_get_view_result() comes in, I first came across this function in this post:

http://drupal.org/node/342132

I had been wondering about the availability of functionality like this in drupal views for a while and this function firmly ticks the box! The function is available in views v2.8 and greater (~December 2009).

Anyway you just call it passing in the view and display names (optionally with view arguments) and it executes the view and returns an array with the result data in it! The article does a good job of explaining how to use the function and get at the returned data.

The easiest way to get up and running is to create a 'node view'. With node views views_get_view_result() returns an array of objects that contain the node Ids, you can then use these nids to access the nodes' data by first calling node_load(), for example:

// Invoke the view which returns all estates that have
// a province specified.
$estates = views_get_view_result('estates_with_province');

// Get a list of Irish provinces from the location module
$provinces = location_get_provinces('ie');

$options = array();

// Loop through the results and fill the options
// array with the province id and name.
foreach ($estates as $estate) {
// Load up the estate node so that we can
// access its data.
$node = node_load($estate->nid);

// Pull out the province id
$pid = $node->locations[0]['province'];

// Add to the options array fro the select/dropdown
$options[$pid] = $provinces[$pid];
}

I have used it for filling select options on custom forms, it saves me from having to hand craft complex database queries - I just use the Views UI get the a view working and serving up the required data and then get the data into my PHP via views_get_view_result()!

Anyway, I have found it to be of great use and if you're looking for similar functionality I can heartily recommend it!

Share this post: 

Recent Work

Some of Ireland's biggest brands use Pride Design to Design, Build, Manage, Market and Maintain their websites.

Website design for St.Patricks Credit Union in Dublin, Including Loan Calculators.

Website design and development for a step by step wedding and other stationary ordering website f

New website designed and developed for Cork's newest café, which is based in Merchants Quay in Co

New Website design for Pain Relief clinic based in Cork City.

Website for a busy Dublin city centre pub.

We redrafted the Ballymaloe house website to modernise the look and feel of the site and to also

We revamped the website for the Ballymaloe Cookery School.

We revamped the Cully & Sully website onto Drupal.

Rachel Allen was brought up in Dublin and left home at eighteen to study at the world-famous Ball

Clóna Dairy Products are a Dairy Company based in Clonakilty, Co. Cork, since 1919.

Project Arts Centre is Ireland’s leading centre for the presentation and development of c