diff --git a/field_collection.module b/field_collection.module index 30f175e..2a2e827 100644 --- a/field_collection.module +++ b/field_collection.module @@ -595,6 +595,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 exists other node with the same instance + if (!empty($result)) { + $item['entity']->item_id = ''; + } + } + } + } $item['entity']->save(TRUE); $item = array('value' => $item['entity']->item_id); } @@ -1399,4 +1419,4 @@ function field_collection_devel_generate($object, $field, $instance, $bundle) { $field_collection->save(TRUE); return array('value' => $field_collection->item_id); -} +} \ No newline at end of file