Fixing Invalid Translations for Drupal 8 Content Moderation

I recently migrated content from a Drupal 7 site to a new Drupal 8 install using core’s Migrate, Migrate Drupal and Migrate Drupal UI modules. A few months after the initial migration, I decided to enable core Content Moderation for use with one of my migrated content types. No beuno.
Just saving any content for which I had enabled Content Moderation resulted in this tasteless error:

<br />
The website encountered an unexpected error. Please try again later.<br />

Clicking over to Recent log messages at /admin/reports/dblog revealed the following exception:
<br />
Invalid translation language (und) specified.<br />

It was being thrown from two different places when I attempted to save a node.

The details for each in order were:

<br />
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).<br />

<br />
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).<br />

Several quick searches didn’t turn up anything exactly applicable, but I did find a pretty useful summary of a similar issue that put me on the right track.
Basically, my content migrated from a Drupal 7 site was set with a legacy langcode of und. If I just load a node and output its language attribute using the Devel module’s dpm() function, you can see what’s amiss:
<br />
use Drupal\node\Entity\Node;</p>
<p>$node = Node::load(<My NID>);<br />
dpm($node->language()->getId()); // returns 'und', which was causing a problem<br />

It’s the language specification of und that was preventing me from saving the content with Content Moderation enabled. Changing the language over to the site default, EN in my case, just took a minute with the following code.
<br />
use Drupal\node\Entity\Node;</p>
<p>$node = Node::load(<My NID>);<br />
$langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();<br />
$node->set('langcode', $langcode);<br />
$node->save();<br />
dpm($node->language()->getId()); // now this returns 'en', which is good!<br />

Now you can see that the language attribute is reflecting an appropriate value, which in my case meant I was able to save the node like normal with Content Moderation enabled. Fixed!
All that remained was doing the same operation for every node in the database. For my project there were only a couple of thousand nodes, so I just used the code below (I ran this at devel/php but you could do it in a module or wherever is easiest).
<br />
use Drupal\node\Entity\Node;<br />
$langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();</p>
<p>$result = db_query("SELECT nid FROM node WHERE langcode = 'und' LIMIT 100");<br />
foreach ($result as $record) {<br />
  $node = Node::load($record->nid);<br />
  $node->set('langcode', $langcode);<br />
  $node->save();<br />
}<br />

I limited my query to 100 nodes to avoid a timeout, then just executed the code several times until all my nodes were updated. If you have tons of nodes that need updating, you might consider writing a hook_cron or using batch operations.
Warning: The site I was working on wasn't multilingual. The code above sets every node with a language specification of und to the site default, which may not be what you want for a multilingual site.
That’s it! It seems simpler in retrospect but took me awhile to understand what was going on. I also created an issue on Drupal.org so hopefully this bug will get addressed eventually.

Code Drupal Drupal 8 Drupal Planet

Read This Next