Unverified Commit cebca298 authored by alexpott's avatar alexpott

Issue #2839089 by hchonov, anmolgoyal74, leolando.tan, arunkumark, tstoeckler:...

Issue #2839089 by hchonov, anmolgoyal74, leolando.tan, arunkumark, tstoeckler: $storage->load()->getTranslation('non-default')->getTranslation('default') initializes three entity objects instead of only two
parent b9a310e2
......@@ -846,8 +846,8 @@ public function getTranslation($langcode) {
// 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
......
......@@ -5,6 +5,7 @@
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 @@ public function testTranslationStatus() {
}
}
/**
* 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)));
}
}
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