diff --git a/field_collection.module b/field_collection.module index fe9911c..1ec33ba 100644 --- a/field_collection.module +++ b/field_collection.module @@ -609,6 +609,26 @@ function field_collection_field_presave($entity_type, $entity, $field, $instance if (!empty($item['entity']->is_new)) { $item['entity']->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE); } + else { + if ($entity_type == 'node' || $entity_type == 'field_collection_item') { + // Reset item_id when it's a new translation. + if (isset($entity->nid) && !$entity->nid) { + $item['entity']->item_id = ''; + } + else { + $query = new EntityFieldQuery(); + $query->fieldCondition($item['entity']->field_name, 'value', $item['entity']->item_id, '='); + if (isset($entity->nid)) { + $query->entityCondition('entity_id', $entity->nid, '!='); + } + $result = $query->execute(); + // Reset item_id if another node with the same instance exists. + if (!empty($result)) { + $item['entity']->item_id = ''; + } + } + } + } $item['entity']->save(TRUE); $item = array('value' => $item['entity']->item_id); }