fbpx Organising your views in Views 1.x | ComputerMinds Skip to main content

Organising your views in Views 1.x

17th Jan 2009

Mike Dixon

Senior Mind

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) {

  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?