summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-09-26 08:08:23 +0100
committerAlex Pott2016-09-26 08:08:23 +0100
commit174223750940ef712d6dbe30608e9b6d1cf893d3 (patch)
tree0300e566523c00f306bf47417a202608be0a3524
parent2a3217ce62b719ef50776ee145773d6ae5b216fa (diff)
Issue #2744517 by webflo: Twig template link() call to accept markup
-rw-r--r--core/lib/Drupal/Core/Template/TwigExtension.php6
-rw-r--r--core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php2
-rw-r--r--core/modules/system/src/Tests/Theme/EngineTwigTest.php3
-rw-r--r--core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig2
4 files changed, 12 insertions, 1 deletions
diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php
index 3ebe01b..8270c66 100644
--- a/core/lib/Drupal/Core/Template/TwigExtension.php
+++ b/core/lib/Drupal/Core/Template/TwigExtension.php
@@ -8,6 +8,7 @@ use Drupal\Core\Cache\CacheableDependencyInterface;
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 @@ class TwigExtension extends \Twig_Extension {
}
$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 cab7e7c..83aa4f2 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 ae08fc8..a79c01d 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 @@ class EngineTwigTest extends WebTestBase {
/** @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('<span>register</span>'), 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 8925705..5d20ce2 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 @@
<div>link via the linkgenerator: {{ link('register', test_url_attribute, {'id': 'kitten'}) }}</div>
<div>link via the linkgenerator: {{ link('register', 'route:user.register', {'id': 'kitten'}) }}</div>
<div>link via the linkgenerator: {{ link('register', 'route:user.register', attributes) }}</div>
+{% set title %}<span>register</span>{% endset %}
+<div>link via the linkgenerator: {{ link(title, test_url) }}</div>