From d1e8ae45351015910c0dd449d9b8615a44ffc0f7 Mon Sep 17 00:00:00 2001
From: Darren Oh <darren@oh.name>
Date: Sun, 30 Oct 2016 14:29:06 -0400
Subject: [PATCH] Issue #1390708 by milesw, jamsilver, Darren Oh: Fixed crash
 with reference to missing entity.

---
 uuid.entity.inc | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/uuid.entity.inc b/uuid.entity.inc
index 4306339..dd69b3d 100644
--- a/uuid.entity.inc
+++ b/uuid.entity.inc
@@ -601,33 +601,46 @@ function entity_property_uuid_to_id(&$objects, $entity_type, $properties) {
   if (!is_array($properties)) {
     $properties = array($properties);
   }
-  $uuids = array();
-  $values = array();
-  $i = 0;
-  foreach ($things as &$object) {
+  foreach ($things as $k => &$object) {
     foreach ($properties as $property) {
       // This is probably an entity object.
       if (is_object($object) && isset($object->{$property})) {
-        $values[$i] = &$object->{$property};
+        $value = &$object->{$property};
       }
       // This is probably a field items array.
       elseif (is_array($object) && isset($object[$property])) {
-        $values[$i] = &$object[$property];
+        $value = &$object[$property];
       }
       else {
-        $i++;
         continue;
       }
-      if (uuid_is_valid($values[$i])) {
-        $uuids[] = $values[$i];
+      if (uuid_is_valid($value)) {
+        $ids = entity_get_id_by_uuid($entity_type, array($value));
+      }
+      else {
+        $ids = array();
+      }
+      if (is_object($object)) {
+        if (isset($ids[$value])) {
+          $object->{$property} = $ids[$value];
+        }
+        // The UUID lookup failed, remove the item.
+        else {
+          unset($object->{$property});
+        }
+      }
+      elseif (is_array($object)) {
+        if (isset($ids[$value])) {
+          $object[$property] = $ids[$value];
+        }
+        // The UUID lookup failed, remove the item.
+        elseif (is_array($objects)) {
+          unset($objects[$k]);
+        }
+        else {
+          unset($object[$property]);
+        }
       }
-      $i++;
-    }
-  }
-  $ids = entity_get_id_by_uuid($entity_type, $uuids);
-  foreach ($values as $i => $value) {
-    if (isset($ids[$value])) {
-      $values[$i] = $ids[$value];
     }
   }
 }
-- 
2.9.3 (Apple Git-75)

