From 6aae55904008de3fa60ff3d7faa41793d148d231 Mon Sep 17 00:00:00 2001 From: hanoii Date: Wed, 18 Jul 2018 09:36:05 +0500 Subject: [PATCH] Issue #2978898 by hanoii: Add support for HTML markup on drupal_link --- src/TwigExtension.php | 7 +++++++ tests/src/Functional/TwigTweakTest.php | 8 ++++++++ tests/twig_tweak_test/templates/twig-tweak-test.html.twig | 1 + 3 files changed, 16 insertions(+) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 4cb623e..ba512f7 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -13,6 +13,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Link; use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Render\Element; +use Drupal\Core\Render\Markup; use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Drupal\image\Entity\ImageStyle; @@ -535,6 +536,12 @@ class TwigExtension extends \Twig_Extension { public function drupalLink($text, $user_input, array $options = [], $check_access = FALSE) { $url = $this->drupalUrl($user_input, $options, $check_access); if ($url) { + // The text has been processed by twig already, convert it to a safe + // object for the render system. + // @see \Drupal\Core\Template\TwigExtension::getLink() + if ($text instanceof \Twig_Markup) { + $text = Markup::create($text); + } return Link::fromTextAndUrl($text, $url); } } diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index d16b35f..28a86e4 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -6,6 +6,7 @@ use Drupal\Core\Link; use Drupal\Core\Url; use Drupal\file\Entity\File; use Drupal\responsive_image\Entity\ResponsiveImageStyle; +use Drupal\Core\Render\Markup; use Drupal\Tests\BrowserTestBase; use Drupal\user\Entity\Role; @@ -222,6 +223,13 @@ class TwigTweakTest extends BrowserTestBase { $xpath = '//div[@class = "tt-link"]'; self::assertEquals($link, trim($this->xpath($xpath)[0]->getHtml())); + // Test link with HTML. + $text = Markup::create('Edit'); + $url = Url::fromUserInput('/node/1/edit', ['absolute' => TRUE]); + $link = Link::fromTextAndUrl($text, $url)->toString(); + $xpath = '//div[@class = "tt-link-html"]'; + self::assertEquals($link, trim($this->xpath($xpath)[0]->getHtml())); + // Test status messages. $xpath = '//div[@class = "tt-messages"]/div[contains(@class, "messages--status") and contains(., "Hello world!")]'; $this->assertByXpath($xpath); diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index ac6f42f..6c6bdf2 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -45,6 +45,7 @@
{{ drupal_title() }}
{{ drupal_url('node/1', {absolute: true}) }}
+
{{ drupal_messages() }}
{{ drupal_breadcrumb() }}