From 42083a1cc7b6ece9e20249972e47cbabf2759c0c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sun, 8 Feb 2015 00:02:31 +0000 Subject: [PATCH] Issue #2414721 by jesperjb, plach: EntityAdapter should be instantiated per language --- .../Drupal/Core/Entity/ContentEntityBase.php | 4 +-- .../Tests/Entity/EntityTranslationTest.php | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index a767326a35..62c464dcd0 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -650,9 +650,9 @@ protected function initializeTranslation($langcode) { $translation->translations = &$this->translations; $translation->enforceIsNew = &$this->enforceIsNew; $translation->translationInitialize = FALSE; - // The label is the only entity key that can change based on the language, - // so unset that in case it is currently set. + // Reset language-dependent properties. unset($translation->entityKeys['label']); + $translation->typedData = NULL; return $translation; } diff --git a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php index 2e8a3b32b3..65bb74002d 100644 --- a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Entity; +use Drupal\Component\Utility\String; use Drupal\Core\Language\LanguageInterface; use Drupal\entity_test\Entity\EntityTestMulRev; use Drupal\language\Entity\ConfigurableLanguage; @@ -665,4 +666,28 @@ protected function doTestLanguageChange($entity_type) { } } + /** + * Tests how entity adapters work with translations. + */ + function testEntityAdapter() { + $entity_type = 'entity_test'; + $default_langcode = 'en'; + $values[$default_langcode] = array('name' => $this->randomString()); + $controller = $this->entityManager->getStorage($entity_type); + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $controller->create($values[$default_langcode]); + + foreach ($this->langcodes as $langcode) { + $values[$langcode] = array('name' => $this->randomString()); + $entity->addTranslation($langcode, $values[$langcode]); + } + + $langcodes = array_merge(array($default_langcode), $this->langcodes); + foreach ($langcodes as $langcode) { + $adapter = $entity->getTranslation($langcode)->getTypedData(); + $name = $adapter->get('name')->value; + $this->assertEqual($name, $values[$langcode]['name'], String::format('Name correctly retrieved from "@langcode" adapter', array('@langcode' => $langcode))); + } + } + } -- GitLab