User login

Exporting views to files to be managed by version control: Drupal 7

The trick to somewhat Drupal 8-esque configuration management in Drupal 7, when it comes to Views, is to make your view defined in the user interface to be defined by a module instead. This is done with hook_views_default_views().

From Michelle Lauer's Views chapter in the Definitive Guide to Drupal 7:

<?php
/**
 * Implements hook_views_api().
 */
function dgd7glue_views_api() {
  return array(
   
'api' => '3.0',
  );
}

/**
 * Implements hook_views_default_views().
 */
function dgd7glue_views_default_views() {
 
$path = './' . drupal_get_path('module', 'dgd7glue') . '/views/*.inc';
 
$views = array();
  foreach (
glob($path) as $views_filename) {
    require_once(
$views_filename);
  }
  return
$views;
}
?>

Now you can paste the export of each view you want to save (go to the view's edit dropdown and select export) into an include file in a views folder in your module. Each file needs to start with the PHP opening tag and end with adding the view to the $views array.

<?php
//put export code here
$views[$view->name] = $view;
?>

(Never include the closing ?> in production code.)

Searched words: 
multiple views module export Views 2.x to code save load default views from a directory of files

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.