From f589b8800a41fe06cb467a39bfbf6210820fc8cd Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Sat, 5 Aug 2017 10:29:45 -0500
Subject: [PATCH] Issue #2856917 by amateescu, kriboogh: Moderating an entity
 that uses a special language (e.g. LANGCODE_NOT_SPECIFIED) throws exception

---
 .../src/EntityOperations.php                  |  3 +++
 .../src/Kernel/ContentModerationStateTest.php | 23 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php
index f192408b09cd..8ddbbe5bab0a 100644
--- a/core/modules/content_moderation/src/EntityOperations.php
+++ b/core/modules/content_moderation/src/EntityOperations.php
@@ -179,6 +179,9 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
       $content_moderation_state = $storage->create([
         'content_entity_type_id' => $entity_type_id,
         'content_entity_id' => $entity_id,
+        // Make sure that the moderation state entity has the same language code
+        // as the moderated entity.
+        'langcode' => $entity->language()->getId(),
       ]);
       $content_moderation_state->workflow->target_id = $workflow->id();
     }
diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
index 55173dd27044..713e5b51684e 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
@@ -5,7 +5,9 @@
 use Drupal\content_moderation\Entity\ContentModerationState;
 use Drupal\Core\Entity\EntityPublishedInterface;
 use Drupal\Core\Entity\EntityStorageException;
+use Drupal\Core\Language\LanguageInterface;
 use Drupal\entity_test\Entity\EntityTestBundle;
+use Drupal\entity_test\Entity\EntityTestRev;
 use Drupal\entity_test\Entity\EntityTestWithBundle;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\KernelTests\KernelTestBase;
@@ -327,6 +329,27 @@ public function testMultilingualModeration() {
     $this->assertEquals(6, $english_node->getRevisionId());
   }
 
+  /**
+   * Tests that entities with special languages can be moderated.
+   */
+  public function testModerationWithSpecialLanguages() {
+    $workflow = Workflow::load('editorial');
+    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
+    $workflow->save();
+
+    // Create a test entity.
+    $entity = EntityTestRev::create([
+      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+    ]);
+    $entity->save();
+    $this->assertEquals('draft', $entity->moderation_state->value);
+
+    $entity->moderation_state->value = 'published';
+    $entity->save();
+
+    $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value);
+  }
+
   /**
    * Tests that a non-translatable entity type with a langcode can be moderated.
    */
-- 
GitLab