How do I create a view using code in Drupal 7?

We can create a view from the admin panel. But I want to create a view using php code. Can anyone show me the way?

+3


source to share


1 answer


There is some code floating around that won't work for me. But it was done. Add this php to your .module file. Then create a views folder and then put all your views there with the .inc extension. Each view file will be simple <?php

, followed by an exact export of the view ...



/**
 * Implements hook_views_api().
 */

 function MODULENAME_views_api() {
    return array ('api' => 3.0);
 }

 function MODULENAME_views_default_views() {
  // Check for all view file in views directory
  $files = file_scan_directory(drupal_get_path('module', 'MODULENAME') . '/views', '/.*\.inc$/');

  // Add view to list of views
  foreach ($files as $filepath => $file) {
    require $filepath;
    if (isset($view)) {
      $views[$view->name] = $view;
    }
  }

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

      

+6


source







All Articles