Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
17422375
Commit
17422375
authored
Sep 26, 2016
by
Alex Pott
Browse files
Issue
#2744517
by webflo: Twig template link() call to accept markup
parent
2a3217ce
Changes
4
Hide whitespace changes
Inline
Side-by-side
core/lib/Drupal/Core/Template/TwigExtension.php
View file @
17422375
...
...
@@ -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
,
...
...
core/lib/Drupal/Core/Utility/LinkGeneratorInterface.php
View file @
17422375
...
...
@@ -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
...
...
core/modules/system/src/Tests/Theme/EngineTwigTest.php
View file @
17422375
...
...
@@ -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
(
'<span>register</span>'
),
new
Url
(
'user.register'
,
[],
[
'absolute'
=>
TRUE
])),
];
// Verify that link() has the ability to bubble cacheability metadata:
...
...
core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.link_generator.html.twig
View file @
17422375
...
...
@@ -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>
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment