When generating the form for a new translation, entity_translation follows the following steps:

  • Generate a copy of the form for the source language
  • Copy the form elements for each translatable field into the main form
  • Recursively iterate through the copied elements and set #language to the target language

The problem arises when a field type makes use of the #parents or the #field_parents attributes, as they will typically contain the source language. This causes form values to be saved to the source language instead of the target language, even though the language of the form element is the target language (took me a long time to figure out what was happening here).

The attached patch modifies the recursive function to also update #parents and #field_parents attributes.

For a real-life example, try using a field_collection field which has been set to translatable (note that field_collection needs #1366220: Field collection translatable Field language for setHostEntity to work).

Comments

plach’s picture

Status: Needs review » Needs work

Thanks for the patch, it looks mostly good and tests pass so I'm feeling pretty confident to commit it once the things below are fixed. I guess we don't need test coverage since this is likely to go away once #1282018: Improve UX of language-aware entity forms lands.

+++ b/entity_translation.admin.inc
@@ -344,11 +344,27 @@ function entity_translation_edit_form($form, $form_state, $entity_type, $entity,
+function _entity_translation_form_language(&$element, $langcode, $source) {

I'd reverse the $source and $langcode parameters to improve readability: IMO converting from source to target feels more natural.

+++ b/entity_translation.admin.inc
@@ -344,11 +344,27 @@ function entity_translation_edit_form($form, $form_state, $entity_type, $entity,
+  // Replace specific occurrences of the source language with the target language.

Comment does not wrap at column 80.

+++ b/entity_translation.admin.inc
@@ -344,11 +344,27 @@ function entity_translation_edit_form($form, $form_state, $entity_type, $entity,
+    if ($key == '#language') {

Do we need a strict equality operator here as below?

floretan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.81 KB

Thanks for the feedback, here's an updated patch with your three points taken into account.

plach’s picture

Status: Needs review » Fixed

Committed to HEAD, thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Add reference to field_collection patch.

  • Commit 082ac9d on master, et-permissions-1829630, factory, et-fc, revisions authored by floretan, committed by plach:
    Issue #1367832 by floretan: Fixed Check #parents and #field_parents()...

  • Commit 082ac9d on master, et-permissions-1829630, factory, et-fc, revisions, workbench authored by floretan, committed by plach:
    Issue #1367832 by floretan: Fixed Check #parents and #field_parents()...