I found that simply translating the body field of the Article content type on a new install throws a fatal error :(

  1. Install a new site with the standard profile.
  2. Log in as UID 1.
  3. Enable Content Translation + Language modules.
  4. Add a second language: go to admin/config/regional/language/add and add a second language (eg. German).
  5. Enable content translation for the Article content type: go to admin/structure/types/manage/article and enable the following options under "Language settings":
    • Show language selector on create and edit pages
    • Enable translation
  6. Enable content translation for the body field: go to admin/structure/types/manage/article/fields/node.article.body/field and enable the option "Users may translate this field".
  7. Create a node in English: go to node/add/article and create a node, filling in the title and body field. Click on "Save and Publish".
  8. Create a German translation: go to de/node/1/translations/add/en/de and click on "Save and keep published".

Result:

Integrity constraint violation: 1062 Duplicate entry '1-en' for key 'PRIMARY': INSERT INTO {node_field_revision} (nid, vid, langcode, default_langcode, title, uid, status, created, changed, promote, sticky) VALUES (...) in Drupal\Core\Entity\FieldableDatabaseStorageController->save() (line 693 of /home/pieter/drupal/core/lib/Drupal/Core/Entity/FieldableDatabaseStorageController.php).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pfrenssen’s picture

Status: Active » Needs review
FileSize
3.13 KB

I rolled the steps above in a test to see if this can be replicated on the test bot.

I find it very strange that I have to add an assertion to detect that an integrity constraint violation occurs during the test. This probably means that the exception is caught before Simpletest can grab it. Makes me wonder how many fatal errors occur during testing now but are simply not detected.

Status: Needs review » Needs work

The last submitted patch, 1: 2129039-1-content_translation-fatal_error.patch, failed testing.

Gábor Hojtsy’s picture

Priority: Major » Critical

I also reproduced this earlier several times, and we have seen people reproducing it in Prague at the sprint. (There may be other duplicate copies of this) While the translation is actually saved, there is no way out of this error, you need to navigate manually away. Therefore uping to critical.

plach’s picture

I found out what is causing this: it seems we have some weird issue with field item references, basically we are getting different revision ids per language in the entity object, aw :(

Still looking for the root cause.

Gábor Hojtsy’s picture

Adding more tags.

pfrenssen’s picture

I also noticed in #2087299: Impossible to save nodes if translation is newer than default language that this happens for other properties on entities, like the 'changed' property on nodes: when you retrieve this you always get it back for a particular translation.

I am also working on this.

plach’s picture

I think the two issues might be related.

plach’s picture

Status: Needs work » Needs review
FileSize
1.5 KB

The attched patch should fix the issue.

Since the test posted above more or less replicates what we have now, but just for nodes, and given that the bug lives in ContentEntityBase, I'd like to try to provide test coverage for this in EntityTranslationTest. Couldn't reproduce the issue so far, though.

Just a note: this does not happen if you create from scratch a plain content type with just the body field. I usually run the minimal install so I didn't experience this issue previously.

nyirocsaba’s picture

I'm not yet familiar with the Drupal 8 core, but after doing some late night and morning research on this issue I ended up with the following:
When the new revision get created in the FieldableDatabaseStorageController->saveRevision the new vid is passed only for the active language, which in this particular flow is the german (de).

nyirocsaba’s picture

nyirocsaba’s picture

FileSize
4.15 KB

Update:
1. keep the active language revision id up to date (accidentaly removed in the earlies patch)
2. include pfrenssen's test

plach’s picture

@nyirocsaba:

Sorry, but I think the approach you are following is not correct: untranslatable fields are not supposed to have different values per language, AAMOF currently we are creating references so that the field item objects for the various languages are always the same, see ContentEntityBase::getTranslatedField().

The fact that right now we can somehow store different revisions ids per language in the same entity object is completely wrong and will cause huge problems.

What we need to fix is the root cause, which seems to lie in the entity cloning process, although I am not able to reproduce the bug in the entity translation tests. However, the proper fix should be something along the lines of #8.

hass’s picture

@Plach: are you not able to repro this issue? I have installed latest dev from scratch with default install (not the minimal) and than you run directly into these issues, once translation get's enabled for article.

plach’s picture

I am not able to reproduce the issue in a test case dealing only with the entity object.

yched’s picture

(review of @plach's #8)

  1. +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
    @@ -904,10 +904,21 @@ public function __clone() {
           foreach ($this->fields as $name => $properties) {
             foreach ($properties as $langcode => $property) {
    

    While we're in here, the variable names seem really misleading here. What is called $properties is an array of (langcode => FieldItemList for the langcode). So this should be:

    foreach ($this->fields as $name => $items_by_langcode) {
      foreach ($items_by_langcode as $langcode => $items) {
    
  2. +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
    @@ -904,10 +904,21 @@ public function __clone() {
    +            $this->fields[$name][$langcode]->setContext($name, $this);
    

    Looks like this is done in both cases, so it could go out of the if / else block ?

  3. +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
    @@ -904,10 +904,21 @@ public function __clone() {
    +          // Untranslatable fields have multiple references for the same field
    +          // object keyed by language. To avoid creating different field objects
    +          // we retain just the original value, as references will be recreated
    +          // later as needed.
    +          elseif ($langcode == Language::LANGCODE_DEFAULT) {
    +            $this->fields[$name] = array($langcode => clone $property);
    +            $this->fields[$name][$langcode]->setContext($name, $this);
    +          }
    

    In the case of untranslatable fields, we only act on $langcode == LANGCODE_DEFAULT because that's the only langcode that's supposed to be present in $this->fields[$name], right ?
    So maybe in the case of untranslatable fields, no need to foreach on langcodes, just do the one needed stuff on LANGCODE_DEFAULT ? Would make the logic easier to follow IMO.

plach’s picture

I was not able to reproduce the issue in the Entity Translation tests but I added test coverage anyway. This merges also the test provided by @pfrenssen with the existing node translation tests. We need quite some refactoring to make those pass with the standard profile (including fixing a bug caused by form cache in the entity form controller).

This should also address @yched's review: I opted for using $values instead of $items_by_langcode (which is quite verbose) for consistency with the field caching code.

plach’s picture

Re-uploading the test-only patch (should fail tests).

plach’s picture

Come on, bot :(

plach’s picture

Last try for tonight

Gábor Hojtsy’s picture

Don't know why those did not go for tests... maybe some undocumented file name pattern?!

Status: Needs review » Needs work

The last submitted patch, 20: et-clone_refs_broken-2129039-16-should-fail-tests-only.patch, failed testing.

plach’s picture

Status: Needs work » Needs review

Finally :)

Gábor Hojtsy’s picture

@yched, @hass, @nyirocsaba, @pfrenssen any feedback on the patch?

yched’s picture

yched’s picture

Oh, sorry - the fails for #20 were expected, actual patch is in #16.
Reviewing.

yched’s picture

Status: Needs review » Reviewed & tested by the community

Yup, looks good :-)
(although, not sure why this is filed for content_translation.module while the fix is in ContentEntityBase ?)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: et-clone_refs_broken-2129039-16-should-fail-tests-only.patch, failed testing.

yched’s picture

Status: Needs work » Reviewed & tested by the community

RTBC as per #26 - #16 is the patch to commit.

plach’s picture

Gábor Hojtsy’s picture

Issue tags: +alpha target

Let's get this in the Monday alpha :)

hass’s picture

Still get this when adding a translation:

At URL: node/2/translations/add/de/en

InvalidArgumentException: Invalid translation language (en) specified. in Drupal\Core\Entity\ContentEntityBase->addTranslation() (line 691 of core\lib\Drupal\Core\Entity\ContentEntityBase.php).

I have created an English translation of a German article. I also get Article English article 2 has been updated.

EDIT: And afterwards I see two English articles listed on project home (English UI) where only one should be shown. Also with the same URL alias... it's just shown twice. I guess every translation will add an item. If I switch to German I see two times the German article.

plach’s picture

I saw that in Prague but I was not able to reproduce it reliably. Can you post the steps? I suspect it might be a different issue.

And afterwards I see two English articles listed on project home (English UI) where only one should be shown. Also with the same URL alias... it's just shown twice. I guess every translation will add an item. If I switch to German I see two times the German article.

See #2006606: Views rendering ignores entity language, eg: Identical nodes rendered in views when nodes have translations.

hass’s picture

It's easy to repro here. Just add a translation to an existing node and save.

hass’s picture

UHHH, uncheck Create new revision!

jthorson’s picture

Status: Reviewed & tested by the community » Needs work

Just playing around to look into the untested files issue. Please ignore.

jthorson’s picture

Status: Needs work » Needs review
jthorson’s picture

Status: Needs review » Reviewed & tested by the community
jthorson’s picture

jthorson’s picture

hass’s picture

jthorson’s picture

Trying to reproduce #2136501: Sometimes patches are not sent for testbot. ... sorry for the noise. :( Still RTBC, and the patch in #16 is still the one to commit.

jthorson’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 41: et-clone_refs_broken-2129039-16.tests_.patch, failed testing.

jthorson’s picture

Status: Needs work » Reviewed & tested by the community

Bizarre ... can't explain why the bot wasn't picking up the patches, but can't reproduce it either. :(

Back to RTBC and restored the patch from #16 to the top of the table.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

tstoeckler’s picture

Status: Fixed » Reviewed & tested by the community
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Shoot, I am so sorry! This is why I should not commit patches while on the phone. :P~

Reverted previous; committed and pushed #16 to 8.x. Let's see how it likes that. :)

Gábor Hojtsy’s picture

Issue tags: -sprint

Thanks all!

Status: Fixed » Closed (fixed)

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