Import Views programmatically (in code)

Public

Inside test.module include .inc file

</> CopyGet raw version
php
  1. /**
  2.  * Include views
  3.  */
  4. module_load_include('inc', 'test', 'test.views');

test.views.inc

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_views_api().
  3.  */
  4. function test_views_api() {
  5. return array(
  6. 'api' => '3.0',
  7. );
  8. }
  9.  
  10. /**
  11.  * Implements hook_views_default_views().
  12.  */
  13. function test_views_default_views() {
  14. $views = array();
  15. $path = drupal_get_path('module', 'test') . '/views/';
  16.  
  17. $files = array(
  18. 'test.name_of_the_view.inc',
  19. );
  20.  
  21. foreach($files as $file) {
  22. require_once $path . $file;
  23. $views[$view->name] = $view;
  24. unset($view);
  25. }
  26.  
  27. return $views;
  28. }

views/test.name_of_the_view.inc (paste here your exported views code from "/admin/structure/views/view/name_of_the_view/export")

</> CopyGet raw version
php
  1. /* Example exported code can look like this */
  2.  
  3. $view = new view();
  4. $view->name = 'name_of_the_view';
  5. $view->description = '';
  6. $view->tag = 'default';
  7. $view->base_table = 'node';
  8. $view->human_name = 'Name of the view';
  9. $view->core = 7;
  10. $view->api_version = '3.0';
  11. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  12. ...