diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 3ebe01b36a997b67d554f0bac2b24d172d1192a6..8270c66d622167b9632030bcaff489f9838f8244 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -8,6 +8,7 @@ use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Render\AttachmentsInterface; use Drupal\Core\Render\BubbleableMetadata; +use Drupal\Core\Render\Markup; use Drupal\Core\Render\RenderableInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\UrlGeneratorInterface; @@ -273,6 +274,11 @@ public function getLink($text, $url, $attributes = []) { } $url->setOption('attributes', $attributes); } + // The text has been processed by twig already, convert it to a safe object + // for the render system. + if ($text instanceof \Twig_Markup) { + $text = Markup::create($text); + } $build = [ '#type' => 'link', '#title' => $text, diff --git a/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php index cab7e7ce7e30c8dd1fa3b3e839a1e2ed83a173b2..83aa4f2e0480b26c21636a8aa3256a71f7cc9bba 100644 --- a/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php +++ b/core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php @@ -31,7 +31,7 @@ interface LinkGeneratorInterface { * This keeps the context of the link title ('settings' in the example) for * translators. * - * @param string|array $text + * @param string|array|\Drupal\Component\Render\MarkupInterface $text * The link text for the anchor tag as a translated string or render array. * Strings will be sanitized automatically. If you need to output HTML in * the link text, use a render array or an already sanitized string such as diff --git a/core/modules/system/src/Tests/Theme/EngineTwigTest.php b/core/modules/system/src/Tests/Theme/EngineTwigTest.php index ae08fc83527b5bb455d95e2b9441fa8f0c56d425..a79c01dd630235c78229786a1256d0016736efbf 100644 --- a/core/modules/system/src/Tests/Theme/EngineTwigTest.php +++ b/core/modules/system/src/Tests/Theme/EngineTwigTest.php @@ -2,6 +2,7 @@ namespace Drupal\system\Tests\Theme; +use Drupal\Core\Render\Markup; use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; @@ -75,12 +76,14 @@ public function testTwigLinkGenerator() { /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */ $link_generator = $this->container->get('link_generator'); + $expected = [ 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE])), 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['absolute' => TRUE, 'attributes' => ['foo' => 'bar']])), 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['foo' => 'bar', 'id' => 'kitten']])), 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['id' => 'kitten']])), 'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], ['attributes' => ['class' => ['llama', 'kitten', 'panda']]])), + 'link via the linkgenerator: ' . $link_generator->generate(Markup::create('register'), new Url('user.register', [], ['absolute' => TRUE])), ]; // Verify that link() has the ability to bubble cacheability metadata: diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig index 8925705be1eea73c0ce725d5a9fc343ebb642d28..5d20ce22b90290a6ba75c83c52525eae89e22f28 100644 --- a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig @@ -3,3 +3,5 @@