Skip to content

Commit

Permalink
Issue #2999549 by mtift, Daniel Korte, mherchel, alexpott, larowlan: …
Browse files Browse the repository at this point in the history
…Allow button tag in LinkGenerator for better accessibility

(cherry picked from commit 8ed835c0bce767690c584cc07b2d98991b5b78a6)
  • Loading branch information
alexpott committed Mar 13, 2020
1 parent f4bc836 commit 52d208e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
19 changes: 19 additions & 0 deletions lib/Drupal/Core/GeneratedButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Drupal\Core;

/**
* This class holds a <button> generated from the <button> route.
*
* Unlike \Drupal\Core\Render\Element\Button, this is not for generating
* buttons for forms. This class is for putting a button in a list of links
* such as a multi-level menu.
*/
class GeneratedButton extends GeneratedLink {

/**
* {@inheritdoc}
*/
const TAG = 'button';

}
37 changes: 30 additions & 7 deletions lib/Drupal/Core/Utility/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\GeneratedLink;
use Drupal\Core\GeneratedButton;
use Drupal\Core\GeneratedNoLink;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
Expand Down Expand Up @@ -165,19 +166,41 @@ public function generate($text, Url $url) {
if ($url->isExternal()) {
$generated_link = new GeneratedLink();
$attributes['href'] = $url->toString(FALSE);
return $this->doGenerate($generated_link, $attributes, $variables);
}
elseif ($url->isRouted() && $url->getRouteName() === '<nolink>') {
if ($url->isRouted() && $url->getRouteName() === '<nolink>') {
$generated_link = new GeneratedNoLink();
unset($attributes['href']);
return $this->doGenerate($generated_link, $attributes, $variables);
}
else {
$generated_url = $url->toString(TRUE);
$generated_link = GeneratedLink::createFromObject($generated_url);
// The result of the URL generator is a plain-text URL to use as the href
// attribute, and it is escaped by \Drupal\Core\Template\Attribute.
$attributes['href'] = $generated_url->getGeneratedUrl();
if ($url->isRouted() && $url->getRouteName() === '<button>') {
$generated_link = new GeneratedButton();
$attributes['type'] = 'button';
unset($attributes['href']);
return $this->doGenerate($generated_link, $attributes, $variables);
}
$generated_url = $url->toString(TRUE);
$generated_link = GeneratedLink::createFromObject($generated_url);
// The result of the URL generator is a plain-text URL to use as the href
// attribute, and it is escaped by \Drupal\Core\Template\Attribute.
$attributes['href'] = $generated_url->getGeneratedUrl();
return $this->doGenerate($generated_link, $attributes, $variables);
}

/**
* Generates the link.
*
* @param Drupal\Core\GeneratedLink $generated_link
* The generated link, along with its associated cacheability metadata.
* @param array $attributes
* The attributes of the generated link.
* @param array $variables
* The link text, url, and other options.
*
* @return Drupal\Core\GeneratedLink
* The generated link, along with its associated cacheability metadata.
*/
protected function doGenerate($generated_link, $attributes, $variables) {
if (!($variables['text'] instanceof MarkupInterface)) {
$variables['text'] = Html::escape($variables['text']);
}
Expand Down
7 changes: 7 additions & 0 deletions modules/system/system.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ system.theme_settings_theme:
requirements:
_access: 'TRUE'

'<button>':
path: ''
options:
_no_path: TRUE
requirements:
_access: 'TRUE'

'<current>':
path: '<current>'

Expand Down
21 changes: 21 additions & 0 deletions tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\Tests\Core\Utility;

use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\GeneratedButton;
use Drupal\Core\GeneratedNoLink;
use Drupal\Core\GeneratedUrl;
use Drupal\Core\Language\Language;
Expand Down Expand Up @@ -199,6 +200,26 @@ public function testGenerateNone() {
$this->assertSame('<a href="">Test</a>', (string) $result);
}

/**
* Tests the generate() method with the <button> route.
*
* @covers ::generate
*/
public function testGenerateButton() {
$this->urlGenerator->expects($this->never())
->method('generateFromRoute');
$this->moduleHandler->expects($this->once())
->method('alter')
->with('link', $this->isType('array'));

$url = Url::fromRoute('<button>');
$url->setUrlGenerator($this->urlGenerator);

$result = $this->linkGenerator->generate('Test', $url);
$this->assertTrue($result instanceof GeneratedButton);
$this->assertSame('<button type="button">Test</button>', (string) $result);
}

/**
* Tests the generate() method with an external URL.
*
Expand Down

0 comments on commit 52d208e

Please sign in to comment.