diff --git a/core/lib/Drupal/Core/Entity/EntityRenderController.php b/core/lib/Drupal/Core/Entity/EntityRenderController.php index 9d68684..c6b58e2 100644 --- a/core/lib/Drupal/Core/Entity/EntityRenderController.php +++ b/core/lib/Drupal/Core/Entity/EntityRenderController.php @@ -167,7 +167,8 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la $view_modes = array(); $displays = array(); $context = array('langcode' => $langcode); - foreach ($entities as $entity) { + foreach ($entities as &$entity) { + $entity = $entity->getExistingTranslation($langcode); $bundle = $entity->bundle(); // Allow modules to change the view mode. diff --git a/core/modules/node/lib/Drupal/node/Controller/NodeController.php b/core/modules/node/lib/Drupal/node/Controller/NodeController.php index c889adc..8b0c247 100644 --- a/core/modules/node/lib/Drupal/node/Controller/NodeController.php +++ b/core/modules/node/lib/Drupal/node/Controller/NodeController.php @@ -126,7 +126,7 @@ public function page(NodeInterface $node) { * The page title. */ public function pageTitle(NodeInterface $node) { - return String::checkPlain($node->label()); + return String::checkPlain($node->getExistingTranslation()->label()); } /** diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php index 45e42ab..baa8d23 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php @@ -188,4 +188,47 @@ public function testDisabledBundle() { $this->assertEqual($enabledNode->id(), reset($rows)->entity_id); } + /** + * Tests that translations are rendered properly. + */ + function testTranslationRendering() { + $default_langcode = $this->langcodes[0]; + $values[$default_langcode] = $this->getNewEntityValues($default_langcode); + $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode); + $node = \Drupal::entityManager()->getStorageController($this->entityType)->load($this->entityId); + $node->setPromoted(TRUE); + + // Create translations. + foreach (array_diff($this->langcodes, array($default_langcode)) as $langcode) { + $values[$langcode] = $this->getNewEntityValues($langcode); + $translation = $node->addTranslation($langcode, $values[$langcode]); + $translation->setPromoted(TRUE); + } + $node->save(); + + // Test that the frontpage view displays the correct translations. + \Drupal::moduleHandler()->install(array('views'), TRUE); + $this->rebuildContainer(); + $this->doTestTranslations('node', $values); + + // Test that the node page displays the correct translations. + $this->doTestTranslations('node/' . $node->id(), $values); + } + + /** + * Tests that the given path dsiplays the correct translation values. + * + * @param string $path + * The path to be tested. + * @param array $values + * The translation values to be found. + */ + protected function doTestTranslations($path, array $values) { + $languages = language_list(); + foreach ($this->langcodes as $langcode) { + $this->drupalGet($path, array('language' => $languages[$langcode])); + $this->assertText($values[$langcode]['title'], format_string('The %langcode node translation is correctly displayed.', array('%langcode' => $langcode))); + } + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php index 31201ff..2290b69 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityTranslationTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Language\Language; use Drupal\Core\TypedData\TranslatableInterface; use Drupal\entity_test\Entity\EntityTestMulRev; +use Drupal\Component\Utility\MapArray; /** * Tests entity translation. @@ -498,19 +499,22 @@ function testEntityTranslationAPI() { * Tests language fallback applied to field and entity translations. */ function testLanguageFallback() { - $default_langcode = $this->langcodes[0]; - $langcode = $this->langcodes[1]; - $langcode2 = $this->langcodes[2]; + $current_langcode = $this->container->get('language_manager')->getLanguage(Language::TYPE_CONTENT)->id; + $this->langcodes[] = $current_langcode; $values = array(); - for ($i = 0; $i < 3; $i++) { - $values[$this->langcodes[$i]]['name'] = $this->randomName(); - $values[$this->langcodes[$i]]['user_id'] = mt_rand(0, 127); + foreach ($this->langcodes as $langcode) { + $values[$langcode]['name'] = $this->randomName(); + $values[$langcode]['user_id'] = mt_rand(0, 127); } - $entity = $this->entityManager - ->getStorageController('entity_test_mul') - ->create(array('langcode' => $default_langcode) + $values[$default_langcode]); + $default_langcode = $this->langcodes[0]; + $langcode = $this->langcodes[1]; + $langcode2 = $this->langcodes[2]; + + $entity_type = 'entity_test_mul'; + $controller = $this->entityManager->getStorageController($entity_type); + $entity = $controller->create(array('langcode' => $default_langcode) + $values[$default_langcode]); $entity->save(); $entity->addTranslation($langcode, $values[$langcode]); @@ -531,17 +535,27 @@ function testLanguageFallback() { // Check that the current translation is properly returned. $translation = $entity->getExistingTranslation(); $this->assertEqual($langcode, $translation->language()->id, 'The current translation language matches the topmost language fallback candidate.'); - $current_langcode = $this->container->get('language_manager')->getLanguage(Language::TYPE_CONTENT)->id; - $entity->addTranslation($current_langcode); + $entity->addTranslation($current_langcode, $values[$current_langcode]); $translation = $entity->getExistingTranslation(); $this->assertEqual($current_langcode, $translation->language()->id, 'The current translation language matches the current language.'); // Check that if the entity has no translation no fallback is applied. - $entity = $this->entityManager - ->getStorageController('entity_test_mul') - ->create(array('langcode' => $default_langcode)); - $translation = $entity->getExistingTranslation($default_langcode); - $this->assertIdentical($entity, $translation, 'When the entity has no translation no fallback is applied.'); + $entity2 = $controller->create(array('langcode' => $default_langcode)); + $translation = $entity2->getExistingTranslation($default_langcode); + $this->assertIdentical($entity2, $translation, 'When the entity has no translation no fallback is applied.'); + + // Checks that entity translations are rendered properly. + $controller = $this->entityManager->getRenderController($entity_type); + $build = $controller->view($entity); + $this->assertEqual($build['label']['#markup'], $values[$current_langcode]['name'], 'By default the entity is rendered in the current language.'); + $langcodes = MapArray::copyValuesToKeys($this->langcodes); + // We have no translation for the $langcode2 langauge, hence the expected + // result is the topmost existing translation, that is $langcode. + $langcodes[$langcode2] = $langcode; + foreach ($langcodes as $desired => $expected) { + $build = $controller->view($entity, 'full', $desired); + $this->assertEqual($build['label']['#markup'], $values[$expected]['name'], 'The entity is rendered in the expected language.'); + } } /** @@ -555,23 +569,23 @@ function testFieldDefinitions() { $this->entityManager->clearCachedFieldDefinitions(); $definitions = $this->entityManager->getFieldDefinitions($entity_type); $this->assertFalse($definitions['name']['translatable'], 'Field translatability can be disabled programmatically.'); - + $this->state->set('entity_test.field_definitions.translatable', array('name' => TRUE)); $this->entityManager->clearCachedFieldDefinitions(); $definitions = $this->entityManager->getFieldDefinitions($entity_type); $this->assertTrue($definitions['name']['translatable'], 'Field translatability can be enabled programmatically.'); - + // Check that field translatability is disabled by default. $base_field_definitions = EntityTestMulRev::baseFieldDefinitions($entity_type); $this->assertTrue(!isset($base_field_definitions['id']['translatable']), 'Translatability for the id field is not defined.'); $this->assertFalse($definitions['id']['translatable'], 'Field translatability is disabled by default.'); - + // Check that entity ids and langcode fields cannot be translatable. foreach (array('id', 'uuid', 'revision_id', 'type', 'langcode') as $name) { $this->state->set('entity_test.field_definitions.translatable', array($name => TRUE)); $this->entityManager->clearCachedFieldDefinitions(); $message = format_string('Field %field cannot be translatable.', array('%field' => $name)); - + try { $definitions = $this->entityManager->getFieldDefinitions($entity_type); $this->fail($message); diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php index 392b564..62312a5 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php @@ -92,8 +92,11 @@ protected function init() { /** * Overrides Drupal\entity\Entity::label(). */ - public function label($langcode = Language::LANGCODE_DEFAULT) { + public function label($langcode = NULL) { $info = $this->entityInfo(); + if (!isset($langcode)) { + $langcode = $this->activeLangcode; + } if (isset($info['entity_keys']['label']) && $info['entity_keys']['label'] == 'name') { return $this->getTranslation($langcode)->name->value; } diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMul.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMul.php index 76db7ab..8dfca6f 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMul.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTestMul.php @@ -20,6 +20,7 @@ * module = "entity_test", * controllers = { * "storage" = "Drupal\entity_test\EntityTestStorageController", + * "render" = "Drupal\entity_test\EntityTestRenderController", * "access" = "Drupal\entity_test\EntityTestAccessController", * "form" = { * "default" = "Drupal\entity_test\EntityTestFormController"