20 May 2013

Creating views view in your module is really simple.

Source code viewer
  1. // 1. Create your view the regular way.
  2.  
  3. // 2. Define your views api version and path for files that include views hooks. This goes to .module file.
  4. /**
  5. * Implements hook_views_api().
  6. */
  7. function HOOK_views_api() {
  8. return array(
  9. 'api' => 3.0,
  10. 'path' => drupal_get_path('module', 'HOOK') . '/views',
  11. );
  12. }
  13.  
  14. // 3. Views export creates code that you have to paste in this function. This goes to MODULENAME.views_default.inc and it will be auto-loaded.
  15. /**
  16.  * Implements hook_views_default_views().
  17.  */
  18. function HOOK_views_default_views() {
  19.  
  20. // Paste code form views export.
  21.  
  22. $views[$view->name] = $view;
  23. return $views;
  24. }
  25.  
  26. // 4. Delete your view and clear all caches, view from your code should appear. When you change the view don't forget to add the changes to code.
Programming Language: PHP