summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-04-03 10:56:08 (GMT)
committerAlex Pott2018-04-03 10:56:08 (GMT)
commitcebca298efa34e3bcc24e01f14bcd3b3baeabca7 (patch)
treefa2527c2650fbfbb60ce3f80ef48cbac945e3619
parentb9a310e29e208f9fd4467c4481158e0a7d688947 (diff)
Issue #2839089 by hchonov, anmolgoyal74, leolando.tan, arunkumark, tstoeckler: $storage->load()->getTranslation('non-default')->getTranslation('default') initializes three entity objects instead of only two
-rw-r--r--core/lib/Drupal/Core/Entity/ContentEntityBase.php4
-rw-r--r--core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php28
2 files changed, 30 insertions, 2 deletions
diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
index 6b999c6..4e00f27 100644
--- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php
+++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
@@ -846,8 +846,8 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
// Populate entity translation object cache so it will be available for all
// translation objects.
- if ($langcode == $this->activeLangcode) {
- $this->translations[$langcode]['entity'] = $this;
+ if (!isset($this->translations[$this->activeLangcode]['entity'])) {
+ $this->translations[$this->activeLangcode]['entity'] = $this;
}
// If we already have a translation object for the specified language we can
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
index bcd702a..7332057 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityTranslationTest.php
@@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\TranslationStatusInterface;
+use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
@@ -1013,4 +1014,31 @@ class EntityTranslationTest extends EntityLanguageTestBase {
}
}
+ /**
+ * Tests the translation object cache.
+ */
+ public function testTranslationObjectCache() {
+ $default_langcode = $this->langcodes[1];
+ $translation_langcode = $this->langcodes[2];
+
+ $entity = EntityTestMul::create([
+ 'name' => 'test',
+ 'langcode' => $default_langcode,
+ ]);
+ $entity->save();
+ $entity->addTranslation($translation_langcode)->save();
+
+ // Test that the default translation object is put into the translation
+ // object cache when a new translation object is initialized.
+ $entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
+ $default_translation_spl_object_hash = spl_object_hash($entity);
+ $this->assertEquals($default_translation_spl_object_hash, spl_object_hash($entity->getTranslation($translation_langcode)->getTranslation($default_langcode)));
+
+ // Test that non-default translations are always served from the translation
+ // object cache.
+ $entity = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
+ $this->assertEquals(spl_object_hash($entity->getTranslation($translation_langcode)), spl_object_hash($entity->getTranslation($translation_langcode)));
+ $this->assertEquals(spl_object_hash($entity->getTranslation($translation_langcode)), spl_object_hash($entity->getTranslation($translation_langcode)->getTranslation($default_langcode)->getTranslation($translation_langcode)));
+ }
+
}