Commit da8ea3bf authored by alexpott's avatar alexpott

Issue #2346937 by dawehner, larowlan, Wim Leers, claudiu.cristea, msonnabaum:...

Issue #2346937 by dawehner, larowlan, Wim Leers, claudiu.cristea, msonnabaum: Implement a Renderer service; reduces drupal_render / _theme service container calls
parent 040dc5ff
...@@ -667,7 +667,7 @@ services: ...@@ -667,7 +667,7 @@ services:
- { name: event_subscriber } - { name: event_subscriber }
main_content_renderer.html: main_content_renderer.html:
class: Drupal\Core\Render\MainContent\HtmlRenderer class: Drupal\Core\Render\MainContent\HtmlRenderer
arguments: ['@title_resolver', '@plugin.manager.display_variant', '@event_dispatcher', '@module_handler'] arguments: ['@title_resolver', '@plugin.manager.display_variant', '@event_dispatcher', '@module_handler', '@renderer']
tags: tags:
- { name: render.main_content_renderer, format: html } - { name: render.main_content_renderer, format: html }
main_content_renderer.ajax: main_content_renderer.ajax:
...@@ -700,6 +700,7 @@ services: ...@@ -700,6 +700,7 @@ services:
arguments: ['@content_negotiation', '@title_resolver'] arguments: ['@content_negotiation', '@title_resolver']
bare_html_page_renderer: bare_html_page_renderer:
class: Drupal\Core\Render\BareHtmlPageRenderer class: Drupal\Core\Render\BareHtmlPageRenderer
arguments: ['@renderer']
private_key: private_key:
class: Drupal\Core\PrivateKey class: Drupal\Core\PrivateKey
arguments: ['@state'] arguments: ['@state']
...@@ -994,7 +995,7 @@ services: ...@@ -994,7 +995,7 @@ services:
class: Zend\Feed\Writer\Extension\WellFormedWeb\Renderer\Entry class: Zend\Feed\Writer\Extension\WellFormedWeb\Renderer\Entry
theme.manager: theme.manager:
class: Drupal\Core\Theme\ThemeManager class: Drupal\Core\Theme\ThemeManager
arguments: ['@theme.registry', '@theme.negotiator', '@theme.initialization', '@request_stack'] arguments: ['@app.root', '@theme.registry', '@theme.negotiator', '@theme.initialization', '@request_stack', '@module_handler']
theme.initialization: theme.initialization:
class: Drupal\Core\Theme\ThemeInitialization class: Drupal\Core\Theme\ThemeInitialization
arguments: ['@app.root', '@theme_handler', '@state'] arguments: ['@app.root', '@theme_handler', '@state']
...@@ -1097,3 +1098,6 @@ services: ...@@ -1097,3 +1098,6 @@ services:
arguments: ['@module_handler'] arguments: ['@module_handler']
tags: tags:
- { name: mime_type_guesser } - { name: mime_type_guesser }
renderer:
class: Drupal\Core\Render\Renderer
arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info']
This diff is collapsed.
This diff is collapsed.
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -25,14 +26,24 @@ class EntityViewController implements ContainerInjectionInterface { ...@@ -25,14 +26,24 @@ class EntityViewController implements ContainerInjectionInterface {
*/ */
protected $entityManager; protected $entityManager;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/** /**
* Creates an EntityViewController object. * Creates an EntityViewController object.
* *
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager. * The entity manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/ */
public function __construct(EntityManagerInterface $entity_manager) { public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer) {
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
$this->renderer = $renderer;
} }
/** /**
...@@ -40,7 +51,8 @@ public function __construct(EntityManagerInterface $entity_manager) { ...@@ -40,7 +51,8 @@ public function __construct(EntityManagerInterface $entity_manager) {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('entity.manager') $container->get('entity.manager'),
$container->get('renderer')
); );
} }
...@@ -79,7 +91,7 @@ public function view(EntityInterface $_entity, $view_mode = 'full', $langcode = ...@@ -79,7 +91,7 @@ public function view(EntityInterface $_entity, $view_mode = 'full', $langcode =
$build = $this->entityManager->getTranslationFromContext($_entity) $build = $this->entityManager->getTranslationFromContext($_entity)
->get($label_field) ->get($label_field)
->view($view_mode); ->view($view_mode);
$page['#title'] = drupal_render($build); $page['#title'] = $this->renderer->render($build);
} }
} }
......
...@@ -12,6 +12,23 @@ ...@@ -12,6 +12,23 @@
*/ */
class BareHtmlPageRenderer implements BareHtmlPageRendererInterface { class BareHtmlPageRenderer implements BareHtmlPageRendererInterface {
/**
* The renderer service.
*
* @var \Drupal\Core\Render\Renderer
*/
protected $renderer;
/**
* Constructs a new BareHtmlPageRenderer.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -36,12 +53,12 @@ public function renderBarePage(array $content, $title, $page_theme_property, arr ...@@ -36,12 +53,12 @@ public function renderBarePage(array $content, $title, $page_theme_property, arr
// \Drupal\Core\Render\MainContent\HtmlRenderer::renderResponse() for more // \Drupal\Core\Render\MainContent\HtmlRenderer::renderResponse() for more
// information about this; the exact same pattern is used there and // information about this; the exact same pattern is used there and
// explained in detail there. // explained in detail there.
drupal_render_root($html['page']); $this->renderer->render($html['page'], TRUE);
// Add the bare minimum of attachments from the system module and the // Add the bare minimum of attachments from the system module and the
// current maintenance theme. // current maintenance theme.
system_page_attachments($html['page']); system_page_attachments($html['page']);
return drupal_render($html); return $this->renderer->render($html);
} }
} }
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
use Drupal\Core\Display\PageVariantInterface; use Drupal\Core\Display\PageVariantInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\PageDisplayVariantSelectionEvent; use Drupal\Core\Render\PageDisplayVariantSelectionEvent;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Render\RenderEvents; use Drupal\Core\Render\RenderEvents;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerAwareTrait;
...@@ -54,6 +55,13 @@ class HtmlRenderer implements MainContentRendererInterface { ...@@ -54,6 +55,13 @@ class HtmlRenderer implements MainContentRendererInterface {
*/ */
protected $moduleHandler; protected $moduleHandler;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/** /**
* Constructs a new HtmlRenderer. * Constructs a new HtmlRenderer.
* *
...@@ -65,12 +73,15 @@ class HtmlRenderer implements MainContentRendererInterface { ...@@ -65,12 +73,15 @@ class HtmlRenderer implements MainContentRendererInterface {
* The event dispatcher. * The event dispatcher.
* @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(TitleResolverInterface $title_resolver, PluginManagerInterface $display_variant_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler) { public function __construct(TitleResolverInterface $title_resolver, PluginManagerInterface $display_variant_manager, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
$this->titleResolver = $title_resolver; $this->titleResolver = $title_resolver;
$this->displayVariantManager = $display_variant_manager; $this->displayVariantManager = $display_variant_manager;
$this->eventDispatcher = $event_dispatcher; $this->eventDispatcher = $event_dispatcher;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
$this->renderer = $renderer;
} }
/** /**
...@@ -108,14 +119,14 @@ public function renderResponse(array $main_content, Request $request, RouteMatch ...@@ -108,14 +119,14 @@ public function renderResponse(array $main_content, Request $request, RouteMatch
// and hence may not execute any #post_render_cache_callbacks (because they // and hence may not execute any #post_render_cache_callbacks (because they
// might add yet more assets to be attached), and therefore it must be // might add yet more assets to be attached), and therefore it must be
// rendered with drupal_render(), not drupal_render_root(). // rendered with drupal_render(), not drupal_render_root().
drupal_render_root($html['page']); $this->renderer->render($html['page'], TRUE);
if (isset($html['page_top'])) { if (isset($html['page_top'])) {
drupal_render_root($html['page_top']); $this->renderer->render($html['page_top'], TRUE);
} }
if (isset($html['page_bottom'])) { if (isset($html['page_bottom'])) {
drupal_render_root($html['page_bottom']); $this->renderer->render($html['page_bottom'], TRUE);
} }
$content = drupal_render($html); $content = $this->renderer->render($html);
// Store the cache tags associated with this page in a X-Drupal-Cache-Tags // Store the cache tags associated with this page in a X-Drupal-Cache-Tags
// header. Also associate the "rendered" cache tag. This allows us to // header. Also associate the "rendered" cache tag. This allows us to
...@@ -177,7 +188,7 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte ...@@ -177,7 +188,7 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
// ::renderContentIntoResponse(). // ::renderContentIntoResponse().
// @todo Remove this once https://www.drupal.org/node/2359901 lands. // @todo Remove this once https://www.drupal.org/node/2359901 lands.
if (!empty($main_content)) { if (!empty($main_content)) {
drupal_render($main_content, FALSE); $this->renderer->render($main_content, FALSE);
$main_content = [ $main_content = [
'#markup' => $main_content['#markup'], '#markup' => $main_content['#markup'],
'#attached' => $main_content['#attached'], '#attached' => $main_content['#attached'],
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\node\NodeTypeInterface; use Drupal\node\NodeTypeInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
...@@ -29,21 +30,34 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa ...@@ -29,21 +30,34 @@ class NodeController extends ControllerBase implements ContainerInjectionInterfa
*/ */
protected $dateFormatter; protected $dateFormatter;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/** /**
* Constructs a NodeController object. * Constructs a NodeController object.
* *
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter service. * The date formatter service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/ */
public function __construct(DateFormatter $date_formatter) { public function __construct(DateFormatter $date_formatter, RendererInterface $renderer) {
$this->dateFormatter = $date_formatter; $this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static($container->get('date.formatter')); return new static(
$container->get('date.formatter'),
$container->get('renderer')
);
} }
...@@ -112,7 +126,7 @@ public function add(NodeTypeInterface $node_type) { ...@@ -112,7 +126,7 @@ public function add(NodeTypeInterface $node_type) {
*/ */
public function revisionShow($node_revision) { public function revisionShow($node_revision) {
$node = $this->entityManager()->getStorage('node')->loadRevision($node_revision); $node = $this->entityManager()->getStorage('node')->loadRevision($node_revision);
$node_view_controller = new NodeViewController($this->entityManager); $node_view_controller = new NodeViewController($this->entityManager, $this->renderer);
$page = $node_view_controller->view($node); $page = $node_view_controller->view($node);
unset($page['nodes'][$node->id()]['#cache']); unset($page['nodes'][$node->id()]['#cache']);
return $page; return $page;
......
...@@ -94,6 +94,13 @@ abstract class FieldPluginBase extends HandlerBase { ...@@ -94,6 +94,13 @@ abstract class FieldPluginBase extends HandlerBase {
*/ */
protected $linkGenerator; protected $linkGenerator;
/**
* Stores the render API renderer.
*
* @var \Drupal\Core\Render\Renderer
*/
protected $renderer;
/** /**
* Overrides Drupal\views\Plugin\views\HandlerBase::init(). * Overrides Drupal\views\Plugin\views\HandlerBase::init().
*/ */
...@@ -907,7 +914,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ...@@ -907,7 +914,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#items' => $items, '#items' => $items,
'#list_type' => $type, '#list_type' => $type,
); );
$output .= drupal_render($item_list); $output .= $this->getRenderer()->render($item_list);
} }
} }
} }
...@@ -1153,7 +1160,7 @@ public function advancedRender(ResultRow $values) { ...@@ -1153,7 +1160,7 @@ public function advancedRender(ResultRow $values) {
else { else {
$value = $this->render($values); $value = $this->render($values);
if (is_array($value)) { if (is_array($value)) {
$value = drupal_render($value); $value = $this->getRenderer()->render($value);
} }
$this->last_render = $value; $this->last_render = $value;
$this->original_value = $value; $this->original_value = $value;
...@@ -1166,7 +1173,7 @@ public function advancedRender(ResultRow $values) { ...@@ -1166,7 +1173,7 @@ public function advancedRender(ResultRow $values) {
foreach ($raw_items as $count => $item) { foreach ($raw_items as $count => $item) {
$value = $this->render_item($count, $item); $value = $this->render_item($count, $item);
if (is_array($value)) { if (is_array($value)) {
$value = drupal_render($value); $value = $this->getRenderer()->render($value);
} }
$this->last_render = $value; $this->last_render = $value;
$this->original_value = $this->last_render; $this->original_value = $this->last_render;
...@@ -1184,7 +1191,7 @@ public function advancedRender(ResultRow $values) { ...@@ -1184,7 +1191,7 @@ public function advancedRender(ResultRow $values) {
} }
if (is_array($value)) { if (is_array($value)) {
$value = drupal_render($value); $value = $this->getRenderer()->render($value);
} }
// This happens here so that renderAsLink can get the unaltered value of // This happens here so that renderAsLink can get the unaltered value of
// this field as a token rather than the altered value. // this field as a token rather than the altered value.
...@@ -1640,7 +1647,7 @@ protected function addSelfTokens(&$tokens, $item) { } ...@@ -1640,7 +1647,7 @@ protected function addSelfTokens(&$tokens, $item) { }
protected function documentSelfTokens(&$tokens) { } protected function documentSelfTokens(&$tokens) { }
/** /**
* Pass values to drupal_render() using $this->themeFunctions() as #theme. * Pass values to $this->getRenderer()->render() using $this->themeFunctions() as #theme.
* *
* @param \Drupal\views\ResultRow $values * @param \Drupal\views\ResultRow $values
* Holds single row of a view's result set. * Holds single row of a view's result set.
...@@ -1655,7 +1662,7 @@ function theme(ResultRow $values) { ...@@ -1655,7 +1662,7 @@ function theme(ResultRow $values) {
'#field' => $this, '#field' => $this,
'#row' => $values, '#row' => $values,
); );
return drupal_render($build); return $this->getRenderer()->render($build);
} }
public function themeFunctions() { public function themeFunctions() {
...@@ -1745,6 +1752,20 @@ protected function linkGenerator() { ...@@ -1745,6 +1752,20 @@ protected function linkGenerator() {
} }
return $this->linkGenerator; return $this->linkGenerator;
} }
/**
* Returns the render API renderer.
*
* @return \Drupal\Core\Render\Renderer
*/
protected function getRenderer() {
if (!isset($this->renderer)) {
$this->renderer = \Drupal::service('renderer');
}
return $this->renderer;
}
} }
/** /**
......
...@@ -67,7 +67,7 @@ public function testView() { ...@@ -67,7 +67,7 @@ public function testView() {
// Initialize the controller to test. // Initialize the controller to test.
$controller = new EntityViewController($entity_manager); $controller = new EntityViewController($entity_manager, $this->getMock('Drupal\Core\Render\RendererInterface'));
// Test the view method. // Test the view method.
$this->assertEquals($controller->view($entity, 'full'), 'Output from rendering the entity'); $this->assertEquals($controller->view($entity, 'full'), 'Output from rendering the entity');
......
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