Skip to main content
Steven Merrill

Version Your Views: D5 Reminder

This post was originally posted on the Phase2 Blog.

In a previous article I extolled the virtues of keeping your Views in code, which lets you deploy or change them as easily as uploading or updating a module on your production site. In the article, I wrote about using Drupal 6 and Views2 to do so.

Drupal 5 / Views 1 Execution #

Views 1 for Drupal 5 also has this mechanism in place, so if you're working on a Drupal 5 site, it's still worth using. It’s how the calendar and date modules provide a default calendar view, among other things. The method for putting default views in your modules is pretty similar, and it’s still a great technique to practice. In Views 2 for Drupal 6, you had to implement two hooks to have a module provide default views in code: hook_views_api() and hook_views_default_views().

Views 1 doesn’t have the hook_views_api(), nor will Views 1 automatically look for a MODULENAME.views.inc or a MODULENAME.views_default.inc file, so you can just put an implementation of hook_views_default_views() in your main module file. If we were making the fictitious treehouse_utils module, the code would look exactly the same as in the previous example:

<?php

function treehouse_utils_views_default_views() {

$views = array();

// Start copy and paste of Export tab output.

// End copy and paste of Export tab output.

// Add view to list of views to provide.
$views[$view->name] = $view;

// ...Repeat all of the above for each view the module should provide.

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

}

Activating Your Views #

Unlike Views2, default views are updated when a module is first enabled, so if you are adding default views to an existing module, you will have to disable and re-enable the module to get them to show up, or clear out the Views cache, which you can do by going to http://yoursite.com/admin/build/views/tools and clicking the Clear Views Cache button.

The Clear Views Cache approach is a far cleaner one than disabling and reenabling modules. I recently put some default views for Drupal 5 in a custom module which was the backbone of our site, and disabling and re-enabling it was no easy task.