Drupal 7 Altering Views

Nearly every Drupal site I create ends up using the Views community module. As of this writing the current version of Views is 3.x. Sometimes I make an exception and don’t use Views (it’s a lot of code) in a given website. But, Views is an efficient way to create lists of items, or displays of collections of pieces of content on your website. You can do a lot with just Views and its administration module Views UI. But, sometimes you have to go beyond what Views can do out of the box and so need to take a step into the Views API to interact with a $view as it’s being created or displayed.

The Views module provides a set of hooks to interact with a View during its lifetime (creation, configuration, rendering, etc). The Views hook based API can be found in views.api.php file for Drupal 7/8.

For any website using Views I find the following api hooks useful, this is by far not all the api Views exposes for developers:

  • hook_views_data
    This hook allows your module to define additional data (tables) Views can interact with and their relationships to other Drupal data tables in your database.

  • hook_views_api
    This hook allows your module to be essentially registered as a user of the Views api.

  • hook_views_default_views
    This hooks allows you take an export of a View from the Views Admin area and copy and paste it into this hook definition for your module. Your $view(s) will then be installed onto the site when your module is Installed. This is a poor man’s Features alternative.

  • hook_views_[ pre_render, pre_view, query_alter ]
    These hooks allow you to interact with a particular $view (or views). Alter rows of the view data, change HTML or CSS, alter its database query.

Here are some example usage of these API calls:

/**
 * Implements hook_views_api().
 *
 * This function may go in your MYMODULE.module file.
 */
function MYMODULE_views_api() {
  return array("version" => "3.0");
}
/**
 * @file
 * MYMODULE.views_default.inc
 *
 * note this file and function live in a special file
 * in your module directory.
 */

/**
 * Implements hook_views_default_views().
 */
function MYMODULE_views_default_views() {
  $export = array();

  $view = new view;
  $view->name = 'my_slideshow';
  $view->description = 'frontpage slideshow for www.demo.com';
  $view->tag = 'mysite';
  $view->base_table = 'node';
  $view->human_name = 'frontpage slideshow magic!';
  $view->core = 7;
  $view->api_version = '3.0';
  $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

  // zillion of lines here ...
  $export['my_slideshow'] = $view;
  return $export;
}
/**
 * Implements hook_views_data().
 *
 * Tell Views about other data in the database, it
 * doesnt know about yet ...
 */
function MYMODULE_views_data() {
  $data['TABLENAME']['table']['base'] = array(
    'field' => 'id',
    'title' => t('Custome Data'),
    'help' => t('Custom data in a table used on the site.'),
    'weight' => -10,
  );

  $data['TABLENAME']['table']['group'] = t('Custom Content');

  $data['TABLENAME']['body'] = array(
    'title' => t('Content Body'), 
    'help' => t('The body of our custom content.'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['TABLENAME']['category'] = array(
    'title' => t('Category'), 
    'help' => t('The category of the content.'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['TABLENAME']['date'] = array(
    'title' => t('Timestamp field'), 
    'help' => t('The date/time of the entry.'), 
    'field' => array(
      'handler' => 'views_handler_field_date', 
      'click sortable' => TRUE,
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
  );

  return $data;
}

The last few functions such as pre_render, pre_view, pre_build, post_build largely take a $view as an argument. They simply allow you to affect a change at a different point in the View’s lifetime.

/** Implements hook_views_pre_view(). */
function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {

  if (($view->name == 'MYSITE_news' && $view->current_display == 'block_2') ||
    ($view->name == 'MYSITE_events' && $view->current_display == 'block_1')) {
    $default_fifteen_days = 15;
    $days = variable_get('MYSITE_stale_duration', $default_fifteen_days);
    // old field was "changed".
    $view->display[$display_id]->handler->options['filters']['timestamp']['value'] = "-$days day";
  }
}

Lastly, the hook_views_query_alter() is simply a helper method to the default hook_query_alter. Every view may have a TAG appended to the SQL query. You may use this View hook, or the default hook_query_alter to change the Query itself (such as tweaking the WHERE clause).

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>