Home ›
Exporting views to files to be managed by version control: Drupal 7Exporting views to files to be managed by version control: Drupal 7
Submitted by Benjamin Melançon on April 7, 2014 - 6:15am
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