Issue #2839089 by hchonov, anmolgoyal74, leolando.tan, arunkumark, tstoeckler: $storage->load()->getTranslation('non-default')->getTranslation('default') initializes three entity objects instead of only two

merge-requests/1654/head
Alex Pott 2018-04-03 12:56:08 +02:00
parent b9a310e29e
commit cebca298ef
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 30 additions and 2 deletions

View File

@ -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

View File

@ -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)));
}
}