Export all views on a site to csv, json, spreadsheet etc The Question.

How do I export a spreadsheet documenting all the views from a drupal site?

More info:

Say you want a summary of all the views on a site, perhaps you are going to rebuild them - maybe there is some iffy php_views in there. Maybe you are simply documenting a messy site that you've inherited. 

While there is support for exporting individual views to various formats it is not as easy to export a comprehensive list of all your views to a spreadsheet.
The Answer.

views_get_all_views()


The Script (save this as views_summarize.php or similar):

&lt;?php
$views = [
  [
    'view machine name',
    'view name',
    'view desc',
    'base table',
    'display machine name',
    'display title',
    'display plugin',
    'php field the first',
  ]
];
foreach (views_get_all_views() as $view) {
  foreach ($view-&gt;display as $display =&gt; $data) {
    $views[] = [
      // 'view machine name'
      $view-&gt;name,
      // 'view name'
      $view-&gt;human_name,
      // 'view desc'
      $view-&gt;description,
      // 'base table'
      $view-&gt;base_table,
      // 'display machine name'
      $display,
      // 'display title'
      $data-&gt;display_title,
      // 'display plugin'
      $data-&gt;display_plugin,
      // 'php value in default field'
      $view-&gt;display['default']-&gt;display_options['fields']['php']['php_value'],
    ];
  }
}
$fp = fopen('views-summary.csv', 'w');
foreach ($views as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);


Notes:

The returned $view object has properties nested within arrays and objects, make sure you shift gears accordingly in your query:

e.g.

$view-&gt;display['default']-&gt;display_options['fields']['php']['php_value'],


You can run this script with:

drush php-script views_summarize.php


Inspect the $view objects returned by views_get_all_views()

drush ev "print_r(views_get_all_views());"


Alternative uses

Site documentation is a tricky task, this approach is useful when documenting any pre-existing site configuration or content like rules, contexts etc...

A few hours later

Refined version of above script - outputs marker to indicate presence and location of php in views - yikes!

https://gist.github.com/gl2748/313b19bce1a99e42d94f