[originally reported in #2863794: Translated content is lost when cloning]

Description

Using ET (Entity Translation) Beta6 on a content type that is configured to use Content Translation, breaks the editing process; ET-enabled fields do not get instantiated during translating to the values in the source language. They also do not display the correct language version; when viewing the node in the target language.

How to reproduce

[Adjusted from #2863794-5: Translated content is lost when cloning]
System configuration:

  • English default language
  • French enabled language
  • Entity Translation Beta6
  • Configure your Content Type to use Content Translation and configure at least one of the fields to use Entity Translation

Steps (default field values not initialized to the source language) :

  • Add new node, populate english fields with english text.
  • Translate new node, english fields not copied over to the french fields.
  • Additionally, english fields show up in the french when viewing the node in french
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joseph.olstad created an issue. See original summary.

stefanos.petrakis@gmail.com’s picture

Title: field copy/node clone fails with content translation for fields with translation enabled » Field copy fails with content translation for fields with entity translation enabled
Issue summary: View changes
stefanos.petrakis@gmail.com’s picture

Issue summary: View changes
stefanos.petrakis@gmail.com’s picture

Category: Support request » Bug report
FileSize
590 bytes

I updated the issue adding the steps to reproduce the problem. Edit at will if you notice sth odd.
And here is a patch for you to have a go at.

Most importantly, this code can adjust the entity being prepared for translation (using content translation), and call setActiveLanguage() on its EntityTranslationHandler to set the correct active language.

Ref: field_attach_prepare_translation

joseph.olstad’s picture

Thanks for the patch, just testing it now.

joseph.olstad’s picture

Status: Active » Reviewed & tested by the community

Patch works great! Thanks

I backed off the other related patch from the other issue and applied this new patch.

tested use case scenario, success!

Fixes issue.

stefanos.petrakis@gmail.com’s picture

Status: Reviewed & tested by the community » Needs work

Excellent. For completeness I would like to have tests added for this case, I will set this to "Needs work" and will try to work and provide some testing code in the next days. Unless you are faster. ;)

Thanks for checking and happy it worked for you.

joseph.olstad’s picture

Hmm, ya writing a test case for this, challenging to say the least, all the power to you. I suppose you can pilfer some existing test code from entity_translation and maybe pilfer some test code from i18n , put them together, and make a test for this use case.

stefanos.petrakis@gmail.com’s picture

Here is a test which covers the case described in the issue's description, and the submitted code subsequently.

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

Nice work Stefanos!
Its got the test, the code works, tests looks good, it passes with new code change.

I'd say that you've pretty much got it covered. Thanks

stefanos.petrakis@gmail.com’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the check Joseph, one minor last change, I will change the message of the last assertion, from:

"Original body value correctly populated."

to

"Body field correctly instantiated with the value of the source language".

Will update that on commit.

  • stefanos.petrakis committed a837e38 on 7.x-1.x
    Issue #2870524 by stefanos.petrakis, joseph.olstad: Field copy fails...
joseph.olstad’s picture

Status: Fixed » Needs work

Sorry to say this, but we missed a test case.

While the fields 'appear' to be copied over correctly, when going back a second time to edit and view, they are missing. fill out the data again, and it'll save correctly.

I'm currently investigating.

joseph.olstad’s picture

I'm going to take a crack at fixing this.

joseph.olstad’s picture

Clues

SELECT entity_type, bundle, entity_id, language, field_location_value FROM field_data_field_location order by entity_id asc;

#entity_type, bundle, entity_id, language, field_location_value
'node',           'article', '43624',  'en',         'test news article 6 EN location'
'node',           'article', '43625',  'en',         'test news article 6 FR location'

in the second row for the translated content node, the field language should be: 'fr' for entity_id 43624 instead of 'en'

investigating...

sorry for the trouble

joseph.olstad’s picture

I believe the permanent solution to this is in doing a variable_get on language_content_type_%BUNDLE%
where %BUNDLE% is the name of your content type.

if the value of this variable is 4 , that means entity_translation enabled
if the value of this variable is 2, that means content translation enabled

using this information , I will quickly create a new patch and have this issue licked for good.

joseph.olstad’s picture

Argh, hold off the firetrucks, I might have had the wrong patch running in the wrong environment.

joseph.olstad’s picture

Status: Needs work » Fixed

leave this as fixed, I'm running some more tests.

joseph.olstad’s picture

Sorry for the noise, yes the issue is Fixed , confirmed. The anomaly I was observing was a temporary PEBKAC issue. the PEBKAC issue has been resolved.

Thanks

stefanos.petrakis@gmail.com’s picture

Yep, quite some noise here :-)
Good that this PEBKAC happened while I was AFK.
And in any case, happy this was a false alarm.
Cheers

Alexander Fedorenko’s picture

Trapped with the issue mentioned in #13 after update of entity translation from beta5 to beta6 and applying #9 patch.

System configuration:
- English language is default;
- German enabled as second language;
- Content type with "Multilingual support" set into "Enabled, with translation".
- Part of the fields are translatable as per "Field translation" option on field edit page.

When issue occurs:
- Original node is being created in default language (EN).
- Translation form gets original values for multilingual fields thanks to the patch #9.
- After node is being saved - everything looks fine, but when translated node is being edited - multilingual fields are empty.

What seems to be happen in the backend:
- As a final step - node has multilingual fields saved with the wrong keys (eg $node->field['en'][0]['value'] instead of desirable $node->field['de'][0]['value']).
- After form is being submitted and when form submit handlers are not yet executed (includes/form.inc:1520 form_execute_handlers()) - $form_state['values'] array contains submitted data with the proper language keys. When it comes to locale_field_node_form_submit() and tries to get current entity language (modules/locale/locale.module:432 locale_field_entity_form_submit()) - it gets default language. It tries to load entity language with the updated getFormLanguage() method and always fall back to the default language because translated entity is new and no active language is set in the translation handler at this point. [This is the reason why field values are being visible on node edit form after second submit - getFormLanguage() gets active language properly in this case.]
- At the same time at locale_field_node_form_submit() $form_state['values'] array is being updated for the multilingual fields, basicaly input is being flipped - it removes all the values with proper language keys and sets all values as if they were set for default language.
- This mess-up field submit process (modules/field/field.attach.inc:890 field_attach_submit()) and incorrect values are beings saved inside of the node.

Alexander Fedorenko’s picture

Update: problem occurs in "Locale" module because it takes $form_state['values'] array, converts it into the object and process field values basing on that information. Values array doesn't contain information about entity_translation_handler_id, which force EntityTranslationHandlerFactory to create new instance of translation handler (with no active language) when "Locale" tries to get current language of an entity. Proposed patch is in attachments.

Alexander Fedorenko’s picture

Status: Fixed » Needs review

Status: Needs review » Needs work
Alexander Fedorenko’s picture

Alexander Fedorenko’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Alexander Fedorenko’s picture

joseph.olstad’s picture

Alex, my comment #13 was a gaffe on my part, I fixed my dirty entity_translation module by backing off a different conflicting patch.

using the dev version that contains this patch fixed my issue.

I also use the locale module, your use case looks the same as mine, if there is something new that you discovered, is there an assert test we can test for? The tests added by the maintainer appear to cover the scenario afaik.

With that said, curiously your patch does pass simpletest testing. But so does head without your patch.

Alexander Fedorenko’s picture

Hi Joseph,
I've extended proposed patch with the new testcase which covers mentioned issue. Existing testcases were not covering this scenario.
Additionally checked that problem can be reproduced on clean drupal installation, so it's not something related to my environment.

Patch #30 can be applied on top of dev version of module.

joseph.olstad’s picture

Thx Alexander, haven't yet tested your patch, I will try out the patch asap to make sure it works with my configuration as well.

Simon Georges’s picture

Even without Content Translation enabled, this patch fixed the problem I mentioned in #2877103: Content can not be created in another language as the one chosen by configuration. Thanks!

plach’s picture

Status: Needs review » Needs work

Thanks for the patch! Visually the solution looks good to me, but I'll wait for Joseph to test it before committing it.

Meanwhile here a few minor remarks around coding standards and comments:

  1. +++ b/includes/translation.handler.inc
    @@ -1533,8 +1533,9 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
    +   * @param $form_state
    

    Missing newline before @param and @param description.

  2. +++ b/includes/translation.handler.inc
    @@ -1542,6 +1543,15 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
    +    // Pass entity_translation_handler_id into form_state if it's available
    

    Missing trailing dot, comma or colon :)

  3. +++ b/includes/translation.handler.inc
    @@ -1542,6 +1543,15 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
    +    // "Locale" module uses $form_state['values'] as an entity object in it's
    

    its

  4. +++ b/includes/translation.handler.inc
    @@ -1542,6 +1543,15 @@ class EntityTranslationDefaultHandler implements EntityTranslationHandlerInterfa
    +    // form state - entity language is not being determined correctly.
    

    Please replace the dash with a comma, to make the comment more readable.

  5. +++ b/tests/entity_translation.test
    @@ -692,4 +723,20 @@ class EntityTranslationContentTranslationTestCase extends EntityTranslationTestC
    \ No newline at end of file
    

    This :)

Alexander Fedorenko’s picture

Thanks for the remarks!
Here is the updated version of the patch with fixed coding standard issues which are mentioned above and which were listed here: https://www.drupal.org/pift-ci-job/668199 (only for the lines which were added).

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

@plach & @Alexander ,
I've tested the patch, it works on my stack for the use case as described in the issue summary.

Thanks!

stefanos.petrakis@gmail.com’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#2877074: Refactor the entity_translation_language() callback to make it bundle-specific

Thank you all for the work so far. I will set this back to "Needs work", as I believe there is a less obscure alternative available over at #2877074: Refactor the entity_translation_language() callback to make it bundle-specific.

The patch from #2877074-3: Refactor the entity_translation_language() callback to make it bundle-specific (plus the one that solves the broken test #2877074-5: Refactor the entity_translation_language() callback to make it bundle-specific) makes writing the locale specific workaround (array2object property) unnecessary. Specifically, the call to entity_language() from locale_field_entity_form_submit() will now correctly get the node's set language, instead of being handled by an ET handler even though the node is not under ET's jurisdiction. In my view, that was the high-level problem, more than anything else.

And similar to the recent patches made available here (thank you @Alexander), that patch also solves the problem reported over at #2877103: Content can not be created in another language as the one chosen by configuration. Update: The problem in #2877103: Content can not be created in another language as the one chosen by configuration cannot be reproduced following the steps in the description and therefore patching it does not really provide any insights. I would ignore this for the time being, till we know how to reproduce that problem.

We could definitely keep the testFieldsSaveUsingContentTranslation tests provided here (I tested them separately together with the patches from #2877074: Refactor the entity_translation_language() callback to make it bundle-specific and they came out green).

One way or the other, we will get to the bottom of this pretty soon I believe.

stefanos.petrakis@gmail.com’s picture

Now that #2877074: Refactor the entity_translation_language() callback to make it bundle-specific has been commited (2100a4d) and as mentioned in #36, we don't need the code changes suggested.

I am rerolling the patch from #34 keeping the tests though, for the robot to test and to prove that the current dev now supports these cases.

stefanos.petrakis@gmail.com’s picture

So, the tests provided by @Alexander are passing as well as the rest of ET's tests.
It would be great if @Alexander could give some confirmation too, whether the problem is solved with the latest dev.
And then we can give this a push.

joseph.olstad’s picture

Might as well push the tests into dev from #37 #2870524-37: Field copy fails with content translation for fields with entity translation enabled
having extra tests is a good thing.

stefanos.petrakis@gmail.com’s picture

Version: 7.x-1.0-beta6 » 7.x-1.x-dev
Status: Needs review » Reviewed & tested by the community

I am going to set this to RTBC as it's essentially extending the tests suite and will commit it soon-ish.

joseph.olstad’s picture

Very cool, once these two tests are added in it takes us up to 104 tests for entity_translation ! Great work Stefanos!

stefanos.petrakis@gmail.com’s picture

Status: Reviewed & tested by the community » Fixed

Regarding the tests: I had a better look at the tests provided in #34 and they don't provide any checks regarding Entity Translation, they are simple Content Translation tests.

I will not add the tests and close the issue as the OP has already been addressed.

Status: Fixed » Closed (fixed)

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