diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 960bb7da84b1fb2805187e18be9a5ed52c79e863..d810a24c78a36ae8c1f478686c92dcd5160f237c 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 b6c4fd03777d92d4c2279c9cd33bf8f146a88b87..a2ba387c3bc7cb7dc3da45752ec34ff1b237838c 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);