From 2302c575d9b17dd98d79d61d33301f98c13628b4 Mon Sep 17 00:00:00 2001
From: James Harvey <james@webmarkdesigns.com.au>
Date: Tue, 16 Jul 2013 21:57:55 +1000
Subject: [PATCH] Issue #1817956 by jamesharv, darrenmothersele: Added Field
 collection support.

---
 controllers/UUIDFieldCollectionItemEntity.inc | 21 +++++++++++++++++++++
 uuid.core.inc                                 | 16 ++++++++++++++++
 uuid.entity.inc                               |  4 ++++
 uuid.info                                     |  1 +
 4 files changed, 42 insertions(+)
 create mode 100644 controllers/UUIDFieldCollectionItemEntity.inc

diff --git a/controllers/UUIDFieldCollectionItemEntity.inc b/controllers/UUIDFieldCollectionItemEntity.inc
new file mode 100644
index 0000000..3e6d890
--- /dev/null
+++ b/controllers/UUIDFieldCollectionItemEntity.inc
@@ -0,0 +1,21 @@
+<?php
+/**
+ * @file
+ *   Overrides save functionality from FieldCollectionItemEntity in order to
+ *   avoid exceptions when deploying a field_collection entity before its host
+ *   entity exists.
+ */
+class UUIDFieldCollectionItemEntity extends FieldCollectionItemEntity {
+  public function save($skip_host_save = FALSE) {
+    if (isset($this->hostEntity) || empty($this->__uuid_universalized)) {
+      // If we have a host entity or this entity has not been universalized by deploy.
+      // $this->__uuid_universalised is set in field_collection_entity_uuid_load().
+      parent::save($skip_host_save);
+    } else {
+      // If there is no host entity, we may be receiving it from deploy
+      // so save the item directly (and force revision id to match item id)
+      $this->revision = TRUE;
+      entity_get_controller($this->entityType)->save($this);
+    }
+  }
+}
diff --git a/uuid.core.inc b/uuid.core.inc
index e54b556..ca999d4 100644
--- a/uuid.core.inc
+++ b/uuid.core.inc
@@ -319,6 +319,12 @@ function entityreference_field_uuid_presave($entity_type, $entity, $field, $inst
 function field_collection_entity_uuid_load(&$entities, $entity_type) {
   if ($entity_type == 'field_collection_item') {
     entity_property_id_to_uuid($entities, 'field_collection_item', 'value');
+
+    // Flag that this entity has been universalised.
+    // @see UUIDFieldCollectionItemEntity
+    foreach ($entities as $entity) {
+      $entity->__uuid_universalized = TRUE;
+    }
   }
 }
 
@@ -343,6 +349,16 @@ function field_collection_field_uuid_load($entity_type, $entity, $field, $instan
  */
 function field_collection_field_uuid_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
   entity_property_uuid_to_id($items, 'field_collection_item', 'value');
+
+  // Update the revision_ids of the field to match the head revisions of the entities.
+  $entity_ids = array_map(function($item) {
+    return $item['value'];
+  }, $items);
+
+  $field_collections = entity_load("field_collection_item", $entity_ids);
+  foreach ($items as &$item) {
+    $item['revision_id'] = $field_collections[$item['value']]->revision_id;
+  }
 }
 
 /**
diff --git a/uuid.entity.inc b/uuid.entity.inc
index 50081c3..0715166 100644
--- a/uuid.entity.inc
+++ b/uuid.entity.inc
@@ -65,6 +65,7 @@ function uuid_get_core_entity_info() {
       'entity keys' => array(
         'uuid' => 'uuid',
       ),
+      'entity class' => 'UUIDFieldCollectionItemEntity',
     );
   }
   return $info;
@@ -87,6 +88,9 @@ function uuid_entity_info_alter(&$info) {
     if (!empty($core_info['entity keys']['revision uuid'])) {
       $info[$entity_type]['entity keys']['revision uuid'] = $core_info['entity keys']['revision uuid'];
     }
+    if (!empty($core_info['entity class'])) {
+      $info[$entity_type]['entity class'] = $core_info['entity class'];
+    }
   }
 }
 
diff --git a/uuid.info b/uuid.info
index 03ec3a7..f43ac21 100644
--- a/uuid.info
+++ b/uuid.info
@@ -4,3 +4,4 @@ core = 7.x
 package = UUID
 configure = admin/config/system/uuid
 files[] = uuid.test
+files[] = controllers/UUIDFieldCollectionItemEntity.inc
-- 
1.7.12.4 (Apple Git-37)

