From 0a8e34cf15f237c0672dd6ea7776d46393467ce1 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sat, 29 Mar 2014 12:40:21 +0100 Subject: [PATCH] =?UTF-8?q?Issue=20#2149649=20by=20G=C3=A1bor=20Hojtsy,=20?= =?UTF-8?q?YesCT,=20Xano,=20herom,=20vijaycs85,=20Sweetchuck,=20plach:=20E?= =?UTF-8?q?ntity=20rendering/theming=20does=20not=20use=20the=20active=20e?= =?UTF-8?q?ntity=20language=20to=20render=20links.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/modules/comment/comment.module | 1 + .../Drupal/locale/Tests/LocalePathTest.php | 8 ++-- .../node/lib/Drupal/node/NodeViewBuilder.php | 3 +- .../node/Tests/NodeAccessBaseTableTest.php | 1 + .../node/Tests/NodeTranslationUITest.php | 47 +++++++++++++++++++ core/modules/node/node.module | 4 +- 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 9ee58fc350e3..e35350123845 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -448,6 +448,7 @@ function comment_node_links_alter(array &$node_links, NodeInterface $node, array if (user_access('post comments')) { $links['comment-add'] = array( 'title' => t('Add new comment'), + 'language' => $node->language(), 'attributes' => array('title' => t('Add a new comment to this page.')), 'fragment' => 'comment-form', ); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php index 62aba310e380..4466a91a7d6d 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php @@ -117,14 +117,14 @@ function testPathLanguageConfiguration() { $this->container->get('path.crud')->delete($edit); // Create language nodes to check priority of aliases. - $first_node = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1)); - $second_node = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1)); + $first_node = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1, 'langcode' => 'en')); + $second_node = $this->drupalCreateNode(array('type' => 'page', 'promote' => 1, 'langcode' => Language::LANGCODE_NOT_SPECIFIED)); // Assign a custom path alias to the first node with the English language. $edit = array( 'source' => 'node/' . $first_node->id(), 'alias' => $custom_path, - 'langcode' => 'en', + 'langcode' => $first_node->language()->id, ); $this->container->get('path.crud')->save($edit['source'], $edit['alias'], $edit['langcode']); @@ -132,7 +132,7 @@ function testPathLanguageConfiguration() { $edit = array( 'source' => 'node/' . $second_node->id(), 'alias' => $custom_path, - 'langcode' => Language::LANGCODE_NOT_SPECIFIED, + 'langcode' => $second_node->language()->id, ); $this->container->get('path.crud')->save($edit['source'], $edit['alias'], $edit['langcode']); diff --git a/core/modules/node/lib/Drupal/node/NodeViewBuilder.php b/core/modules/node/lib/Drupal/node/NodeViewBuilder.php index 48fb0ffcf148..34ca5deac925 100644 --- a/core/modules/node/lib/Drupal/node/NodeViewBuilder.php +++ b/core/modules/node/lib/Drupal/node/NodeViewBuilder.php @@ -95,7 +95,7 @@ public static function renderLinks(array $context) { ); if (!$context['in_preview']) { - $entity = entity_load('node', $context['node_entity_id']); + $entity = entity_load('node', $context['node_entity_id'])->getTranslation($context['langcode']); $links['node'] = self::buildLinks($entity, $context['view_mode']); // Allow other modules to alter the node links. @@ -132,6 +132,7 @@ protected static function buildLinks(NodeInterface $entity, $view_mode) { '@title' => $node_title_stripped, )), 'href' => 'node/' . $entity->id(), + 'language' => $entity->language(), 'html' => TRUE, 'attributes' => array( 'rel' => 'tag', diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php index e78b5bc58e4b..f227792dee29 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php @@ -154,6 +154,7 @@ protected function assertTaxonomyPage($is_admin) { $this->drupalGet("taxonomy/term/$tid"); $this->nids_visible = array(); foreach ($this->xpath("//a[text()='Read more']") as $link) { + // See also testTranslationRendering() in NodeTranslationUITest. $this->assertTrue(preg_match('|node/(\d+)$|', (string) $link['href'], $matches), 'Read more points to a node'); $this->nids_visible[$matches[1]] = TRUE; } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php index 1a15c55cff8c..afc0c570710f 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php @@ -271,6 +271,53 @@ function testTranslationRendering() { $this->assertText($values[$langcode]['title'][0]['value']); } + // Need to check from the beginning, including the base_path, in the url + // since the pattern for the default language might be a substring of + // the strings for other languages. + $base_path = base_path(); + + // Check the frontpage for 'Read more' links to each translation. + // See also assertTaxonomyPage() in NodeAccessBaseTableTest. + $node_href = 'node/' . $node->id(); + foreach ($this->langcodes as $langcode) { + $num_match_found = 0; + if ($langcode == 'en') { + // Site default language does not have langcode prefix in the URL. + $expected_href = $base_path . $node_href; + } + else { + $expected_href = $base_path . $langcode . '/' . $node_href; + } + $pattern = '|^' . $expected_href . '$|'; + foreach ($this->xpath("//a[text()='Read more']") as $link) { + if (preg_match($pattern, (string) $link['href'], $matches) == TRUE) { + $num_match_found++; + } + } + $this->assertTrue($num_match_found == 1, 'There is 1 Read more link, ' . $expected_href . ', for the ' . $langcode . ' translation of a node on the frontpage. (Found ' . $num_match_found . '.)'); + } + + // Check the frontpage for 'Add new comment' links that include the + // language. + $comment_form_href = 'node/' . $node->id() . '#comment-form'; + foreach ($this->langcodes as $langcode) { + $num_match_found = 0; + if ($langcode == 'en') { + // Site default language does not have langcode prefix in the URL. + $expected_href = $base_path . $comment_form_href; + } + else { + $expected_href = $base_path . $langcode . '/' . $comment_form_href; + } + $pattern = '|^' . $expected_href . '$|'; + foreach ($this->xpath("//a[text()='Add new comment']") as $link) { + if (preg_match($pattern, (string) $link['href'], $matches) == TRUE) { + $num_match_found++; + } + } + $this->assertTrue($num_match_found == 1, 'There is 1 Add new comment link, ' . $expected_href . ', for the ' . $langcode . ' translation of a node on the frontpage. (Found ' . $num_match_found . '.)'); + } + // Test that the node page displays the correct translations. $this->doTestTranslations('node/' . $node->id(), $values); } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 63c2174dbc19..66145b02c7fa 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -644,7 +644,9 @@ function template_preprocess_node(&$variables) { ); $variables['name'] = drupal_render($username); - $variables['node_url'] = $node->url(); + $variables['node_url'] = $node->url('canonical', array( + 'language' => $node->language(), + )); $variables['label'] = $variables['elements']['title']; unset($variables['elements']['title']); $variables['page'] = $variables['view_mode'] == 'full' && node_is_page($node); -- GitLab