Using Drupal’s hook_views_default_views_alter

I came across an issue recently with a Drupal installation that I am working on that required me to use hook_views_default_views_alter, and found that the help out there was minimal.  I finally got what I was after working, but with much debugging using the Devel module and dpm.  I thought that I would put down what I found out to help others in a similar position. Hook views_default_views_alter allows you to modify programatically any view that is defined in another module in your drupal codebase.  The signature is as follows

function hook_views_default_views_alter(&$views) {}

The first thing you will need to get out of this function is access to the view you want to modify.  The thing that is negated to be demoed in the documentation is that the $views variable is an associative array of the views defined in your site – both in code and through the views interface.

So, the first thing that you need to do is find out the name of the view that you want to modify.  This is an object, which contains all the information about the view.  In my case, I just needed to modify a display, which is a property of the object – another array, with one entry per display.  This is the first piece of the puzzle, and allows you to get the display handler.

$handler =& $views[VIEW_NAME]->display[DISPLAY_NAME]->handler;

Obviously you will want to change VIEW_NAME and DISPLAY_NAME to the names you need to modify.

Now that we have the handler, the easy part begins.  You can modify these attributes of the display.  Need to modify a filter on the view, thats easy, just alter the multi-dimensional array

$handler->display->display_options['filters'][NAME_OF_FILTER]['value'] = NEW_VALUE;

You have complete control over the view in this function.  You can add fields, modify filters, or remove fields.  Probably the best way to go about modifying the view is to get a hold of the handler object, and then use dpm to inspect it.

Here is an example taken from the api docs, on adding a field to a display (my-display) inside view (which I am going to call ghosty) from my ghosty_demo module.

function ghosty_demo_views_default_views_alter(&$views) {
  if(!empty($views['ghosty']->display['my-display']->handler)) {
    $handler =& $views['ghosty']->display['my-display']->handler;
    $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;
  }
}

 

One thought on “Using Drupal’s hook_views_default_views_alter”

  1. Hello Simon,

    I read your post and its very helpful there is a very little help that is available. I am working on something similar, the only thing that is different is I want to an handler that I have written.

    This is the first time for me to work on hooks_views_default_views_alter, but I have worked with hooks_views_data and hooks_views_data_alter.

    he 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.

    Thanks and let me know if the question needs more clarification.

    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,
    ),
    );
    }

Leave a Reply