7 views.api.php hook_views_default_views_alter(&$views)
6 docs.php hook_views_default_views_alter(&$views)

Alter default views defined by other modules.

This hook is called right before all default views are cached to the database. It takes a keyed array of views by reference.

Example usage to add a field to a view:

  $handler =& $view->display['DISPLAY_ID']->handler;
  // Add the user name field to the view.
  $handler->display->display_options['fields']['name']['id'] = 'name';
  $handler->display->display_options['fields']['name']['table'] = 'users';
  $handler->display->display_options['fields']['name']['field'] = 'name';
  $handler->display->display_options['fields']['name']['label'] = 'Author';
  $handler->display->display_options['fields']['name']['link_to_user'] = 1;

Related topics

File

./views.api.php, line 849
Describe hooks provided by the Views module.

Code

function hook_views_default_views_alter(&$views) {
  if (isset($views ['taxonomy_term'])) {
    $views ['taxonomy_term']->display['default']->display_options['title'] = 'Categories';
  }
}

Comments

This hook DOES NOT need to be in MODULENAME.views_default.inc as hook_views_default_views().

Another example:

<?php
function mymodule_views_default_views_alter(&$views) {
 
$handler =& $views['myview']->display['default']->handler;
 
// now modify view
}
?>

After Clear the cache .Revert the views :P

Yes, reverting is a necessary step. However is there a way to do it in code? I really do not want to revert manually.

You can put this code in a hook_update when you need to revert all views or build your own drush command :

/**
* Force revert views.
*/
function HOOK_update_7001() {
  module_load_include('module', 'views');
  $views = views_get_all_views();
  views_revert_allviews($views);

  return t('All views has been reverted.');
}

If you know which views you have to revert, you can call
drush views-revert <view-name-1> <...> <view-name-n>

views_revert_allviews is in the views.drush.inc file, so this will need to be included too:

<?php
/**
 * Force revert views.
 */
function MODULE_update_7001() {
  if (
module_load_include('module', 'views') && module_load_include('inc', 'views', 'drush/views.drush')) {
   
views_revert_allviews(views_get_all_views());
    return
t('All views has been reverted.');
  }
  throw new
DrupalUpdateException('Could not include files');
}
?>

Well seems that the views.drush.inc file uses the dt() function and it fails on some of my systems so I offer this rewritten script as an alternative to the above :

<?php
/**
 * Reverts Views.
 */
function MODULE_update_7001() {
 
ctools_include('object-cache');
 
$views = views_get_all_views();
  foreach (
$views as $view) {
    if (
$view->type == t('Overridden')) {
     
// Revert the view.
     
$view->delete();
     
// Clear its cache.
     
ctools_object_cache_clear('view', $view->name);
    }
  }
  return
t('All views have been reverted.');
}
?>

This is an interpretation of the original drush function omitting dt().

I have previously implemented the hooks_views_data and hooks_views_data_alter for the views that I created. I need to alter default views, but since the code under hooks_views_default_view is machine generated, I am not sure where to describe the new column and the new handler for the new column.
The following is my hooks_views_data_alter code, it would be great if someone could use this as reference and tell how this can be implemented for hook_views_default_views_alter

function bucket_views_data_alter(&$data) {
//debug($data['example_custom']);
$data['example_custom']['actions'] = array(
'title' => t('Bucket'),
'help' => t('Clickable links to actions a user may perform on a row.'),
'field' => array(
'handler' => 'bucket_views_handler_field_actions',
'group' => 'example_custom',
'click sortable' => FALSE,
),
);
}

Note: I am not using drupal's handler, I have written a new handler that I would like to use with hook_views_default_views_alter.

Hope the questions is clear.