Author image
Senior Mind

Organising your views in Views 1.x

If you're still stuck in the Views dark ages, and 1.6 is as good as it gets for you here's a quick tip to make things a little easier:

Exporting your views

You've just spent a good hour creating and tweaking that view until it shows exactly the right thing, and now you want to save it for posterity, portability and all that jazz.

Simple. Click on the 'Export' tab within Views and you'll get a lovely version of your view in code, which you can pop in an implementation of hook_default_views() and you're done, almost.

That's all well and good if you've only got a two or three views to worry about, but after a while you're going to want to change something in one of them, and you have to find it in the code (which is now several hundred lines long) and copy and paste the new export in, making sure you don't touch the other views in code, well there is a better way!

Use the following implementation of hook_views_default_views()

/**
* Implementation of hook_views_default_views()
*/
function example_views_default_views() {

  $views = array();

  $files = file_scan_directory(drupal_get_path('module', 'example') . '/views', '\.inc$');

  foreach ($files as $file) {
    include($file->filename);
  }

  return $views;
}

Then just create a 'views' subdirectory in your module's directory and when you export your views to code, save them to this subdirectory. Just make sure the file-name ends in '.inc', and it's probably sensible to call the file the name of the view so it's easy to find. Don't forget that these files need to valid PHP, so must start with <?php.

Much better eh?

Comments

Config to Code module provides some developer tools to automate this process. Currently it has no UI, so it needs some patience and developer eyes to use it, but that's never stopped the adventurous drupal configurator!

Bevan/

For Views 2:

  1. Export the views using the views module, or drush
  2. Create a subfolder in your module's folder called "views", and a subfolder in that called "default".
  3. Paste each exported view into a file called "VIEWNAME.inc" (always use the viewname to avoid confusion and ensure uniqueness). Make sure the first line of each file is a PHP open tag!
  4. Add the following to your .module file:
    /**
    * Implementation of hook_views_api().
    */
    function MODULENAME_views_api() {
      return array(
        'api' => 2,
      );
    }
  5. Add this code to either your .module file or a new MODULENAME.views_default.inc file, the latter is better:
    /**
    * Implementation of hook_views_default_views().
    */
    function MODULENAME_views_default_views() {
      $views = array();

      $files = file_scan_directory(drupal_get_path('module', 'MODULENAME') . '/views/default', '\.inc$');

      foreach ($files as $file) {
        include($file->filename);
        $views[$view->name] = $view;
      }

      return $views;
    }

Hiyah - just spotted a slight inconsistency that had me stumped for a while - you've got hook_views_default_views() and hook_default_views() in there. Of course I read the wrong one first and copied that into my module ;)

Comments on this article are now closed, if you want to give us feeback you can use our contact form instead.