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
b9a310e29e
commit
cebca298ef
|
@ -846,8 +846,8 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
|
|
||||||
// Populate entity translation object cache so it will be available for all
|
// Populate entity translation object cache so it will be available for all
|
||||||
// translation objects.
|
// translation objects.
|
||||||
if ($langcode == $this->activeLangcode) {
|
if (!isset($this->translations[$this->activeLangcode]['entity'])) {
|
||||||
$this->translations[$langcode]['entity'] = $this;
|
$this->translations[$this->activeLangcode]['entity'] = $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we already have a translation object for the specified language we can
|
// If we already have a translation object for the specified language we can
|
||||||
|
|
|
@ -5,6 +5,7 @@ namespace Drupal\KernelTests\Core\Entity;
|
||||||
use Drupal\Component\Utility\SafeMarkup;
|
use Drupal\Component\Utility\SafeMarkup;
|
||||||
use Drupal\Core\Language\LanguageInterface;
|
use Drupal\Core\Language\LanguageInterface;
|
||||||
use Drupal\Core\TypedData\TranslationStatusInterface;
|
use Drupal\Core\TypedData\TranslationStatusInterface;
|
||||||
|
use Drupal\entity_test\Entity\EntityTestMul;
|
||||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
use Drupal\entity_test\Entity\EntityTestMulRev;
|
||||||
use Drupal\field\Entity\FieldConfig;
|
use Drupal\field\Entity\FieldConfig;
|
||||||
use Drupal\field\Entity\FieldStorageConfig;
|
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)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue