Skip to content

Instantly share code, notes, and snippets.

@asanchez75
Created September 27, 2012 16:55
Show Gist options
  • Save asanchez75/3795097 to your computer and use it in GitHub Desktop.
Save asanchez75/3795097 to your computer and use it in GitHub Desktop.
Views
print only fields free for html tags
$view = views_get_view('noticias');
$view->set_display('block_2');
$view->execute();
dpm($view->result);
print full output of field with html tags
$view = views_get_view('noticias');
dpm($view->execute_display('block_2'));
// more examples
// http://karenziv.com/2011/10/drupal-calling-a-view-programmatically/
I have a situation where I needed to call a view programmatically to output its results in a way that Views can’t natively. That’s pretty well documented in the wild, but I had a couple special factors that I needed to account for.
Building the View
My view needed to get all published nodes of a certain content type that were updated on or after a given date. I started with a base view and created a new block display, added my fields and the content type and status filters. Now, the updated date is chosen by the user which normally means using arguments, but arguments can only handle equality comparisons. This means I had to use an exposed filter. I saved the view, tested, everything ok.
Calling the View
First, I just retrieved the view to make sure it worked.
$view = views_get_view('foo');
$view->execute();
$response = $view->result;
By default, views_get_view() returns the default display, but in my case, I needed to return the block display that I had just created. Regardless of the title and name you give a display, Views assigns a unique key for each display based on its type and order. In this case, the display I had built was the first block, so its key was block_1.
$display = 'block_1';
$view = views_get_view('foo');
$view->set_display($display);
$view->execute();
$response = $view->result;
Filtering Results
Ok, that’s great, but what about my filter? Adding it was straightforward but it wasn’t filtering rows based on the $delta value. It turns out that when you make a filter based on the updated date, Views prefers the date in a “machine readable format”, according to the UI. This evidently doesn’t include unix timestamps. Once I formatted the timestamp to a string, it worked.
$display = 'block_1';
$filter_name = 'changed';
$view = views_get_view('foo');
$view->set_display($display);
$filter = $view->get_item($view->current_display, 'filter', $filter_name);
$filter['value']['value'] = date('r', $delta);
$view->set_item($view->current_display, 'filter', $filter_name, $filter);
$view->execute();
$response = $view->result;
Getting All Results
For some reason, even though the display I created was set to return unlimited results, verified in the UI, calling the view programmatically returned only 10 rows. I still have no clue why it ignores that setting, but there’s a way around it. Here’s the completed code!
$display = 'block_1';
$filter_name = 'changed';
$view = views_get_view('foo');
$view->set_display($display);
$view->set_items_per_page(0);
$filter = $view->get_item($view->current_display, 'filter', $filter_name);
$filter['value']['value'] = date('r', $delta);
$view->set_item($view->current_display, 'filter', $filter_name, $filter);
$view->execute();
$response = $view->result;
===========================================================================================
<?php
$view = views_get_view('view_name'); // fetch the view
$display_id = 'default'; // chose the display type
$view->set_display($display_id);
$view->is_cacheable = FALSE;
$item = $view->get_item($display_id, 'filter', 'distance');// telling the view that we are fetching filter. distance is the filter name. We can check the filter name from view export as well.
$item['value'] = array('postal_code'=>'00210', 'search_distance'=>'100', 'search_units'=>'mile'); // passing values to all the exposed filter. We can pass other options like operator etc (not just value) as well.
$view->set_item($display_id, 'filter', 'distance', $item); // set item
$view->is_cacheable = FALSE;
$output = $view->render(); // render
print $output; // display output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment