Drupal: Creating views programmatically in Views 2
Task:
Create a view via code with Views (in your custom module).
Solution
To create a view via code with Views 2 in Drupal 6 you should:
- Create a view using Viws user interface as you usually do this;
- Export the view and get a code;
- Replace the first string ($view = new view;) with $view = views_new_view();
- Then you can display it, execute it, embed it and whatever you want (i.e. $view->execute_display(‘default’, array())
Here is an example using a simple View that displays Title field of all Published nodes:
//create new view
$view = views_new_view();
//define the view (this code was created through 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’ => ”,
));
// output the view
print $view->execute_display(‘default’, array());
How can you use this in your module?
/*
* Implementation of hook_views_api()
*/
function MODULE_NAME_views_api() {
return array(‘api’ => 2.0);
}
/*
* Implementation of hook_views_default_views()
*/
function MODULE_NAME_views_default_views(){
$view = new view;
// Insert view export here
$views[$view->name] = $view;
return $views;
}
There is an inconsistency between the two samples of code above.
The first says
$view = views_new_view();
and
print $view->execute_display(‘default’, array());
and second says:
$view = new view;
and
$views[$view->name] = $view;
return $views;
Are the first few lines of code and the second few lines of code exclusive to each other?
[…] programmatically in Views 2 | TemplateZineHow to create a view via code with Views 2 on Drupal 6.0http://www.templatezine.com/20 ..Theming Views in Drupal with Templates and Preprocess Functions …If you’re building a […]
From where I can call this function MODULE_NAME_views_api() and function MODULE_NAME_views_default_views() functions?? Or it will be automatically called? I tried this way in drupal7 but not working