Drupal: .htaccess Access Deny
August 21, 2014 – 7:59 am | No Comment

In this article I will tell how to forbid access to certain resources for some clients. The instructions will include descriptions of different directives.

Read the full story »
CSS Templates

Contain reviews and news about CSS Templates.

Freebies

Contain freebies such as icons, graphics, headers and images for your websites.

Fun Stuff

Contains other fun stuff for entertainment or interesting site showcase.

How-To

Contain technical elaborations on some specific workarounds or common tweak.

Joomla Templates

Contains reviews and news about Joomla templates.

Home » How-To

Drupal: Creating views programmatically in Views 2

Submitted by on March 30, 2010 – 6:51 am3 Comments

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:

  1. Create a view using Viws user interface as you usually do this;
  2. Export the view and get a code;
  3. Replace the first string ($view = new view;) with $view = views_new_view();
  4. 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;
}

3 Comments »

  • Tom Sherlock says:

    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

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.