diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 4e00f27b20231f29a69899a255e665d627e71e36..ff59d92a1ab6645450e8e176f4c2ac10b4555848 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -772,11 +772,11 @@ protected function updateFieldLangcodes($langcode) { * {@inheritdoc} */ public function onChange($name) { - // Check if the changed name is the value of an entity key and if the value - // of that is currently cached, if so, reset it. Exclude the bundle from - // that check, as it ready only and must not change, unsetting it could + // Check if the changed name is the value of any entity keys and if any of + // those values are currently cached, if so, reset it. Exclude the bundle + // from that check, as it ready only and must not change, unsetting it could // lead to recursions. - if ($key = array_search($name, $this->getEntityType()->getKeys())) { + foreach (array_keys($this->getEntityType()->getKeys(), $name, TRUE) as $key) { if ($key != 'bundle') { if (isset($this->entityKeys[$key])) { unset($this->entityKeys[$key]); diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 8b4f2ba82726de9d9222e162220bb9db7f859b45..d7dc6351467c4b1a780abaa8a05a767d049f89f6 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -101,6 +101,9 @@ function entity_test_entity_type_alter(array &$entity_types) { if (!$state->get('entity_test_new')) { unset($entity_types['entity_test_new']); } + + $entity_test_definition = $entity_types['entity_test']; + $entity_test_definition->set('entity_keys', $state->get('entity_test.entity_keys', []) + $entity_test_definition->getKeys()); } /** diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php index 9f8c5b052167c7d36c77d3219b254471987db833..771d81a9adcc487018e7c06934b908af6be9306c 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php @@ -109,7 +109,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ], ]); - return $fields; + return $fields + \Drupal::state()->get($entity_type->id() . '.additional_base_field_definitions', []); } /** @@ -164,4 +164,15 @@ public function getName() { return $this->get('name')->value; } + /** + * {@inheritdoc} + */ + public function getEntityKey($key) { + // Typically this protected method is used internally by entity classes and + // exposed publicly through more specific getter methods. So that test cases + // are able to set and access entity keys dynamically, update the visibility + // of this method to public. + return parent::getEntityKey($key); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityKeysTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityKeysTest.php new file mode 100644 index 0000000000000000000000000000000000000000..834f04cc27bebb1bbd2c38f00a18de863f106064 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityKeysTest.php @@ -0,0 +1,56 @@ +state->set('entity_test.additional_base_field_definitions', [ + 'test_field' => BaseFieldDefinition::create('string')->setTranslatable($translatable), + ]); + $this->state->set('entity_test.entity_keys', [ + 'key_1' => 'test_field', + 'key_2' => 'test_field', + ]); + drupal_flush_all_caches(); + $this->installEntitySchema('entity_test'); + + $entity = EntityTest::create([]); + + $entity->set('test_field', 'foo'); + $this->assertEquals('foo', $entity->getEntityKey('key_1')); + $this->assertEquals('foo', $entity->getEntityKey('key_2')); + + $entity->set('test_field', 'bar'); + $this->assertEquals('bar', $entity->getEntityKey('key_1')); + $this->assertEquals('bar', $entity->getEntityKey('key_2')); + } + + /** + * Data provider for ::testMultipleKeysCache. + */ + public function multipleKeysCacheTestCases() { + return [ + 'translatable Entity Key' => [ + TRUE, + ], + 'Non-translatable entity key' => [ + FALSE, + ], + ]; + } + +}