Problem/Motivation
Premise: content moderation and translation is set up.
When a node is created, the moderation state value is created for that language. When a new translation is saved onto the node, the moderation state also gets a translation. This is because you can have differences in the state per translation. All fine and good, except that content_moderation only creates the translations if you actually save a translation directly (as opposed to, for example, creating a node in EN, adding 5 translations and then saving once the entire node). See EntityOperations::updateOrCreateFromEntity(). This might be fixed in #3150294: New translations for moderated nodes are not created in the initial workflow state, I see there is some code that sets a moderation state value directly when a translation is first created, which would solved this issue most likely.
However, until then, we need a solution here to create the moderation state translations when we clone entities.
Steps to reproduce
- Create a node in draft and translate it.
- Clone the node, from the original language
- Check the DB that the newly created node moderation state entity is only in EN, whereas the node you cloned had it in both languages
Proposed resolution
When cloning, take care of the moderation state, if needed.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | interdiff-3292852-6-7.txt | 3.02 KB | upchuk |
| #7 | 3292852-7.patch | 19.27 KB | upchuk |
| #6 | 3292852-6.patch | 20.1 KB | upchuk |
| #6 | interdiff-3292852-4-6.txt | 8.81 KB | upchuk |
| #4 | interdiff-3292852-2-4.txt | 3.57 KB | upchuk |
Comments
Comment #2
upchuk commentedWrote a test and patch to fix this.
So essentially, after the clone is created and saved, we can expect the moderation state to only have 1 translation (regardless of the number of translations the entity has) for the reason mention in the ticket description. So, for each language, create a translation of the moderation state with the same exact values as the original. And since the content_moderation sets to the default state all new nodes (which the clone is), it's fine to reset the moderation state to the same state for all languages, regardless if the original node had different states across its languages.
Comment #3
sinn commentedPatch #21 does what expected - moderation stated for all translations are created.
But with patch #2 I faced issue.
Step to reproduce:
1. Create a node. Publish and translate it to French language, for instance.
2. Clone French version - few "draft" moderation states with correct languages are created.
3. Publish French version - moderation state is moved to "published" for this translation
4. Publish English version - Exception is thrown
Moderation state is still "draft" for English version.
Comment #4
upchuk commentedGood catch, I fixed the issue. It was caused by the fact that when cloning a node from a translation, the newly created node would still retain the same language as default language. So if the default lang is EN and you clone from FR, the new node stays with EN as the default lang. And this is fine. However, the created content moderation state entity gets created with FR as the default language and this is inconsistent with the node. Added a test to prove this and fixed it by syncing the moderation state default language with that of the node.
Comment #5
sinn commentedPatch #4 works well.
Comment #6
upchuk commentedI discovered another issue related to this. When you have a published node with translations and you clone it, the translations get correctly the corresponding moderation state also translated. However, the node status itself is not 0 on the translations to reflect the moderation state. This is because that is set before in a presave. So I added sync of the moderation state value at the node level to match the original also in all the translations.
Comment #7
upchuk commentedAh yes, I remembered why I hadn't used the content moderation service before..cannot rely on it cause the module may not be in use.
Comment #8
joevagyok commented#7 patch works well. Moderation states translations are correctly created for each language. Also fixes the issue mentioned in #3.
Comment #9
sinn commentedPatch #7 works well.
Comment #11
upchuk commentedMerged
Comment #12
upchuk commented