Community Documentation

Creating views programmatically in Views 2

Last updated January 15, 2010. Created by thaddeusmt on January 15, 2010.
Log in to edit this page.

To create a View via code with Views 2 in Drupal 6:

  1. Create the view using the View UI like you normally would
  2. Export it to get the code
  3. Replace the first line ($view = new view;) with this: $view = views_new_view();
  4. Then display it, execute it, embed it, or whatever you want (i.e. $view->execute_display('default', array())

Here is an example using a simple View that displays the Title field of all Published nodes:

<?php
//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());
?>

Comments

It should also be noted, that

It should also be noted, that if you want to save the view to the database, it's as simple as this:

$view->save();

Thanks for the extra note

Now I can make custom, complicated views and use these as the filter for a node reference field.
Cheers!

It is very good example

Is there any simple syntax available like following we can pas the arguments and got the result as we want. In this example no field selection option. In your example it is possible. I use if condition before the field display, if it allowed display other wise skipped. If you know the easy way like following then please share

<?php
$view
= 'viewName';
$arg#1 = 'value';
$arg#2 = 'value';
$arg =array($arg#1, $arg#2);
$view = views_get_view($view);
print
$view->preview('default', $arg);
?>

Simple method to start to implement a default view

Step 1

Change

$view->disabled = FALSE;
to
$view->disabled = TRUE;

Step 2

Change

// now output the view (or whatever you want to do with it)
print $view->execute_display('default', array());
to
// now output the view (or whatever you want to do with it)
$views[$view->name] = $view;
return $views;

Step 3

Put the code in your own module in a file named with mymodule.views_default.inc. Place this file in the includes directory. This directory you are defined in mymodule.module with

/**
* Implementation of hook_views_api().
*/
function mymodule_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'mymodule') .'/includes'
  );
}

Step 4

Empty the cache to see the default view in the list of deactivated views (admin/build/views).

Thanks for this code, it gave

Thanks for this code, it gave me an php error though. The code below works for me.
Guus van de Wal

<?php
//create a new view
//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());
?>

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders

Structure Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here