Setting views header and footer on the fly

May 8, 2011 — sivaji

Drupal Views is an excellent query builder module, helps to display almost any content in your Drupal site in different formats with custom header and footer, in a searchable format. It is occasionally required to set header and footer dynamically. Views module offers APIs hook_views_pre_render() to do this.

I had to implement this recently for one of my projects, for a views that takes taxonomy term from URL and description of term to be set in view's header on the fly.

/**
 * Implementation of hook_views_pre_render().
 */
function mymodule_views_pre_render($view) {  
  if ($view->name == 'view_foo_bar' && isset($view->args[0])) {
    $term = $view->args[0];    
    $term = array_pop(taxonomy_get_term_by_name($term));
    if (isset($term->tid) && !empty($term->description)) {
      $header = $term->description;
      $handler = &$view->display['default']->handler;
      $handler->set_option('header', $header);
      $handler->set_option('header_format', 2);
    }    
  }
}

Above snippet is the implementation of hook_views_pre_render() I used for my requirement. If you need any additional options refer function views1_import() in /views/includes/convert.inc

G2 Block C, Suraj Nivas, 16/17 Station Border Road, Chrompet, Chennai - 600 044, Tamilnadu, India. Phone : +91 44 222 344 78 contact Map