Batch Revert Views

Druplicon Logo

Isn't it a pain when you have dozens of Views setup and they are all marked as "overridden" because you just pulled in an updated feature file from somewhere. Features doesn't always notice when the Views on your site aren't up to date.

The following snippet (which you should use with caution) will batch "delete" (or Revert, once the view is in code) all Views which are marked as Overridden. This took a few seconds to run on our development machine.

$views = views_get_all_views();
 
foreach ($views as $view) {
  if ($view->disabled) continue;
 
  if ($view->type == t('Overridden')) {
    $view->delete();
    views_object_cache_clear('view', $view->name);
  }
}

I ran this from the Devel PHP page (http://www.example.com/devel/php). It essentially does the same as the view module does when you individually revert views, but this does it without confirming on ALL VIEWS MARKED AS Overridden. I cannot stress enough - use at your own risk, and backup your database first!

Comment Icon

7 Comments

The most recent comment was on Tue, 26th Apr 2011 - 12:11

Next I needed to batch revert Panel Pages too.. Here is the snippet for that:

ctools_include('plugins');
ctools_get_plugins('page_manager', 'tasks');
module_load_include('inc', 'features', 'features.export');
$pages = features_get_default('page_manager_pages', 'FEATURES_MODULE_NAME');
array_walk($pages, 'page_manager_page_delete');

It also seems this is a known issue with Features.

EDIT: Looks like this one can take a long time. Just had to increase the set_time_limit to give this one long enough to finish.

"Features doesn't always notice when the Views on your site aren't up to date."

I've found that it's usually just a caching issue. The following has always worked for me:

drush cc all
drush fr all
drush cc all

Thanks! - this post has inspired me to create a drush command for this purpose. It's currently waiting review on the views issue queue, check it out!

http://drupal.org/node/1079178

Hi mate :-) nice to come across your site to drop you a line, Copenhagen seems like ages ago since we caught up :-). Just a thought, and it's a late thought at nearly 4am in the morning, would you still have the same issues if you changed the version number for the exported feature you are updating/overriding?

Hi, nice article. I'm looking for a way to disable a default view using php. I'm writing an update hook for a live site that uses a default view, but I no longer want it enabled. Have you come across this? I tried "$view->disable();" but that gives an error...

There is a line in the Exporting PHP like this:

$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

That determines if the view is disabled by default or not.

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.