diff --git a/field_collection.module b/field_collection.module index 6462014..3636812 100644 --- a/field_collection.module +++ b/field_collection.module @@ -892,7 +892,7 @@ function field_collection_field_settings_form($field, $instance) { */ function field_collection_field_insert($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) { foreach ($items as &$item) { - if (isset($item['entity'])) { + if (!empty($item)) { if ($entity = field_collection_field_get_entity($item)) { if (!empty($entity->is_new)) { $entity->setHostEntity($host_entity_type, $host_entity, LANGUAGE_NONE, FALSE); @@ -902,6 +902,26 @@ function field_collection_field_insert($host_entity_type, $host_entity, $field, 'value' => $entity->item_id, 'revision_id' => $entity->revision_id, ); + if ($entity = field_collection_field_get_entity($item)) { + if (!empty($host_entity->is_new) && empty($entity->is_new)) { + // If the host entity is new but we have a field_collection that is not + // new, it means that its host is being cloned. Thus we need to clone + // the field collection entity as well. + $new_entity = clone $entity; + $new_entity->item_id = NULL; + $new_entity->revision_id = NULL; + $new_entity->is_new = TRUE; + $entity = $new_entity; + } + if (!empty($entity->is_new)) { + $entity->setHostEntity($host_entity_type, $host_entity, LANGUAGE_NONE, FALSE); + } + $entity->save(TRUE); + $item = array( + 'value' => $entity->item_id, + 'revision_id' => $entity->revision_id, + ); + } } } } @@ -1660,14 +1680,14 @@ function field_collection_field_widget_render_required($element) { * FAPI validation of an individual field collection element. */ function field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) { - $instance = field_widget_instance($element, $form_state); - $field = field_widget_field($element, $form_state); + $instance = @field_widget_instance($element, $form_state); + $field = @field_widget_field($element, $form_state); $field_parents = $element['#field_parents']; $field_name = $element['#field_name']; $language = $element['#language']; $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state); - $field_collection_item = $field_state['entity'][$element['#delta']]; + $field_collection_item = $element['#entity']; // Attach field API validation of the embedded form. field_attach_form_validate('field_collection_item', $field_collection_item, $element, $form_state); @@ -1911,3 +1931,19 @@ function field_collection_devel_generate($object, $field, $instance, $bundle) { return array('value' => $field_collection->item_id); } + +/** + * Implements hook_field_attach_presave() + */ +function field_collection_field_attach_presave($entity_type, $entity) { + // Clear the item_id on the field collection entity before saving in order to force creation of a new field + // collection rather than overwriting the original + if (empty($entity->is_new)) { + if ($entity_type == 'field_collection_item') { + if (isset($entity->item_id)) { + $entity->item_id = ''; + $entity->save(TRUE); + } + } + } +} \ No newline at end of file