Unverified Commit 62fb1206 authored by alexpott's avatar alexpott

Issue #2961986 by Sam152, Berdir, tstoeckler: The ContentEntityBase entity key...

Issue #2961986 by Sam152, Berdir, tstoeckler: The ContentEntityBase entity key cache is purged incorrectly when two keys exist for one field
parent 51afa681
......@@ -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]);
......
......@@ -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());
}
/**
......
......@@ -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);
}
}
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Entity\EntityTest;
/**
* Test the behavior of entity keys.
*
* @group entity
*/
class EntityKeysTest extends EntityKernelTestBase {
/**
* Test the cache when multiple keys reference a single field.
*
* @dataProvider multipleKeysCacheTestCases
*/
public function testMultipleKeysCache($translatable) {
$this->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,
],
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment