I completed a content migration from a Drupal 7 site to a new Drupal 8 site using the upgrade GUI. Migration was pretty painless. Months later I enabled core content moderation and turned content moderation on for my custom content type "Collection Item". With content moderation on, I was no longer able to save my Collection Item nodes without seeing: The website encountered an unexpected error. Please try again later.

Two errors register, as follows:

Drupal\Core\Entity\EntityStorageException: Invalid translation language (und) specified. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 770 of /Applications/MAMP/htdocs/project/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

And...

InvalidArgumentException: Invalid translation language (und) specified. in Drupal\Core\Entity\ContentEntityBase->addTranslation() (line 823 of /Applications/MAMP/htdocs/project/core/lib/Drupal/Core/Entity/ContentEntityBase.php).

I then enable the devel module and visit /devel/php where I load my node to inspect the language settings:

$node = node_load(3743);
dpm($node->language());

And I see that language is "und":

stdClass Object
(
    [__CLASS__] => Drupal\Core\Language\Language
    [defaultValues] => Array
        (
            [id] => en
            [name] => English
            [direction] => ltr
            [weight] => 0
            [locked] => 
        )

    [name] => Not specified
    [id] => und
    [direction] => ltr
    [weight] => 2
    [locked] => 1
)

This appears to be legacy data that came over from Drupal 7. The language of 'und' prevents me from saving / updating the node when content moderation has been enabled for the content type. If I disable content moderation, I'm able to save the node as normal.

If I manually change the node's langcode to 'en' via some php code at devel/php, I'm able to save the node with content moderation enabled. Also you can see the language setting has changed appropriately:

$node = node_load(3743);
$node->langcode = 'en';
$node->save();
dpm($node->language());
stdClass Object
(
    [__CLASS__] => Drupal\Core\Language\Language
    [defaultValues] => Array
        (
            [id] => en
            [name] => English
            [direction] => ltr
            [weight] => 0
            [locked] => 
        )

    [name] => English
    [id] => en
    [direction] => ltr
    [weight] => 0
    [locked] => 
)

Comments

eric.toupin created an issue. See original summary.

timmillwood’s picture

Issue tags: +Workflow Initiative
dmitri.daranuta’s picture

dmitri.daranuta’s picture

Status: Active » Needs review
dmitri.daranuta’s picture

Version: 8.2.6 » 8.3.1
timmillwood’s picture

Version: 8.3.1 » 8.4.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks, nice start.

We really need tests for this though.

+++ b/core/modules/content_moderation/src/EntityOperations.php
@@ -189,8 +190,9 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
+    $languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);

LanguageManager needs to be injected.

tacituseu’s picture

I think this should be combined with solution from #2856917: Moderating an entity that uses a special language (e.g. LANGCODE_NOT_SPECIFIED) throws exception, that is create it with the same langcode as entity in the first place, otherwise there is a mismatch, entity is in 'und' and the content_moderation_state in site's default ('en') might cause problems with views later.

amateescu’s picture

Status: Needs work » Closed (duplicate)
Issue tags: -Needs tests