diff --git a/entity_translation.module b/entity_translation.module index dd728f5..c3cd9f3 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -1259,10 +1259,14 @@ function entity_translation_field_attach_form($entity_type, $entity, &$form, &$f function entity_translation_prepare_element($element, &$form_state) { static $drupal_static_fast; if (!isset($drupal_static_fast)) { - $drupal_static_fast['source_forms'] = &drupal_static(__FUNCTION__, array()); + $drupal_static_fast = &drupal_static(__FUNCTION__, array( + 'source_forms' => array(), + 'source_form_states' => array(), + )); } $source_forms = &$drupal_static_fast['source_forms']; + $source_form_states = &$drupal_static_fast['source_form_states']; $form = $form_state['complete form']; $build_id = $form['#build_id']; $source = $element['#source']; @@ -1279,9 +1283,11 @@ function entity_translation_prepare_element($element, &$form_state) { $source_form_state = $form_state; field_attach_form($entity_type, $element['#entity'], $source_form, $source_form_state, $source); $source_forms[$build_id][$source][$entity_type][$id] = &$source_form; + $source_form_states[$build_id][$source][$entity_type][$id] = &$source_form_state; } $source_form = &$source_forms[$build_id][$source][$entity_type][$id]; + $source_form_state = $source_form_states[$build_id][$source][$entity_type][$id]; $langcode = $element['#language']; $field_name = $element['#field_name']; @@ -1292,6 +1298,7 @@ function entity_translation_prepare_element($element, &$form_state) { if (isset($source_form[$field_name][$source])) { $element[$langcode] = $source_form[$field_name][$source]; entity_translation_form_element_language_replace($element, $source, $langcode); + entity_translation_form_element_state_replace($element, $source_form[$field_name], $field_name, $source_form_state, $form_state); unset($element[$element['#previous']]); } @@ -1299,6 +1306,70 @@ function entity_translation_prepare_element($element, &$form_state) { } /** + * Helper function. Sets the right values in $form_state['field'] when using + * source language values as defaults. + */ +function entity_translation_form_element_state_replace($element, $source_element, $field_name, $source_form_state, &$form_state) { + if (isset($source_element['#language'])) { + $source = $source_element['#language']; + + // Iterate through the form structure recursively. + foreach (element_children($element) as $key) { + if (isset($source_element[$key])) { + lb_general_entity_translation_form_element_state_replace($element[$key], $source_element[$key], $key, $source_form_state, $form_state); + } + elseif (isset($source_element[$source])) { + lb_general_entity_translation_form_element_state_replace($element[$key], $source_element[$source], $key, $source_form_state, $form_state); + } + } + + if (isset($source_element[$source]['#field_parents'])) { + $source_parents = $source_element[$source]['#field_parents']; + $langcode = $element['#language']; + $parents = $element[$langcode]['#field_parents']; + $source_state = field_form_get_state($source_parents, $field_name, $source, $source_form_state); + /* This part is specific to field collections really. */ + if (isset($source_state['entity'])) { + foreach ($source_state['entity'] as $delta => $source_state_entity) { + if ($source_state_entity instanceof FieldCollectionItemEntity) { + $source_state['entity'][$delta] = entity_translation_clone_entity('field_collection_item', $source_state_entity); + } + } + } + /* End part. */ + field_form_set_state($parents, $field_name, $langcode, $form_state, $source_state); + } + } +} + +/** + * Clones the entity object and makes sure it will get saved as new entity. + * + * @return + * The cloned entity object. + * + * @see entity_ui_clone_entity() + */ +function entity_translation_clone_entity($entity_type, $entity) { + // Clone the entity and make sure it will get saved as a new entity. + $entity = clone $entity; + + $entity_info = entity_get_info($entity_type); + $entity->{$entity_info['entity keys']['id']} = FALSE; + if (!empty($entity_info['entity keys']['name'])) { + $entity->{$entity_info['entity keys']['name']} = FALSE; + } + $entity->is_new = TRUE; + + // Make sure the status of a cloned exportable is custom. + if (!empty($entity_info['exportable'])) { + $status_key = isset($entity_info['entity keys']['status']) ? $entity_info['entity keys']['status'] : 'status'; + $entity->$status_key = ENTITY_CUSTOM; + } + return $entity; +} + +/** * Helper function. Recursively replaces the source language with the given one. */ function entity_translation_form_element_language_replace(&$element, $source, $langcode) {