diff --git a/entity_translation.module b/entity_translation.module index 1668b68..4183852 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -1604,6 +1604,9 @@ function entity_translation_field_attach_submit($entity_type, $entity, $form, &$ // Update the wrapped entity with the submitted values. $handler->setEntity($entity); $handler->entityFormSubmit($form, $form_state); + + // Process in-place translations for the taxonomy autocomplete widget. + entity_translation_taxonomy_term_field_attach_submit($entity_type, $entity, $form, $form_state); } } diff --git a/entity_translation.taxonomy.inc b/entity_translation.taxonomy.inc index 4cdb596..ab85fea 100644 --- a/entity_translation.taxonomy.inc +++ b/entity_translation.taxonomy.inc @@ -154,11 +154,13 @@ function entity_translation_form_field_ui_field_edit_taxonomy_autocomplete_form_ * * @param array $element * The widget form element. + * @param array $form_state + * The form state array. * * @return bool * TRUE if in-place translation is enabled, FALSE otherwise. */ -function _entity_translation_taxonomy_autocomplete_translation_enabled($element) { +function _entity_translation_taxonomy_autocomplete_translation_enabled($element, $form_state) { $field = field_info_field($element['#field_name']); if (field_is_translatable($element['#entity_type'], $field)) { return FALSE; @@ -166,13 +168,18 @@ function _entity_translation_taxonomy_autocomplete_translation_enabled($element) $entity_type = 'taxonomy_term'; $parent_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']); - $langcode = $parent_handler->getActiveLanguage(); + $active_langcode = $parent_handler->getActiveLanguage(); $translations = $parent_handler->getTranslations(); + $entity_langcode = isset($translations->original) ? $translations->original : LANGUAGE_NONE; list(,, $bundle) = entity_extract_ids($element['#entity_type'], $element['#entity']); $instance = field_info_instance($element['#entity_type'], $field['field_name'], $bundle); + // We need to make sure that we are not dealing with a translation form. + // However checking the active language is not enough, because the user may + // have changed the entity language. return - isset($translations->original) && $translations->original != $langcode && + (isset($form_state['entity_translation']['is_translation']) ? + $form_state['entity_translation']['is_translation'] : ($active_langcode != $entity_langcode)) && !empty($instance['settings']['entity_translation_taxonomy_autocomplete_translate']) && (user_access('translate any entity') || user_access("translate $entity_type entities")); } @@ -201,7 +208,7 @@ function entity_translation_field_widget_taxonomy_autocomplete_form_alter(&$elem // If we are using the regular autocomplete behavior also in translation // forms, we need to set our custom callback. - if (!_entity_translation_taxonomy_autocomplete_translation_enabled($element)) { + if (!_entity_translation_taxonomy_autocomplete_translation_enabled($element, $form_state)) { $element['#autocomplete_path'] = 'entity_translation/' . $entity_type . '/autocomplete/' . $langcode . '/' . $element['#field_name']; $translations = $parent_handler->getTranslations(); if (isset($translations->original) && $translations->original != $langcode) { @@ -397,10 +404,11 @@ function entity_translation_taxonomy_autocomplete_validate($element, &$form_stat // This is the language of the parent entity, that we will be applying to new // terms. $parent_handler = entity_translation_get_handler($element['#entity_type'], $element['#entity']); - $langcode = $parent_handler->getActiveLanguage(); + $langcode = !empty($form_state['entity_translation']['form_langcode']) ? + $form_state['entity_translation']['form_langcode'] : $parent_handler->getActiveLanguage(); // Handle in-place translation. - if (_entity_translation_taxonomy_autocomplete_translation_enabled($element)) { + if (_entity_translation_taxonomy_autocomplete_translation_enabled($element, $form_state)) { // The referenced terms cannot change, so we just need to collect their term // identifiers. We also build a map of the corresponding deltas for later // use. @@ -415,6 +423,9 @@ function entity_translation_taxonomy_autocomplete_validate($element, &$form_stat $entity_type = 'taxonomy_term'; $name_field = 'name_field'; $source_langcode = $parent_handler->getSourceLanguage(); + // This is a validation handler, so we must defer the actual save to the + // submit phase. + $terms_to_save = &$form_state['entity_translation']['taxonomy_autocomplete'][$element['#entity_type']][$id][$element['#field_name']]; foreach (taxonomy_term_load_multiple(array_keys($deltas)) as $term) { // This is also the right context to perform validation. $term_translation = $element[$deltas[$term->tid]]['#value']; @@ -443,14 +454,14 @@ function entity_translation_taxonomy_autocomplete_validate($element, &$form_stat $name_field => array($typed_langcode => array(array('value' => $term_translation))), ); $handler->setTranslation($translation, $translation_values); - entity_translation_entity_save($entity_type, $term); + $terms_to_save[] = $term; } // Otherwise we just update the existing translation, if it has changed. // If the term is language-neutral, we just update its main value. This is // expected to happen normally, but could when referencing existing terms. elseif ($term_translation != _entity_translation_taxonomy_label($term, $typed_langcode)) { $term->{$name_field}[$typed_langcode][0]['value'] = $term_translation; - entity_translation_entity_save($entity_type, $term); + $terms_to_save[] = $term; } } } @@ -502,7 +513,6 @@ function entity_translation_taxonomy_autocomplete_validate($element, &$form_stat } } - // FIXME: language change // Now collect the identifiers for the various terms and update the taxonomy // reference field values. foreach ($typed_tags as $delta => $typed_tag) { @@ -528,3 +538,21 @@ function entity_translation_taxonomy_autocomplete_validate($element, &$form_stat form_set_value($element, $value, $form_state); } + +/** + * Term-specific implementation of hook_field_attach_submit(). + */ +function entity_translation_taxonomy_term_field_attach_submit($entity_type, $entity, $form, &$form_state) { + // Finally save in-place translations + if (!empty($form_state['entity_translation']['taxonomy_autocomplete'])) { + foreach ($form_state['entity_translation']['taxonomy_autocomplete'] as $entity_type => $entity_type_data) { + foreach ($entity_type_data as $id => $field_name_data) { + foreach ($field_name_data as $field_name => $term_data) { + foreach ($term_data as $term) { + entity_translation_entity_save('taxonomy_term', $term); + } + } + } + } + } +}