Commit e2851240 authored by alexpott's avatar alexpott

Issue #2342745 by Cottser, chr.fritsch, epari.siva, lauriii, wesruv,...

Issue #2342745 by Cottser, chr.fritsch, epari.siva, lauriii, wesruv, joelpittet, dawehner: Allow Twig link function to pass in HTML attributes
parent ca5fced8
......@@ -222,14 +222,22 @@ public function getUrlFromPath($path, $options = array()) {
* The link text for the anchor tag as a translated string.
* @param \Drupal\Core\Url|string $url
* The URL object or string used for the link.
* @param array $attributes
* An optional array of link attributes.
*
* @return array
* A render array representing a link to the given URL.
*/
public function getLink($text, $url) {
public function getLink($text, $url, array $attributes = []) {
if (!$url instanceof Url) {
$url = Url::fromUri($url);
}
if ($attributes) {
if ($existing_attributes = $url->getOption('attributes')) {
$attributes = array_merge($existing_attributes, $attributes);
}
$url->setOption('attributes', $attributes);
}
$build = [
'#type' => 'link',
'#title' => $text,
......
......@@ -77,10 +77,14 @@ public function testTwigUrlGenerator() {
public function testTwigLinkGenerator() {
$this->drupalGet('twig-theme-test/link-generator');
/** @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']])),
];
// Verify that link() has the ability to bubble cacheability metadata:
......
......@@ -46,6 +46,7 @@ public function linkGeneratorRender() {
return array(
'#theme' => 'twig_theme_test_link_generator',
'#test_url' => new Url('user.register', [], ['absolute' => TRUE]),
'#test_url_attribute' => new Url('user.register', [], ['attributes' => ['foo' => 'bar']]),
);
}
......
<div>link via the linkgenerator: {{ link("register", test_url) }}</div>
<div>link via the linkgenerator: {{ link('register', test_url) }}</div>
<div>link via the linkgenerator: {{ link('register', test_url, {'foo': 'bar'}) }}</div>
<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>
......@@ -39,7 +39,7 @@ function twig_theme_test_theme($existing, $type, $theme, $path) {
'template' => 'twig_theme_test.url_generator',
);
$items['twig_theme_test_link_generator'] = array(
'variables' => array('test_url' => NULL),
'variables' => array('test_url' => NULL, 'test_url_attribute' => NULL),
'template' => 'twig_theme_test.link_generator',
);
$items['twig_theme_test_url_to_string'] = array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment