From 7d1f29faf4591d6de82132a0d3cca7f8af0f9716 Mon Sep 17 00:00:00 2001
From: Francesco Placella <plach@183211.no-reply.drupal.org>
Date: Mon, 25 Mar 2019 10:45:52 +0100
Subject: [PATCH] Issue #3042993 by amateescu, tedbow: Translatable and
 revisonable installed entity type definitions are missing the
 'revision_translation_affected' entity key

(cherry picked from commit a54e207f611d203d08992bed0c466c01c1954ec4)
---
 core/modules/system/system.install            | 32 +++++++++++++++++++
 ...dateAddRevisionTranslationAffectedTest.php |  5 +++
 2 files changed, 37 insertions(+)

diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index 960bb7da84b1..d810a24c78a3 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -2247,3 +2247,35 @@ function system_update_8701() {
   // The system.theme.data key is no longer used in Drupal 8.7.x.
   \Drupal::state()->delete('system.theme.data');
 }
+
+/**
+ * Add the 'revision_translation_affected' entity key.
+ */
+function system_update_8702() {
+  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
+
+  // Get a list of revisionable and translatable entity types.
+  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $last_installed_definitions */
+  $last_installed_definitions = array_filter($entity_definition_update_manager->getEntityTypes(), function (EntityTypeInterface $entity_type) {
+    return $entity_type->isRevisionable() && $entity_type->isTranslatable();
+  });
+
+  // Ensure that we don't use the cached in-code definitions to support sites
+  // that might be updating from 8.3.x straight to 8.7.x.
+  \Drupal::entityTypeManager()->useCaches(FALSE);
+  $live_definitions = \Drupal::entityTypeManager()->getDefinitions();
+
+  // Update the 'revision_translation_affected' entity key of the last installed
+  // definitions to use the value of the live (in-code) entity type definitions
+  // in cases when the key has not been populated yet.
+  foreach ($last_installed_definitions as $entity_type_id => $entity_type) {
+    $revision_translation_affected_key = $live_definitions[$entity_type_id]->getKey('revision_translation_affected');
+    if (!$entity_type->hasKey('revision_translation_affected') && !empty($revision_translation_affected_key) && $entity_definition_update_manager->getFieldStorageDefinition($revision_translation_affected_key, $entity_type_id)) {
+      $entity_keys = $entity_type->getKeys();
+      $entity_keys['revision_translation_affected'] = $revision_translation_affected_key;
+      $entity_type->set('entity_keys', $entity_keys);
+      $entity_definition_update_manager->updateEntityType($entity_type);
+    }
+  }
+  \Drupal::entityTypeManager()->useCaches(TRUE);
+}
diff --git a/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionTranslationAffectedTest.php b/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionTranslationAffectedTest.php
index b6c4fd03777d..a2ba387c3bc7 100644
--- a/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionTranslationAffectedTest.php
+++ b/core/modules/system/tests/src/Functional/Update/EntityUpdateAddRevisionTranslationAffectedTest.php
@@ -56,6 +56,7 @@ protected function setDatabaseDumpFiles() {
    * Tests the addition of the 'revision_translation_affected' base field.
    *
    * @see system_update_8402()
+   * @see system_update_8702()
    */
   public function testAddingTheRevisionTranslationAffectedField() {
     // Make the entity type revisionable and translatable prior to running the
@@ -74,6 +75,10 @@ public function testAddingTheRevisionTranslationAffectedField() {
     $field_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions('entity_test_update');
     $this->assertTrue(isset($field_storage_definitions['revision_translation_affected']));
 
+    // Check that the entity type has the 'revision_translation_affected' key.
+    $entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('entity_test_update');
+    $this->assertEquals('revision_translation_affected', $entity_type->getKey('revision_translation_affected'));
+
     // Check that the correct initial value was set when the field was
     // installed.
     $entity = \Drupal::entityTypeManager()->getStorage('entity_test_update')->load(1);
-- 
GitLab