Skip to content

Instantly share code, notes, and snippets.

@TimHolt
Last active December 20, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimHolt/6041321 to your computer and use it in GitHub Desktop.
Save TimHolt/6041321 to your computer and use it in GitHub Desktop.
diff --git a/controllers/UUIDFieldCollectionItemEntity.inc b/controllers/UUIDFieldCollectionItemEntity.inc
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment