Last updated 7 June 2014. Created on 20 June 2008.
Edited by acabouet, bekasu, Michelle. Log in to edit this page.

Note: This is for views 1.

Panels 2 comes with a nifty exporter that will dump all your panels related stuff (pages, views panes, mini panels, etc) into one export file set up to work as a module. It's a really useful feature for moving them from site to site or simply keeping them in code for outside the database backups.

For Views 2 see here:

Views 1, being a much older module, doesn't have quite as nice of an exporter. It does allow you to export your views one at a time but using that export in a module takes a bit more work. This article will show you one way of doing it. There are other ways, such as putting all the views in one file, but I find this method easier to maintain.

Step 1: Create a module.
If you already have a site specific module, you can just use that. If not, you'll need to create a module. It's not hard. All you need is a .info and a .module. You can find more information in this tutorial.

Add this function to the module:

function MODULENAME_views_default_views() {
  $views = array();
  $path = drupal_get_path('module', 'MODULENAME') . '/views';
  $files = drupal_system_listing('.inc$', $path, 'name', 0);
  foreach($files as $file) {
    include_once $file->filename;
  return $views;

Be sure to replace MODULENAME with the name of your module, in lower case.

Step 2: Create a views directory.
In your module's directory, create a subdirectory called "views". This is where we will be storing the views include files.

Step 3: Export your views.
On the views administration page, every non-default view has an option to export. For each one, export the view and copy the results into its own text file. At the top, add the line <?php so Drupal recognizes it as code. Save each text file as into the views directory from step 2. What you call them doesn't matter, but I recommend using the name of the view to keep them straight.

Step 4: Let views know what you did.
Enable your module if you haven't already done so. On the views admin page is a tab called "tools". Go there and you'll see an option to clear the views cache. Do so. Now go back to the listing page. All the views you exported will be in the "default" views section and labeled overridden. If you don't need to make any more changes to the views, you can safely delete the database version (labeled "existing views") and just run off the ones in the .inc files. The usual disclaimers about backing up your database first apply here. :)

That's it. You now have all your views running off of code. You can take this module to another site, turn it on, and there are your views. (Assuming, of course, that any modules the views rely on are enabled at the new site.)

If you need to change a view, just override it, change it, re-export it, and overwrite the corresponding .inc file.

Looking for support? Visit the forums, or join #drupal-support in IRC.


jdidelet’s picture

For Views 2, Please add:

foreach($files as $file) {
  include_once $file->filename;
  $views[$view->name] = $view; // Needs this line to work
return $views;

Read on in the comments but I added here for remind.

alamp’s picture

As jdidelet mentioned, the same thing must be applied

Especially if you export view via View UI, there is no

  $views[$view->name] = $view; 

So hook_views_default_views should be like this

 * Implements hook_views_default_views().
function yourmodule_views_default_views() {
  $view = new view();
// Needs this line to work
  $views[$view->name] = $view; 
  return $views;