diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index b4f7e08dfa2b214d31d1144a084d5ad58b5b06d8..5c9fc66f6e03f28517c7abfa1baa5afdf94495dc 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -1271,7 +1271,20 @@ public function renderText($alter) { // @todo Views should expect and store a leading /. See // https://www.drupal.org/node/2423913. - $more_link = ' ' . $this->linkGenerator()->generate($more_link_text, CoreUrl::fromUserInput('/' . $more_link_path, array('attributes' => array('class' => array('views-more-link'))))); + $options = array( + 'attributes' => array( + 'class' => array( + 'views-more-link', + ), + ), + ); + if (UrlHelper::isExternal($more_link_path)) { + $more_link_url = CoreUrl::fromUri($more_link_path, $options); + } + else { + $more_link_url = CoreUrl::fromUserInput('/' . $more_link_path, $options); + } + $more_link = ' ' . $this->linkGenerator()->generate($more_link_text, $more_link_url); } } diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php index 5135c4f003b21287eff44dd82a76ce9bde73c8c0..0b33a58917af55c1de468f83e2d73639aec93234 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php @@ -227,11 +227,17 @@ public function testRenderAsLinkWithoutPath() { } /** - * Test rendering as a link without a path. + * Test rendering with a more link. + * + * @param string $path + * An internal or external path. + * @param string $url + * The final url used by the more link. * + * @dataProvider providerTestRenderTrimmedWithMoreLinkAndPath * @covers ::renderText */ - public function testRenderTrimmedWithMoreLink() { + public function testRenderTrimmedWithMoreLinkAndPath($path, $url) { $alter = [ 'trim' => TRUE, 'max_length' => 7, @@ -239,6 +245,7 @@ public function testRenderTrimmedWithMoreLink() { // Don't invoke translation. 'ellipsis' => FALSE, 'more_link_text' => 'more link', + 'more_link_path' => $path, ]; $this->display->expects($this->any()) @@ -253,11 +260,38 @@ public function testRenderTrimmedWithMoreLink() { $field->field_alias = 'key'; $row = new ResultRow(['key' => 'a long value']); - $expected_result = 'a long <a href="/%3Cfront%3E" class="views-more-link">more link</a>'; + $expected_result = 'a long <a href="' . $url . '" class="views-more-link">more link</a>'; $result = $field->advancedRender($row); $this->assertEquals($expected_result, $result); } + /** + * Data provider for ::testRenderTrimmedWithMoreLinkAndPath(). + * + * @return array + * Test data. + */ + public function providerTestRenderTrimmedWithMoreLinkAndPath() { + $data = []; + // Simple path with default options. + $data[] = ['test-path', '/test-path']; + // Add a fragment. + $data[] = ['test-path#test', '/test-path#test']; + // Query specified as part of the path. + $data[] = ['test-path?foo=bar', '/test-path?foo=bar']; + // Empty path. + $data[] = ['', '/%3Cfront%3E']; + // Front page path. + $data[] = ['<front>', '/%3Cfront%3E']; + + // External URL. + $data[] = ['https://www.drupal.org', 'https://www.drupal.org']; + $data[] = ['http://www.drupal.org', 'http://www.drupal.org']; + $data[] = ['www.drupal.org', '/www.drupal.org']; + + return $data; + } + /** * Tests the "No results text" rendering. *