Last updated March 26, 2011. Created by merlinofchaos on November 24, 2006.
Edited by linclark. Log in to edit this page.
In order to provide 'default' views for your module, you must implement hook_views_default_views(). This hook simply returns an array of views, and the key is the name of the view.
<?php
function hook_views_default_views() {
$view = new stdClass();
$view->name = 'example_view';
...
$views[$view->name] = $view;
return $views;
}
?>The code to generate a view is exactly the same as that provided by the export view function of the Views UI; in fact, it is extremely common practice, and highly recommended practice, to simply build your view as you like it, export it, and paste the code into your default_views hook. There are only some minor modifications you might want to make to the view.
The first is that it's polite to wrap all of the actual printed text, such as titles, headers, footers, etc, in t(). Views does not do this automatically because doing so could interfere with simple importing. But it does not take very long to identify what text to wrap. However, do not wrap $view->name in t() -- that name has internal meaning and should never be translated.
The second is that you can choose whether or not installing the module makes the view immediately available for use. If you simply cut and paste the view as is, it will be available. However, you can add this piece of code:
$view->disabled = TRUE;In so doing, your default view will only be available on the Views administration page, and must be enabled by the administrator for use. This can be important when your view might interfere with some other function of Drupal. All default views can be disabled by the administrator.
Example:
<?php
function node_views_default_views() {
$view = new stdClass();
$view->name = 'frontpage';
$view->description = t('The basic front page view.');
$view->page = true;
$view->url = 'frontpage';
$view->page_title = '';
$view->page_type = 'teaser';
$view->use_pager = true;
$view->nodes_per_page = variable_get('default_nodes_main', 10);
$view->block = false;
$view->menu = false;
$view->breadcrumb_no_home = true;
$view->sort = array (
array (
'tablename' => 'node',
'field' => 'sticky',
'sortorder' => 'DESC',
'options' => '',
),
array (
'tablename' => 'node',
'field' => 'created',
'sortorder' => 'DESC',
'options' => '',
),
);
$view->argument = array (
array (
'type' => 'node_feed',
'argdefault' => '2',
'title' => '',
'options' => '',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'promote',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
);
$views[$view->name] = $view;
return $views;
}
?>The above view is the generated code of the view that (almost) exactly duplicates the functionality of http://www.example.com/tracker