Commit 0e2ca036 authored by xjm's avatar xjm
Browse files

Issue #2505669 by pwolanin, jcloys, joshi.rohit100, dawehner, Wim Leers,...

Issue #2505669 by pwolanin, jcloys, joshi.rohit100, dawehner, Wim Leers, kgoel, larowlan: Inject render service into LinkGenerator instead of calling drupal_render
parent 0b104695
...@@ -695,7 +695,7 @@ services: ...@@ -695,7 +695,7 @@ services:
arguments: ['@request_stack', '@config.factory', '@path_processor_manager'] arguments: ['@request_stack', '@config.factory', '@path_processor_manager']
link_generator: link_generator:
class: Drupal\Core\Utility\LinkGenerator class: Drupal\Core\Utility\LinkGenerator
arguments: ['@url_generator', '@module_handler'] arguments: ['@url_generator', '@module_handler', '@renderer']
router: router:
class: Drupal\Core\Routing\AccessAwareRouter class: Drupal\Core\Routing\AccessAwareRouter
arguments: ['@router.no_access_checks', '@access_manager', '@current_user'] arguments: ['@router.no_access_checks', '@access_manager', '@current_user']
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
use Drupal\Core\GeneratedLink; use Drupal\Core\GeneratedLink;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Path\AliasManagerInterface; use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Template\Attribute; use Drupal\Core\Template\Attribute;
use Drupal\Core\Url; use Drupal\Core\Url;
...@@ -36,6 +37,13 @@ class LinkGenerator implements LinkGeneratorInterface { ...@@ -36,6 +37,13 @@ class LinkGenerator implements LinkGeneratorInterface {
*/ */
protected $moduleHandler; protected $moduleHandler;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/** /**
* Constructs a LinkGenerator instance. * Constructs a LinkGenerator instance.
* *
...@@ -43,10 +51,13 @@ class LinkGenerator implements LinkGeneratorInterface { ...@@ -43,10 +51,13 @@ class LinkGenerator implements LinkGeneratorInterface {
* The url generator. * The url generator.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler. * The module handler.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/ */
public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler) { public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
$this->urlGenerator = $url_generator; $this->urlGenerator = $url_generator;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
$this->renderer = $renderer;
} }
/** /**
...@@ -75,8 +86,7 @@ public function generate($text, Url $url, $collect_cacheability_metadata = FALSE ...@@ -75,8 +86,7 @@ public function generate($text, Url $url, $collect_cacheability_metadata = FALSE
// Start building a structured representation of our link to be altered later. // Start building a structured representation of our link to be altered later.
$variables = array( $variables = array(
// @todo Inject the service when drupal_render() is converted to one. 'text' => is_array($text) ? $this->renderer->render($text) : $text,
'text' => is_array($text) ? drupal_render($text) : $text,
'url' => $url, 'url' => $url,
'options' => $url->getOptions(), 'options' => $url->getOptions(),
); );
......
...@@ -181,7 +181,7 @@ protected function setUpUrlIntegrationServices() { ...@@ -181,7 +181,7 @@ protected function setUpUrlIntegrationServices() {
\Drupal::getContainer()->set('unrouted_url_assembler', $this->unroutedUrlAssembler); \Drupal::getContainer()->set('unrouted_url_assembler', $this->unroutedUrlAssembler);
$this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface')); $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'), $this->renderer);
} }
/** /**
......
...@@ -43,6 +43,13 @@ class LinkGeneratorTest extends UnitTestCase { ...@@ -43,6 +43,13 @@ class LinkGeneratorTest extends UnitTestCase {
*/ */
protected $moduleHandler; protected $moduleHandler;
/**
* The mocked renderer service.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/** /**
* The mocked URL Assembler service. * The mocked URL Assembler service.
* *
...@@ -68,8 +75,8 @@ protected function setUp() { ...@@ -68,8 +75,8 @@ protected function setUp() {
$this->urlGenerator = $this->getMock('\Drupal\Core\Routing\UrlGenerator', array(), array(), '', FALSE); $this->urlGenerator = $this->getMock('\Drupal\Core\Routing\UrlGenerator', array(), array(), '', FALSE);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->renderer = $this->getMock('\Drupal\Core\Render\RendererInterface');
$this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->moduleHandler); $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->moduleHandler, $this->renderer);
$this->urlAssembler = $this->getMock('\Drupal\Core\Utility\UnroutedUrlAssemblerInterface'); $this->urlAssembler = $this->getMock('\Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
} }
......
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