From f9d83dee78e30d421c188ce8ff527d2c76e9321f Mon Sep 17 00:00:00 2001
From: Dmitry Demenchuk <dmitry@demenchuk.me>
Date: Fri, 16 Oct 2015 10:00:42 +0100
Subject: [PATCH] Fix VIEWS_IMPORT_DIR visibility

---
 views_import.drush.inc         | 51 ++++++++++++++++++++++++++++++++++++++++++
 views_import.module            | 25 ++++++++++++++++++++-
 views_import.views_default.inc |  3 +--
 3 files changed, 76 insertions(+), 3 deletions(-)
 create mode 100644 views_import.drush.inc

diff --git a/views_import.drush.inc b/views_import.drush.inc
new file mode 100644
index 0000000..9373791
--- /dev/null
+++ b/views_import.drush.inc
@@ -0,0 +1,51 @@
+<?php
+/**
+ * @file
+ * Drush commands for views_import module.
+ *
+ * Command to export includes files to store Views definitions on the disk.
+ */
+
+module_load_include('inc', 'views_import', 'views_import.drush');
+
+/**
+ * Callback for views_import_export_views command.
+ *
+ * @param string $view_name
+ *   String containing the name of the view to export.
+ */
+function drush_views_import_export_views($view_name = '') {
+  $views = views_get_all_views();
+  $all = drush_get_option('all', 0);
+  if ($view_name == '' and $all >= 1) {
+    foreach ($views as $vname => $view) {
+      _views_import_export_view($vname, $views);
+    }
+  }
+  elseif ($view_name != '') {
+    if (array_key_exists($view_name, $views)) {
+      _views_import_export_view($view_name, $views);
+    }
+    else {
+      return drush_set_error(dt('"@view_name" is not a valid view name', array('@view_name' => $view_name)));
+    }
+  }
+  else {
+    return drush_set_error(dt('No view name given, nothing to do :('));
+  }
+}
+
+/**
+ * Helper function to write individual views files.
+ *
+ * @param string $name
+ *   Name of the view to export.
+ * @param array $views
+ *   Array of Views available to Drupal, generally from views_get_all_views().
+ */
+function _views_import_export_view($name, $views) {
+  $file_name = $name . '.inc';
+  $export = "<?php \n";
+  $export .= views_export_view($views[$name]);
+  file_put_contents(VIEWS_IMPORT_DIR . '/' . $file_name, $export);
+}
diff --git a/views_import.module b/views_import.module
index c71b8c8..f7b288b 100644
--- a/views_import.module
+++ b/views_import.module
@@ -3,10 +3,33 @@
  * @file
  * Views Import.
  */
+ 
+define('VIEWS_IMPORT_DIR', 'sites/all/imports/views');
 
 /**
- * Implementation of hook_views_api.
+ * Implements hook_views_api().
  */
 function views_import_views_api() {
   return array("version" => "3.0");
 }
+
+/**
+ * Implements hook_drush_command().
+ */
+function views_import_drush_command() {
+  $items['views_import_export_views'] = array(
+    'description' => 'Export views to views_import folder.',
+    'aliases' => array('vie'),
+    'arguments' => array(
+      'view' => 'The name of a view to export',
+    ),
+    'options' => array(
+      'all' => 'Export all active views',
+    ),
+    'examples' => array(
+      'drush vie frontpage' => 'Export the frontpage view to views_import folder.',
+      'drush vie --all=1' => 'Export all views to views_import folder.',
+    ),
+  );
+  return $items;
+}
diff --git a/views_import.views_default.inc b/views_import.views_default.inc
index ba892d6..0c15a1b 100644
--- a/views_import.views_default.inc
+++ b/views_import.views_default.inc
@@ -28,8 +28,7 @@ function _views_import_fetch_view($uri) {
  * @return array
  */
 function views_import_views_default_views() {
-  $dir = 'sites/all/imports/views';
-  $files = file_scan_directory($dir, '/\.inc$/', array('recurse' => FALSE));
+  $files = file_scan_directory(VIEWS_IMPORT_DIR, '/\.inc$/', array('recurse' => FALSE));
 
   $views = array();
   foreach ($files as $file) {
-- 
2.3.8 (Apple Git-58)

