Skip to main content

Organising your views in Views 1.x

An article from ComputerMinds - Building with Drupal in the UK since 2005
17th Jan 2009

Mike Dixon

Senior Mind
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

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?

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.