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
Comment #2
timmillwoodIt'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.
Comment #3
duneblI 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.
Comment #4
awm commentedComment #5
awm commentedComment #6
awm commentedComment #8
sam152 commentedClarifying the steps to reproduce, this took me a while to figure out. Can confirm this still happens in HEAD.
Comment #9
sam152 commentedComment #10
sam152 commentedGrooming the issue summary a bit, truncating some of the backtrace.
Comment #11
sam152 commentedComment #12
sam152 commentedUpdating the title to be more specific about the root cause of this issue.
Comment #13
sam152 commentedComment #14
sam152 commentedHere 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:
testModerationWithSpecialLanguagesfails because adding a new locked language doesn't work andtestChangingContentLangcodefails because of additional assertions around the need to change the CMS entity langcode instead of adding a new one.Comment #15
sam152 commentedAttempted fix for this.
Comment #16
sam152 commentedComment #18
sam152 commentedComment #19
awm commentedThe 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).
Comment #20
sam152 commentedThis 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?
Comment #21
mattshoafI 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.
Comment #22
sam152 commented@mattshoaf that info is very helpful, thanks. So, waiting for an update from @awm re: #19/#20.
Comment #23
awm commentedin my case, I narrowed down the problem to the "validate" method in ContentTranslationSynchronizedFieldsConstraintValidator.php,
entity->isDefaultTranslationAffectedOnly is returning false because it has this condition:
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
@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.
Comment #24
awm commentedComment #25
awm commentedI am attaching my attempted patch.
Comment #27
sam152 commented@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?
Comment #28
awm commentedsteps 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
Comment #29
awm commentedComment #30
david4lim commentedAdding patch for Drupal 8.5.x
Comment #31
sam152 commented@awm, thanks for clarifying and opening the follow-up. Reuploading #15 with NR status.
Comment #32
amateescu commentedThe patch makes perfect sense, and the test coverage looks great!
Comment #33
plachCan we also assert that the count is 1? ;)
Comment #34
sam152 commentedOkay, good point, I like this a lot better :)
Comment #35
jibran#33 is addressed so back to RTBC.
Comment #37
sam152 commentedFixing linting issues.
Comment #38
jibranLet's try again.
Comment #39
plachWay better!
Saving credits
Comment #41
plachCommitted dd57111 and pushed to 8.7.x. Thanks!
Comment #43
sam152 commentedComment #44
plachThis was not backported to 8.6.x yet.
Comment #46
plachNow it is :)
Comment #47
sam152 commentedAh indeed! Thanks for sorting that.
Comment #49
berdirLooks like this can result in exceptions, see #3024816: EntityOperations::updateOrCreateFromEntity(): A translation already exists for the specified language.