Pass a View Result to a Module

Last modified: 
Tuesday, December 15th, 2015

This is an example of using a singleton and hook_views_post_execute() to capture the result from a View and pass it into a module's scope. Singletons are often considered an anti-pattern, but it seems to make sense to use one here as a means to work around Drupal 7's non-OO, hook paradigm.

class MySingleton {

  private static $instance;
  private $result = NULL;

  public static function singleton() {
    if (!isset(self::$instance)) {
      self::$instance = new MySingleton();
    return self::$instance;

  public function setResult(&$result) {
    if ($result) {
      $this->result = $result;

  public function getResult() {
    return $this->result;

function MYMODULE_views_post_execute(&$view) {
  $my = MySingleton::singleton();
function _MYMODULE_get_results() {
  $map = MySingleton::singleton();
  $results = $my->getResult();
  /* Data transforms and such go on here ... */
  return $results;

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.