function hook_views_default_views

You are here

7 views.api.php hook_views_default_views()
6 docs.php hook_views_default_views()

This hook allows modules to provide their own views which can either be used as-is or as a "starter" for users to build from.

This hook should be placed in MODULENAME.views_default.inc and it will be auto-loaded. MODULENAME.views_default.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

The $view->disabled boolean flag indicates whether the View should be enabled (FALSE) or disabled (TRUE) by default.

Return value

An associative array containing the structures of views, as generated from the Export tab, keyed by the view name. A best practice is to go through and add t() to all title and label strings, with the exception of menu strings.

Related topics

6 functions implement hook_views_default_views()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

comment_views_default_views in modules/comment.views_default.inc
Implementation of hook_views_default_views()
node_views_default_views in modules/node.views_default.inc
Implementation of hook_views_default_views()
search_views_default_views in modules/search.views_default.inc
Implementation of hook_views_default_views()
statistics_views_default_views in modules/statistics.views_default.inc
Implementation of hook_views_default_views()
taxonomy_views_default_views in modules/taxonomy.views_default.inc
Implementation of hook_views_default_views()

... See full list

File

./views.api.php, line 756
Describe hooks provided by the Views module.

Code

function hook_views_default_views() {
  // Begin copy and paste of output from the Export tab of a view.
  $view = new view;
  $view->name = 'frontpage';
  $view->description = 'Emulates the default Drupal front page; you may set the default home page path to this view to make it your front page.';
  $view->tag = 'default';
  $view->base_table = 'node';
  $view->human_name = 'Front page';
  $view->core = 0;
  $view->api_version = '3.0';
  $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

  /* Display: Master */
  $handler = $view->new_display('default', 'Master', 'default');
  $handler->display->display_options['access']['type'] = 'none';
  $handler->display->display_options['cache']['type'] = 'none';
  $handler->display->display_options['query']['type'] = 'views_query';
  $handler->display->display_options['query']['options']['query_comment'] = FALSE;
  $handler->display->display_options['exposed_form']['type'] = 'basic';
  $handler->display->display_options['pager']['type'] = 'full';
  $handler->display->display_options['style_plugin'] = 'default';
  $handler->display->display_options['row_plugin'] = 'node';
  /* Sort criterion: Content: Sticky */
  $handler->display->display_options['sorts']['sticky']['id'] = 'sticky';
  $handler->display->display_options['sorts']['sticky']['table'] = 'node';
  $handler->display->display_options['sorts']['sticky']['field'] = 'sticky';
  $handler->display->display_options['sorts']['sticky']['order'] = 'DESC';
  /* Sort criterion: Content: Post date */
  $handler->display->display_options['sorts']['created']['id'] = 'created';
  $handler->display->display_options['sorts']['created']['table'] = 'node';
  $handler->display->display_options['sorts']['created']['field'] = 'created';
  $handler->display->display_options['sorts']['created']['order'] = 'DESC';
  /* Filter criterion: Content: Promoted to front page */
  $handler->display->display_options['filters']['promote']['id'] = 'promote';
  $handler->display->display_options['filters']['promote']['table'] = 'node';
  $handler->display->display_options['filters']['promote']['field'] = 'promote';
  $handler->display->display_options['filters']['promote']['value'] = '1';
  $handler->display->display_options['filters']['promote']['group'] = 0;
  $handler->display->display_options['filters']['promote']['expose']['operator'] = FALSE;
  /* Filter criterion: Content: Published */
  $handler->display->display_options['filters']['status']['id'] = 'status';
  $handler->display->display_options['filters']['status']['table'] = 'node';
  $handler->display->display_options['filters']['status']['field'] = 'status';
  $handler->display->display_options['filters']['status']['value'] = '1';
  $handler->display->display_options['filters']['status']['group'] = 0;
  $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

  /* Display: Page */
  $handler = $view->new_display('page', 'Page', 'page');
  $handler->display->display_options['path'] = 'frontpage';

  /* Display: Feed */
  $handler = $view->new_display('feed', 'Feed', 'feed');
  $handler->display->display_options['defaults']['title'] = FALSE;
  $handler->display->display_options['title'] = 'Front page feed';
  $handler->display->display_options['pager']['type'] = 'some';
  $handler->display->display_options['style_plugin'] = 'rss';
  $handler->display->display_options['row_plugin'] = 'node_rss';
  $handler->display->display_options['path'] = 'rss.xml';
  $handler->display->display_options['displays'] = array(
    'default' => 'default',
    'page' => 'page',
  );
  $handler->display->display_options['sitename_title'] = '1';

  // (Export ends here.)

  // Add view to list of views to provide.
  $views[$view->name] = $view;

  // ...Repeat all of the above for each view the module should provide.

  // At the end, return array of default views.
  return $views;
}

Comments

Be sure to declare hook_views_api() or this hook won't get fired.

http://api.drupal.org/api/views/views.api.php/function/hook_views_api/7

I believe the filename specified above is incorrect.

This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

This should read:

This hook should be placed in MODULENAME.views_default.inc and it will be auto-loaded. MODULENAME.views_default.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

See http://drupal.org/node/1849474 for description of problems encountered when the file is named MODULENAME.views.inc.

How to provide multiple views? basically the above example illustrates only one view.

Basically, where the code example says...

// ...Repeat all of the above for each view the module should provide.

...create a new view as before (using a distinct name etc.) and add it to the returned $views array as demonstrated in the line preceding the comment.

Is there a way to provide different file for each view ?
something like include(), so i can stored each view per file, sometime i forgot when replace a view by a new one.

Look at this article. http://www.chapterthree.com/blog/matt_cheney/howto_best_practices_embedd...

<?php

/**
* Implementation of hook_views_default_views().
**/
function my_great_module_views_default_views() {
 
$files = file_scan_directory(drupal_get_path('module', 'my_great_module'). '/views', '.view');
  foreach (
$files as $filepath => $file) {
    require
$filepath;
    if (isset(
$view)) {
     
$views[$view->name] = $view;
    }
  }
  return
$views;
}
?>

You need to wrap the filter with /.../ in the file_scan_directory() function

<?php
/**
* Implementation of hook_views_default_views().
**/
function my_great_module_views_default_views() {
 
$files = file_scan_directory(drupal_get_path('module', 'my_great_module'). '/views', '.view');
  foreach (
$files as $filepath => $file) {
    require
$filepath;
    if (isset(
$view)) {
     
$views[$view->name] = $view;
    }
  }
  return
$views;
}
?>

See - https://drupal.org/node/954594