Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
[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
Comments
Comment #2
stefanos.petrakis@gmail.comComment #3
stefanos.petrakis@gmail.comComment #4
stefanos.petrakis@gmail.comI 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
Comment #5
joseph.olstadThanks for the patch, just testing it now.
Comment #6
joseph.olstadPatch 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.
Comment #7
stefanos.petrakis@gmail.comExcellent. 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.
Comment #8
joseph.olstadHmm, 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.
Comment #9
stefanos.petrakis@gmail.comHere is a test which covers the case described in the issue's description, and the submitted code subsequently.
Comment #10
joseph.olstadNice 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
Comment #11
stefanos.petrakis@gmail.comThanks for the check Joseph, one minor last change, I will change the message of the last assertion, from:
to
Will update that on commit.
Comment #13
joseph.olstadSorry 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.
Comment #14
joseph.olstadI'm going to take a crack at fixing this.
Comment #15
joseph.olstadClues
SELECT entity_type, bundle, entity_id, language, field_location_value FROM field_data_field_location order by entity_id asc;
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
Comment #16
joseph.olstadI 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.
Comment #17
joseph.olstadArgh, hold off the firetrucks, I might have had the wrong patch running in the wrong environment.
Comment #18
joseph.olstadleave this as fixed, I'm running some more tests.
Comment #19
joseph.olstadSorry 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
Comment #20
stefanos.petrakis@gmail.comYep, quite some noise here :-)
Good that this PEBKAC happened while I was AFK.
And in any case, happy this was a false alarm.
Cheers
Comment #21
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedTrapped 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.
Comment #22
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedUpdate: 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.
Comment #23
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedComment #25
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedComment #26
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedComment #28
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedComment #29
joseph.olstadAlex, 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.
Comment #30
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedHi 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.
Comment #31
joseph.olstadThx Alexander, haven't yet tested your patch, I will try out the patch asap to make sure it works with my configuration as well.
Comment #32
Simon Georges CreditAttribution: Simon Georges at Makina Corpus commentedEven 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!
Comment #33
plachThanks 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:
Missing newline before @param and @param description.
Missing trailing dot, comma or colon :)
its
Please replace the dash with a comma, to make the comment more readable.
This :)
Comment #34
Alexander Fedorenko CreditAttribution: Alexander Fedorenko commentedThanks 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).
Comment #35
joseph.olstad@plach & @Alexander ,
I've tested the patch, it works on my stack for the use case as described in the issue summary.
Thanks!
Comment #36
stefanos.petrakis@gmail.comThank 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()
fromlocale_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.
Comment #37
stefanos.petrakis@gmail.comNow 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.
Comment #38
stefanos.petrakis@gmail.comSo, 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.
Comment #39
joseph.olstadMight 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.
Comment #40
stefanos.petrakis@gmail.comI am going to set this to RTBC as it's essentially extending the tests suite and will commit it soon-ish.
Comment #41
joseph.olstadVery cool, once these two tests are added in it takes us up to 104 tests for entity_translation ! Great work Stefanos!
Comment #42
stefanos.petrakis@gmail.comRegarding 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.