diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index 9e1896e..aecf0f9 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -107,6 +107,14 @@ */ protected $renderer; + + /** + * Stores the path validator. + * + * @return \Drupal\Core\Path\PathValidatorInterface + */ + protected $path_validator; + /** * Overrides Drupal\views\Plugin\views\HandlerBase::init(). */ @@ -1471,7 +1479,7 @@ protected function renderAsLink($alter, $text, $tokens) { // Check to see if we have a routable path, so path processors run, // otherise use is as-is. if ($i = strpos($path, 'base:') === 0) { - if (!$final_url = \Drupal::pathValidator()->getUrlIfValidWithoutAccessCheck(substr($url['path'], 5))) { + if (!$final_url = $this->getpathValidator()->getUrlIfValidWithoutAccessCheck(substr($url['path'], 5))) { $final_url = CoreUrl::fromUri($path, $options); } else { @@ -1751,6 +1759,19 @@ protected function getRenderer() { return $this->renderer; } + /** + * Returns the path validator.. + * + * @return \Drupal\Core\Path\PathValidatorInterface + */ + protected function getpathValidator() { + if (!isset($this->path_validator)) { + $this->path_validator = \Drupal::service('path.validator'); + } + + return $this->path_validator; + } + } /** 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 4e3bd73..1223db0 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php @@ -143,7 +143,7 @@ protected function setUp() { $route_provider->expects($this->any()) ->method('getRouteByName') ->with('test_route') - ->willReturn(new Route('/test-path')); + ->willReturn(new Route('/test-path/{test}', ['test' => 123])); $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface'); $this->pathValidator = $this->getMock('Drupal\Core\Path\PathValidatorInterface'); @@ -437,8 +437,8 @@ public function providerTestRenderAsLinkWithUrlAndOptions() { * @dataProvider providerTestRenderAsLinkWithPathAndTokens * @covers ::renderAsLink */ - public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) { - $alter = [ + public function testRenderAsLinkWithPathAndTokens($path, $alter, $tokens, $link_html) { + $alter += [ 'make_link' => TRUE, 'path' => $path, ]; @@ -461,6 +461,21 @@ public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) { ->with($build, FALSE) ->willReturn('base:test-path/123'); + $url = Url::fromRoute('test_route', ['test' => 123]); + $url->setUrlGenerator($this->urlGenerator); + + $prefix = !empty($alter['language']) ? '/' . $alter['language']->getId() : ''; + + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with($url->getRouteName(), $url->getRouteParameters()) + ->willReturn($prefix . '/test-path/123'); + + $this->pathValidator->expects($this->at(1)) + ->method('getUrlIfValidWithoutAccessCheck') + ->with('test-path/123') + ->willReturn($url); + $result = $field->advancedRender($row); $this->assertEquals($link_html, $result); } @@ -477,15 +492,19 @@ public function providerTestRenderAsLinkWithPathAndTokens() { $data = []; - $data[] = ['test-path/{{foo}}', $tokens, $link_html]; - $data[] = ['test-path/{{ foo}}', $tokens, $link_html]; - $data[] = ['test-path/{{ foo}}', $tokens, $link_html]; - $data[] = ['test-path/{{foo }}', $tokens, $link_html]; - $data[] = ['test-path/{{foo }}', $tokens, $link_html]; - $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; - $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; - $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; - $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; + $data[] = ['test-path/{{foo}}', [], $tokens, $link_html]; + $data[] = ['test-path/{{ foo}}', [], $tokens, $link_html]; + $data[] = ['test-path/{{ foo}}', [], $tokens, $link_html]; + $data[] = ['test-path/{{foo }}', [], $tokens, $link_html]; + $data[] = ['test-path/{{foo }}', [], $tokens, $link_html]; + $data[] = ['test-path/{{ foo }}', [], $tokens, $link_html]; + $data[] = ['test-path/{{ foo }}', [], $tokens, $link_html]; + $data[] = ['test-path/{{ foo }}', [], $tokens, $link_html]; + $data[] = ['test-path/{{ foo }}', [], $tokens, $link_html]; + + $language = new Language(['id' => 'fr']); + $options = ['language' => $language]; + $data[] = ['test-path/{{foo}}', $options, $tokens, 'value']; return $data; }