Problem/Motivation

This issue manifests itself in a couple of ways:

  • When switching from a specific language to "Not specified", since you cannot add multiple translations when this is selected, content moderation errors out attempting to add this as a new language.
  • When switching between languages, the content moderation state entity tables will continue to fill up with new translations, where the host entity will retain just one.

Using the following steps, a fatal can be triggered from the UI:

  • Add 2 languages
  • Install workflow, content moderation and content translation
  • Enable content translation and content moderation on any node bundle
  • Create a node in one of the installed languages as a "Draft".
  • Edit the node and change the language to "Not specified"

This results in:

Invalid translation language (und) specified. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 805 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\content_moderation\EntityOperations->updateOrCreateFromEntity(Object) (Line: 135)
Drupal\content_moderation\EntityOperations->entityUpdate(Object) (Line: 91)
content_moderation_entity_update(Object)
call_user_func_array('content_moderation_entity_update', Array) (Line: 402)

Proposed resolution

When the default language of an entity is being changed, change the default language of the content_moderation_state entity.

Remaining tasks

Review + commit.

User interface changes

API changes

Data model changes

Comments

lamp5 created an issue. See original summary.

timmillwood’s picture

Version: 8.3.7 » 8.6.x-dev
Issue tags: -invalid, -translation, -language, -(und), -specified, -when, -changing, -of node, -to und, -AND, -content moderation, -is enable +Needs tests

It'd be awesome to get a test to confirm this, a kernel test should be fine.

It'll need fixing in 8.6.x before being backported to 8.5.x, it'll never be fixed in 8.4.x or 8.3.x.

dunebl’s picture

I have the same error in 8.5 in a multilingual site + content moderation when I was trying to change an English node from French to English.

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">InvalidArgumentException</em>: Invalid translation language (und) specified. in <em class="placeholder">Drupal\Core\Entity\ContentEntityBase-&gt;getTranslation()</em> (line <em class="placeholder">866</em> of <em class="placeholder">core/lib/Drupal/Core/Entity/ContentEntityBase.php</em>). <pre class="backtrace">Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraintValidator-&gt;getOriginalTranslation(Object, Object) (Line: 111)
Drupal\content_translation\Plugin\Validation\Constraint\ContentTranslationSynchronizedFieldsConstraintValidator-&gt;validate(Object, Object) (Line: 185)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator-&gt;validateConstraints(Object, &#039;00000000363713070000000020a8e45f&#039;, Array) (Line: 140)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator-&gt;validateNode(Object, Array, 1) (Line: 99)
Drupal\Core\TypedData\Validation\RecursiveContextualValidator-&gt;validate(Object, NULL, NULL) (Line: 90)
Drupal\Core\TypedData\Validation\RecursiveValidator-&gt;validate(Object) (Line: 135)
Drupal\Core\TypedData\TypedData-&gt;validate() (Line: 482)
Drupal\Core\Entity\ContentEntityBase-&gt;validate() (Line: 189)
Drupal\Core\Entity\ContentEntityForm-&gt;validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 83)
Drupal\Core\Form\FormValidator-&gt;executeValidateHandlers(Array, Object) (Line: 276)
Drupal\Core\Form\FormValidator-&gt;doValidateForm(Array, Object, &#039;node_career_opportunities_edit_form&#039;) (Line: 119)
Drupal\Core\Form\FormValidator-&gt;validateForm(&#039;node_career_opportunities_edit_form&#039;, Array, Object) (Line: 575)
Drupal\Core\Form\FormBuilder-&gt;processForm(&#039;node_career_opportunities_edit_form&#039;, Array, Object) (Line: 318)
Drupal\Core\Form\FormBuilder-&gt;buildForm(&#039;node_career_opportunities_edit_form&#039;, Object) (Line: 74)
Drupal\Core\Controller\FormController-&gt;getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 666)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>
awm’s picture

Title: Invalid translation language (und) specified. When changing language of node to und and content moderation is enable » Invalid translation language (und) specified. When changing language of original node when content moderation/editorial workflow enabled.
awm’s picture

StatusFileSize
new728.84 KB
new944.82 KB
new199.11 KB
awm’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

sam152’s picture

Issue summary: View changes

Clarifying the steps to reproduce, this took me a while to figure out. Can confirm this still happens in HEAD.

sam152’s picture

Issue summary: View changes
sam152’s picture

Issue summary: View changes

Grooming the issue summary a bit, truncating some of the backtrace.

sam152’s picture

Title: Invalid translation language (und) specified. When changing language of original node when content moderation/editorial workflow enabled. » Content moderation incorrectly always assumes a language is being added when the language of an entity is being modified.
Issue summary: View changes

Updating the title to be more specific about the root cause of this issue.

sam152’s picture

Issue summary: View changes
sam152’s picture

Status: Active » Needs review
StatusFileSize
new3.2 KB

Here are some tests that a) reproduce exactly what was described in the original issue and also the additional problems identified around content moderation assuming a language is always being added. I think both are valuable even if I think the fix for these will be the same because they fail in different ways: testModerationWithSpecialLanguages fails because adding a new locked language doesn't work and testChangingContentLangcode fails because of additional assertions around the need to change the CMS entity langcode instead of adding a new one.

sam152’s picture

StatusFileSize
new4.52 KB

Attempted fix for this.

sam152’s picture

Title: Content moderation incorrectly always assumes a language is being added when the language of an entity is being modified. » Content moderation incorrectly always assumes a language is being added when the default language of an entity is being changed

The last submitted patch, 14: 2946402-14.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

sam152’s picture

Issue summary: View changes
awm’s picture

The patch did not work for me on 8.5.3. Even without content moderation installed, changing the language on a node throws this error.
Invalid translation language (und) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 866 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).

sam152’s picture

This patch does not seem to apply on 8.5.3. Can you test this against the latest version of 8.7.x. I also can't reproduce the issue with the patch applied based on the steps in the issue summary, can you provide some updated steps which trigger this?

mattshoaf’s picture

I was having an issue where a node that was originally created as "- Not specified -" couldn't be assigned to a language. The error in watchdog was InvalidArgumentException: The entity cannot be translated since it is language neutral (und). in Drupal\Core\Entity\ContentEntityBase->addTranslation() (line 872 of [...]docroot/core/lib/Drupal/Core/Entity/ContentEntityBase.php).

With this patch applied to 8.4.8 (working in a distro that is behind the update curve) I can now assign nodes to a language or change them to unspecified. Note, on this older version of Drupal, the patch only applies to EntityOperations.php, it fails to apply to ContentModerationStateTest.php, but this works as a good enough stop-gap for me until the distro is able to update core.

sam152’s picture

@mattshoaf that info is very helpful, thanks. So, waiting for an update from @awm re: #19/#20.

awm’s picture

in my case, I narrowed down the problem to the "validate" method in ContentTranslationSynchronizedFieldsConstraintValidator.php,

    if ($entity->isDefaultRevision() && !$entity->isDefaultTranslationAffectedOnly()) {
      return;
    }

entity->isDefaultTranslationAffectedOnly is returning false because it has this condition:

return !empty($bundle_info[$bundle_name]['untranslatable_fields.default_translation_affected']); 

To fix my issue, I unchecked "Hide non translatable fields on translation forms" for the content types at "admin/config/regional/content-language".
Alternatively you could use this: https://www.drupal.org/node/2938191.

That said, I am not sure why setting $entity->isDefaultTranslationAffectedOnly() is relying on

$bundle_info[$bundle_name]['untranslatable_fields.default_translation_affected']

@Sam, the patch did not work for me on 8.3. and 8.3 7 clean drush install after adding 2 languages, enabling editorial workflow for article then trying to change the language on an article.
I am attaching a db of 8.7 that you could look into.

awm’s picture

StatusFileSize
new514.44 KB
awm’s picture

StatusFileSize
new896 bytes
new896 bytes

I am attaching my attempted patch.

Status: Needs review » Needs work

The last submitted patch, 25: 2946402-16.patch, failed testing. View results

sam152’s picture

@awm it sounds like you're having a problem different to the one in the issue summary. Could you confirm and if that is the case open up a new issue describing steps to reproduce it?

awm’s picture

steps to reproduce are
0. Clean site install of 8.7
1. adding 2 languages.
2.enabling editorial workflow for article
3. add an article
4. change the language of the article.

This result in the error posted above. I believe it's the same issue.

Update this is the error I am getting:

The website encountered an unexpected error. Please try again later.InvalidArgumentException: Invalid translation language (und) specified. in Drupal\Core\Entity\ContentEntityBase->getTranslation() (line 872 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).

I could open a new issue for this.

I am creating a new issue for what I am seeing which is not related to content moderation I believe.

Here is the new issue https://www.drupal.org/project/drupal/issues/2995655

awm’s picture

david4lim’s picture

Adding patch for Drupal 8.5.x

sam152’s picture

Status: Needs work » Needs review
StatusFileSize
new4.52 KB

@awm, thanks for clarifying and opening the follow-up. Reuploading #15 with NR status.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

The patch makes perfect sense, and the test coverage looks great!

plach’s picture

+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
@@ -416,25 +416,78 @@ public function testModerationWithFieldConfigOverride() {
+    $this->assertEquals(count($entity->getTranslationLanguages()), count($content_moderation_state->getTranslationLanguages()));
...
+    $this->assertEquals(count($entity->getTranslationLanguages()), count($content_moderation_state->getTranslationLanguages()));

Can we also assert that the count is 1? ;)

sam152’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.61 KB
new4.72 KB

Okay, good point, I like this a lot better :)

jibran’s picture

Status: Needs review » Reviewed & tested by the community

#33 is addressed so back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: 2946402-34.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

sam152’s picture

Status: Needs work » Needs review
StatusFileSize
new947 bytes
new4.72 KB

Fixing linting issues.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Let's try again.

plach’s picture

Way better!

Saving credits

  • plach committed dd57111 on 8.7.x
    Issue #2946402 by Sam152, awm, jibran, lamp5, mattshoaf, amateescu:...
plach’s picture

Version: 8.7.x-dev » 8.6.x-dev

Committed dd57111 and pushed to 8.7.x. Thanks!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 37: 2946402-37.patch, failed testing. View results

sam152’s picture

Status: Needs work » Fixed
plach’s picture

Status: Fixed » Reviewed & tested by the community

This was not backported to 8.6.x yet.

  • plach committed a0c92ff on 8.6.x
    Issue #2946402 by Sam152, awm, jibran, lamp5, mattshoaf, amateescu:...
plach’s picture

Status: Reviewed & tested by the community » Fixed

Now it is :)

sam152’s picture

Ah indeed! Thanks for sorting that.

Status: Fixed » Closed (fixed)

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

berdir’s picture