From 95cb10fe0eb19b321334aa3748618ae14a2d899c Mon Sep 17 00:00:00 2001
From: Charles O'Sullivan <chsoney@sfsu.edu>
Date: Fri, 18 Mar 2011 15:58:54 -0700
Subject: [PATCH] Issue #1056266: Allow for revisions which do not have an entity type.

---
 modules/field/views_handler_field_field.inc |   46 +++++++++++++++++++++-----
 1 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/modules/field/views_handler_field_field.inc b/modules/field/views_handler_field_field.inc
index 29c008a..b5a0e2b 100644
--- a/modules/field/views_handler_field_field.inc
+++ b/modules/field/views_handler_field_field.inc
@@ -40,6 +40,9 @@ class views_handler_field_field extends views_handler_field {
     * Store the field informations.
   */
   public $field_info = array();
+  
+  private $revision;
+  private $base_table;
 
   function init(&$view, &$options) {
     parent::init($view, $options);
@@ -77,7 +80,7 @@ class views_handler_field_field extends views_handler_field {
    * present in the query itself (such as "sphinx").
    */
   function query($use_groupby = FALSE) {
-    $base_table_alias = $base_table = $this->view->base_table;
+    $base_table_alias = $this->base_table = $base_table = $this->view->base_table;
     $base_field = $this->view->base_field;
 
     // If the current field is under a relationship you can't be sure that the
@@ -86,7 +89,7 @@ class views_handler_field_field extends views_handler_field {
     if (!empty($this->relationship)) {
       foreach ($this->view->relationship as $relationship) {
         if ($relationship->alias == $this->relationship) {
-          $base_table = $relationship->definition['base'];
+          $this->base_table = $base_table = $relationship->definition['base'];
           $base_table_alias = $relationship->alias;
 
           $table_data = views_fetch_data($base_table);
@@ -111,7 +114,18 @@ class views_handler_field_field extends views_handler_field {
     // Get the entity type according to the base table of the field.
     // Then add it to the query as a formula. That way we can avoid joining
     // the field table if all we need is entity_id and entity_type.
-    $this->entity_type = $entity_type = $this->definition['entity_tables'][$base_table];
+    if (!empty($this->definition['entity_tables'][$base_table])) {
+      $this->entity_type = $entity_type = $this->definition['entity_tables'][$base_table];
+    } 
+    else {
+      foreach ($this->definition['entity_tables'] as $table => $entity) {
+        $entity_info = entity_get_info($entity);
+        if (!empty($entity_info['revision table']) && $entity_info['revision table'] === $base_table) {
+          $this->entity_type = $entity_type = $entity;
+          $this->revision = true;
+        }
+      }
+    }
     // The alias needs to be unique, so we use both the field table and the entity type.
     $entity_type_alias = $this->definition['table'] . '_' . $entity_type . '_entity_type';
     $this->aliases['entity_type'] = $this->query->add_field(NULL, "'$entity_type'", $entity_type_alias);
@@ -485,6 +499,7 @@ class views_handler_field_field extends views_handler_field {
    */
   function pre_render(&$values) {
     if (!empty($values)) {
+          
       // Divide the entity ids by entity type, so they can be loaded in bulk.
       $entities_by_type = array();
       foreach ($values as $key => $object) {
@@ -494,16 +509,29 @@ class views_handler_field_field extends views_handler_field {
           $entities_by_type[$entity_type][$key] = $entity_id;
         }
       }
-
+      
       // Load the entities.
       foreach ($entities_by_type as $entity_type => $entity_ids) {
-        $entities = entity_load($entity_type, $entity_ids);
-
-        foreach ($entity_ids as $key => $entity_id) {
-          if (isset($entities[$entity_id])) {
+        if (!$this->revision) {
+          $entities = entity_load($entity_type, $entity_ids);
+  
+          foreach ($entity_ids as $key => $entity_id) {
+            if (isset($entities[$entity_id])) {
+              $values[$key]->_field_data[$this->field_alias] = array(
+                'entity_type' => $entity_type,
+                'entity' => $entities[$entity_id],
+              );
+            }
+          }
+        }
+        else {
+          $entity_info = entity_get_info($entity_type);
+          $revision_key = $entity_info['entity keys']['revision'];
+          foreach ($entity_ids as $key => $revision_id) {
+            $entity = entity_load($entity_type, false, array($revision_key => $revision_id));
             $values[$key]->_field_data[$this->field_alias] = array(
               'entity_type' => $entity_type,
-              'entity' => $entities[$entity_id],
+              'entity' => array_shift($entity),
             );
           }
         }
-- 
1.5.4.3

