Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Guide to exporting Views in Drupal 6/7 into your module so you can keep them safe in version control. You *ARE* using version control right?

Exporting Views

This guide assumes you have a module ready to save the views but it does not have any Views integration or folder structure. Skip ahead if you've already created the folders and Views Hooks.

  • Add the views folder to your module.
  • Create a file called MODULENAME.views.inc inside the views folder.
  • Add a default_views folder inside the views folder.

Your module directory structure should now resemble something like this:

    MODULE
    |- MODULENAME.info
    |- MODULENAME.module
    |--> views
    |--- MODULENAME.views.inc
    |---> default_views

Before we can start dumping your views into the default_view folder we have to make sure your module has the appropriate Views hooks. Add the following snippet of code to your main module file.

<?php
/**
 * Implements hook_views_api().
 */
function MODULENAME_views_api() {
  return array(
    // Indicates the Views API Version:
    // Views 2 = 2
    // Views 3 = 3 (Latest)
    'api' => 2,
    // This is the path to your views files.
    'path' => drupal_get_path('module', 'MODULENAME') . '/views',
  );
}
?>

This will make views aware that your module has views information to provide and is a required hook, if nothing appears or functions then check this is properly setup and returning the correct structure.

The next step is to add the hook_views_default_views() to your views.inc file to ensure our views are added as default views. To avoid having to update this hook whenever a view is updated, the following code is designed to scan the default_views directory for specifically named files and load them as default views.

<?php
/**
 * Implements hook_views_default_views().
 */
function MODULENAME_views_default_views() {
  $path = '/views/default_views/';
  $extension = '/\.default/';
  $files = file_scan_directory(drupal_get_path('module', 'MODULENAME') . $path, $extension);

  $views = array();

  foreach ($files as $absolute => $file) {
    $func = FALSE;
    $view = NULL;

    require_once $absolute;
    $func = $file->name;

    if (function_exists($func)) {
      $view = $func();
      $views[$view->name] = $view;
    }
  }
  return $views;
}
?>

Now any error free exported View PHP file we place in the MODULENAME/views/default_views folder will be read in as a default view, provided it has the extension of .default.

Finally we are ready to start exporting our views into the module!

Locate the view you wish to export, this can be done either from within the View itself (provided you have saved the view!) or from the list of all views.

  • From the list of all views: ** Find the view you want to export ** Click the arrow next to edit in the Operations column. ** A menu should drop down with a few options, click on export.
  • From the view itself: ** In the top right of the View display, on the same line as the list of displays, click the down arrow. ** A menu should be shown that contains a few options, click on export view.

Both links should take you to a page that contains a text area filled with code, this is your view in its exportable form.

<?php
$view = new view();
$view->name = 'VIEW_NAME';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'BASE_TABLE';
$view->human_name = 'VIEW LABEL';
$view->core = 7;
$view->api_version = '3.0';
...
<snip>
?>

Copy the entire contents of this text area into a function named function VIEW_NAME() that returns the $view. Place this into its own file that is named thus VIEW_NAME.default, and place it in the default_views folder. Ensure that you have the PHP opening tag at the start of the file and that your function returns the view object.

Do so for every view that you wish to export. So that each view is contained within its own file in the default_views folder, returned from a function that is named using the machine name of the view.

To check that this has been done correctly, flush the Drupal cache and navigate to the list of Views.

  • Locate an exported view.
  • Make a change to the view and click save.
  • Return to the list of views and find that view.
  • Click the down arrow in the Operations column.
  • If there is a revert option now present then that view has been successfully exported and reloaded as a default view.
  • Click revert to undo your change and restore the default state of the view.

#Things To Remember Every time this view is updated with a change that needs to be kept (i.e. you're not just testing), then it will need to be exported again and the contents of its file will need to be updated with the new version. It's best to just completely overwrite the code for the view with the new code to avoid missing any changes.

Ensure that you flush caches after you've created a default view to ensure it's acknowledge by Views.

$extension = '/\.default/'; does not work (D6/PHP5.5.9)
Use $extension = '\.default$'; instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment