diff --git a/field_collection.module b/field_collection.module index 66a4179..12c8f21 100644 --- a/field_collection.module +++ b/field_collection.module @@ -883,17 +883,36 @@ function field_collection_field_settings_form($field, $instance) { * creation or to save changes to the host entity and its collections at once. */ function field_collection_field_presave($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) { - foreach ($items as &$item) { + foreach ($items as $key => &$item) { // In case the entity has been changed / created, save it and set the id. // If the host entity creates a new revision, save new item-revisions as // well. if (isset($item['entity']) || !empty($host_entity->revision)) { - - if ($entity = field_collection_field_get_entity($item)) { - +dpm(field_collection_field_get_entity($item)); + if ($entity = field_collection_field_get_entity($item)) { if (!empty($entity->is_new)) { $entity->setHostEntity($host_entity_type, $host_entity, LANGUAGE_NONE, FALSE); } + else { + if ($host_entity_type == 'node' || $host_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 = ''; + } + } + } + } // If the host entity is saved as new revision, do the same for the item. if (!empty($host_entity->revision)) { @@ -914,6 +933,13 @@ function field_collection_field_presave($host_entity_type, $host_entity, $field, ); } } + else { + // Prevent collections from being attached to newly translated content + // when the field collection widget is set to hidden. + if ($host_entity_type == 'node' && isset($entity->translation_source)) { + unset($items[$key]); + } + } } }