From 11a3f6f51b62efa3fb9dcf16167603fb8289756d Mon Sep 17 00:00:00 2001
From: Dmytro Danylevskyi <dmytro@danylevskyi.com>
Date: Tue, 15 Jan 2013 08:23:52 +0200
Subject: [PATCH] Issue #1758940: Fixed views relationship-handler.

---
 views/relation_handler_relationship.inc |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/views/relation_handler_relationship.inc b/views/relation_handler_relationship.inc
index e26650a..4db27cb 100644
--- a/views/relation_handler_relationship.inc
+++ b/views/relation_handler_relationship.inc
@@ -78,7 +78,7 @@ class relation_handler_relationship extends views_handler_relationship {
         ),
       ),
     );
-    if (isset($this->definition['entity_type_left'])) {
+    if (isset($this->definition['entity_type_left']) && !($this->view->base_table == 'relation') && !($this->definition['relationship field'] == 'rid')) {
       // The left table is an entity, not a relation.
       $join->definition['field'] = $entity_id_field_name;
       $this->ensure_no_duplicate_entities($join->definition['extra'], $this->options['entity_deduplication_left'], $this->definition['relation_type'], $this->definition['entity_type_left'], $this->table_alias, $this->real_field);
@@ -87,6 +87,13 @@ class relation_handler_relationship extends views_handler_relationship {
         'value' => $this->definition['entity_type_left'],
       );
     }
+    elseif($this->definition['base'] == 'relation') {
+      $join->definition['field'] = 'endpoints_entity_id';
+      $join->definition['extra'][] = array(
+        'field' => 'endpoints_entity_type',
+        'value' => $this->definition['entity_type_right'],
+      );
+    }
     else {
       // The left table is relation.
       $join->definition['field'] = 'entity_id';
@@ -141,7 +148,7 @@ class relation_handler_relationship extends views_handler_relationship {
       'field'      => $this->definition['base field'],
       'type'       => $join_type,
     );
-    if (isset($this->definition['entity_type_right'])) {
+    if (isset($this->definition['entity_type_right']) && !($this->definition['base'] == 'relation')) {
       // We are finishing on an entity table.
       $join->definition['left_field'] = $entity_id_field_name;
       $this->ensure_no_duplicate_entities($join->definition['extra'], $this->options['entity_deduplication_right'], $this->definition['relation_type'], $this->definition['entity_type_right'], $r, $entity_id_field_name);
@@ -151,6 +158,9 @@ class relation_handler_relationship extends views_handler_relationship {
         'value' => $this->definition['entity_type_right'],
       );
     }
+    elseif (isset($this->definition['entity_type_left']) && !($this->definition['base'] == 'relation')) {
+      $join->definition['left_field'] = $entity_id_field_name;
+    }
     else {
       // We are finishing on relation.
       $join->definition['left_field'] = 'entity_id';
-- 
1.7.9.5

