Web Development Blog

How to programmatically create Views in Drupal 6

01.14.2010 10:08PM by Evan Johnson

So, this stumped me for a while. I needed my small custom Drupal module to select a bunch of nodes based on a CCK Date field and the Publish status. It is a pain to query the CCK tables directly, so every forum post I could find told me to do this via Views. That’s all well and good, but the instructions for doing this all involved creating a View with the Vuiews UI, then calling it to make my query.

But I wanted to have some of the View’s query parameters change dynamically based on my module’s admin settings. Although you could probably do this with some custom Views Argument PHP code, I didn’t want to do it this way. It divorced the code from my module too much somehow. It would mean that after I installed my module I would have to create a View and paste in some custom PHP code! Yuck.

Looking through the Views 2 documentation I learned how to create "default views" for a module, but still I didn’t like this approach. This would mean that the custom view would still appear in the Views list, and could be disabled, modified, and what have you. What I really wanted to do was just create a View programmatically in my module code. How hard could it be?

With Views 1 it was apparently easy to do this with the views_build_view() method, and I found many articles explaining how. But I am using Views 2, so these were of no help.

Some poking around in the Views code showed me the way, however, and it turns out it’s pretty easy after all.

Basically, all you need to do is create a view using the Views UI then Export it to get most of the code. You can’t quite use the exported code as-is though. You need to replace the first line of the export code ($view = new view;) with ($view = views_new_view();). It basically does the same thing. Once you’ve replaced that line you can create a view anywhere you want in your module’s code. You can then execute, embed it, or whatever you want by calling the appropriate functions (like $view->execute_display('default', array())). Here is a piece of example code using a simple view that displays the Title field of all Published nodes:

//create a new view
$view = views_new_view();
//define the view (this code was generated by the Export)
$view->name = 'test_date_view';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'title' => array(
	'label' => 'Title',
	'alter' => array(
	  'alter_text' => 0,
	  'text' => '',
	  'make_link' => 0,
	  'path' => '',
	  'alt' => '',
	  'prefix' => '',
	  'suffix' => '',
	  'help' => '',
	  'trim' => 0,
	  'max_length' => '',
	  'word_boundary' => 1,
	  'ellipsis' => 1,
	  'html' => 0,
	),
	'link_to_node' => 1,
	'exclude' => 0,
	'id' => 'title',
	'table' => 'node',
	'field' => 'title',
	'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'status' => array(
	'operator' => '=',
	'value' => '1',
	'group' => '0',
	'exposed' => FALSE,
	'expose' => array(
	  'operator' => FALSE,
	  'label' => '',
	),
	'id' => 'status',
	'table' => 'node',
	'field' => 'status',
	'relationship' => 'none',
  ),
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('row_options', array(
  'inline' => array(),
  'separator' => '',
));
// now output the view (or whatever you want to do with it)
print $view->execute_display('default', array());

I posted this over in the Drupal documentation as well.

Comment

Gravatar Image
On Apr 29, 09:55 AM, (Link)
David said:

Hi, I am trying to execute the design that you mentioned for programatically creating views. How can I run this code? Also, do I need to add a save function? I was planning on creating multiple views through 1 script. Is that doable? Thanks for your help, David

Gravatar Image
On May 5, 06:15 PM, (Link)
Evan Johnson said:

You run this code anywhere you run PHP. Most likely you will use this code in a custom Module. In the example I am printing it, but you could also make it into a function and have that function return the view’s results. The basic idea is that instead of writing and executing an SQL query to get a result set, you can use a "view". I am creating a temporary and invisible "view" just for the purpose of the query, not a regular "view" that appears in the Views backend area. There is nothing to "save" here, unless you want to save the results.

If you are trying to create multiple default views for a module which will be visible and editable by users in the backend, use the hook_views_default_views() hook. I think this might be what you are trying to do. Check out this link for information on how to do this: http://drupal.org/node/99568

  Subscribe to article comments