From 9677ed4a6244d75a615fc2d0b9609d9d531e9f85 Mon Sep 17 00:00:00 2001
From: Jorrit Schippers <jorrit@161217.no-reply.drupal.org>
Date: Thu, 6 Dec 2012 17:55:56 +0100
Subject: [PATCH] Issue #1714110 by Jorrit: Fixed cross references in
 directional relations.

---
 relation_select.module |   29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/relation_select.module b/relation_select.module
index 9e6bb93..8d60475 100644
--- a/relation_select.module
+++ b/relation_select.module
@@ -456,8 +456,6 @@ function relation_select_field_load($entity_type, $entities, $field, $instances,
     $instance = $instances[$entity_id];
     $items[$entity_id] = array_values(relation_select_entity_get_relations($entity_type, $entity_id, $instance['settings']['relation_type']));
   }
-
-  return $items;
 }
 
 /**
@@ -465,12 +463,19 @@ function relation_select_field_load($entity_type, $entities, $field, $instances,
  */
 function relation_select_field_prepare_view($entity_type, $entities, &$field, &$instances, $langcode, &$items, $displays) {
   foreach ($entities as $entity_id => $entity) {
-    $relation_type = $instances[$entity_id]['settings']['relation_type'];
-    $query = relation_query($entity_type, $entity_id);
-    $query->propertyCondition('relation_type', $relation_type);
-    $relation_ids = array_keys($query->execute());
+    if (empty($items[$entity_id])) {
+      continue;
+    }
+
+    // $items contains the information saved by relation_select_field_load().
+    // This information is cached, so don't query for relations again.
+    $relation_ids = array();
+    foreach ($items[$entity_id] as $item) {
+      $relation_ids[] = intval($item['relation_id']);
+    }
+
     // Who knows why but field does not like it if the delta does not start at 0...
-    $items[$entity_id] = $relation_ids ? array_values(entity_load('relation', $relation_ids)) : array();
+    $items[$entity_id] = array_values(entity_load('relation', $relation_ids));
   }
 }
 
@@ -814,14 +819,22 @@ function relation_select_build_entity_keys($entity_type, $entity_id, $endpoints)
  * @param array $relation_types
  */
 function relation_select_entity_get_relations($entity_type, $entity_id, $relation_type) {
+  $r_index = NULL;
+  // If this is a directional relation, only return the relation instances where
+  // the requested entity is the parent.
+  if (relation_type_load($relation_type)->directional) {
+    $r_index = 0;
+  }
+
   // Get the relation entities for this entity's relation type
-  $relation_query = relation_query($entity_type, $entity_id);
+  $relation_query = relation_query($entity_type, $entity_id, $r_index);
   $relation_query->propertyCondition('relation_type', $relation_type);
   $relation_ids = array_keys($relation_query->execute());
   // Return empty array if we have no relations.
   if (!count($relation_ids)) {
     return array();
   }
+
   // Or query for a list of related entities.
   $query = db_select('field_data_endpoints', 'fde');
   $query->fields('fde', array(
-- 
1.7.9.5

