Altering Drupal Views Footer Content Dynamically

Last modified: 
Friday, November 6th, 2015

This is an example of dynamically altering a view's footer content hook_views_pre_view(). This won't work in hooks_views_pre_render(). I don't know why yet.

Note, I'm doing this from within a custom module and using a custom class to manage the transformation within MYMODULE_views_pre_view(). This is because this code is part of a larger project better managed under OOP. You can move all the transformation code into MYMODULE_views_pre_view() if you prefer.

<?php 
/**
 * @file MYMODULE.module
 */
function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {

  // Customize footer content.
  if ($view->name === 'VIEW_NAME' && $view->current_display === 'VIEW_DISPLAY') {
    $footer = 'MY CUSTOM FOOTER CONTENT';
    $Alter = new ViewsPreViewAlter($view);
    $Alter->footerAlterContent($footer);
  }
}

/**
 * A class for performing generic transformations in hook_views_pre_view().
 */
class ViewsPreViewAlter {

  public $view;

  /**
   * Import the $view object.
   */
  public function __construct(&$view) {
    $this->view = $view;
  }

  /**
   * Override footer content.
   */
  protected function footerAlterContent($content) {
    $options = $this->view->display_handler->get_option('footer');
    $options['area']['content'] = $content;
    $this->view->display_handler->set_option('footer', $options);
  }
}


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.