As a starting point for my new blog, I'll walk you step by step on how to export your views to code. Before we begin though, I bet your first question is:
Why?
Views are stored in the database and therefore can not be put into revision control. Like much of the Drupal system, the meat-and-potatoes of your website are stored in the database, and Views are no exception. If you should ever need to make a change to a view, you have nothing to roll back to, at least not without restoring a database backup, which is just silly.
OK, Now How?
The module we are caring about today is the 'Views exporter' which comes with views. How convenient! Open up you modules page and find the Views section, find the module and enable it, along with the 'Views UI' module. Now head over to views page and you should notice some new things. For one thing, each view now has an export option, which dumps a bunch code on the screen. Also, a page has been added under the 'Tools' section called 'Bulk Export', which we will talk about later.
We are going to focus on how to do this manually first, because it's better to learn how it's done than just using a tool, and the more you know about Views the better. What you want to do is create what I call a 'site' module. When developing a new site, you more times than none end up needing a module to do things like oddball form alters, module hooks, and in this case: export your views.
Module structure
Open up your /sites/{site folder}/modules directory and create a new folder. I'm going to name my mine 'chewbacca' to keep things lively. Create the following file structure:
/chewbacca/
/chewbacca.info
/chewbacca.module
/views/
Open up your text editor of choice, and start with opening chewbacca.info. Place in the following code:
chewbacca.info
; $Id$ name = Chewbacca description = "The wookie" package = Kashyyyk core = "6.x" version = "6.x-dev" ; Module dependencies dependencies[] = views
Now open chewbacca.module:
chewbacca.module
/**
* Implementation of hook_views_api()
*/
function chewbacca_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'chewbacca') .'/views/*.inc',
);
}
/**
* Implementation of hook_default_views()
*/
function chewbacca_views_default_views() {
$path = './'. drupal_get_path('module', 'chewbacca') .'/views/*.inc';
foreach (glob($path) as $views_filename) {
require_once ($views_filename);
}
return $views;
}
Awesome job! Go to the views page find your view, then copy the name of it. Jump into that /views/ folder and create a new called 'view_name'.inc. and place the following code in it:
'view_name'.inc
<?php //View Code //End View Code $views[$view->name] = $view;
Keep that open and jump back to the Views admin. Now click the 'Export' link on the view you want to export. What you want to do is paste the code output by that page into your newly created .inc file in between the comments.
Admin Steps
Now that your module is complete, go to the modules page and enable it. Then go to the Views admin and clear the Views cache by clicking the 'Tools' tab then the button at the top. Finally, go to the listing page and delete that view. You will still see it, almost the same, except for a 'Disable' option on the view now. That means it worked and your view is now in code.

After Thoughts
Views provides a UI to do all of this located under the tools tab and the 'Bulk Export' page, but it groups your selected views into one file, whereas doing it the way noted above allows you to split up your views into separate, files. Where this really shines is when using some sort of version control system. As you later modify your view and export the new code, then commit it, you are able to track each and every change to you views. I could list off the advantages all night, but you wouldn't be reading this if you didn't already have your reasons.
I hope you enjoyed my first useful blog post, and stay tuned for more to come.
