diff --git a/core/lib/Drupal/Core/Utility/LinkGenerator.php b/core/lib/Drupal/Core/Utility/LinkGenerator.php index 70af473..a6b35b4 100644 --- a/core/lib/Drupal/Core/Utility/LinkGenerator.php +++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php @@ -151,15 +151,15 @@ public function generate($text, Url $url) { // them. Include a placeholder for the href. $attributes = array('href' => '') + $variables['options']['attributes']; unset($variables['options']['attributes']); - $url->setOptions($variables['options']); + $variables['url']->setOptions($variables['options']); // External URLs can not have cacheable metadata. - if ($url->isExternal()) { + if ($variables['url']->isExternal()) { $generated_link = new GeneratedLink(); - $attributes['href'] = $url->toString(FALSE); + $attributes['href'] = $variables['url']->toString(FALSE); } else { - $generated_url = $url->toString(TRUE); + $generated_url = $variables['url']->toString(TRUE); $generated_link = GeneratedLink::createFromObject($generated_url); // The result of the URL generator is a plain-text URL to use as the href // attribute, and it is escaped by \Drupal\Core\Template\Attribute. diff --git a/core/modules/system/src/Tests/Utility/LinkGenerationTest.php b/core/modules/system/src/Tests/Utility/LinkGenerationTest.php index 62c4d23..52dd584 100644 --- a/core/modules/system/src/Tests/Utility/LinkGenerationTest.php +++ b/core/modules/system/src/Tests/Utility/LinkGenerationTest.php @@ -65,6 +65,14 @@ function testHookLinkAlter() { // Ensure the content of the link is escaped. $this->assertEscaped('link with markup'); $this->assertRaw('Test!'); + + // Test altering the url alteration. + \Drupal::state()->set('link_generation_test_link_alter_url', TRUE); + $link = $renderer->executeInRenderContext(new RenderContext(), function () use ($url) { + return \Drupal::l(['#markup' => 'link with markup'], $url); + }); + $this->setRawContent($link); + $this->assertLinkByHref('http://other-example.com'); } } diff --git a/core/modules/system/tests/modules/link_generation_test/link_generation_test.module b/core/modules/system/tests/modules/link_generation_test/link_generation_test.module index 622a403..8210d4a 100644 --- a/core/modules/system/tests/modules/link_generation_test/link_generation_test.module +++ b/core/modules/system/tests/modules/link_generation_test/link_generation_test.module @@ -18,4 +18,8 @@ function link_generation_test_link_alter(&$variables) { $variables['text'] .= ' Test!'; } } + if (\Drupal::state()->get('link_generation_test_link_alter_url', FALSE)) { + $new_url = \Drupal\Core\Url::fromUri('http://other-example.com'); + $variables['url'] = $new_url; + } }