.../toolbar/src/Controller/ToolbarController.php | 36 +--------------------- core/modules/toolbar/toolbar.module | 12 ++++++-- 2 files changed, 11 insertions(+), 37 deletions(-) diff --git a/core/modules/toolbar/src/Controller/ToolbarController.php b/core/modules/toolbar/src/Controller/ToolbarController.php index af58522..b4abf27 100644 --- a/core/modules/toolbar/src/Controller/ToolbarController.php +++ b/core/modules/toolbar/src/Controller/ToolbarController.php @@ -6,10 +6,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Controller\ControllerBase; -use Drupal\Core\Render\RenderContext; -use Drupal\Core\Render\RendererInterface; use Drupal\toolbar\Ajax\SetSubtreesCommand; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines a controller for the toolbar module. @@ -17,32 +14,6 @@ class ToolbarController extends ControllerBase { /** - * The renderer. - * - * @var \Drupal\Core\Render\RendererInterface - */ - protected $renderer; - - /** - * Constructs the object. - * - * @param \Drupal\Core\Render\RendererInterface $renderer - * The renderer. - */ - public function __construct(RendererInterface $renderer) { - $this->renderer = $renderer; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('renderer') - ); - } - - /** * Returns an AJAX response to render the toolbar subtrees. * * @return \Drupal\Core\Ajax\AjaxResponse @@ -77,12 +48,7 @@ public function subtreesAjax() { * The access result. */ public function checkSubTreeAccess($hash) { - // As subtree-hashes are calculated during rendering, there must be an - // active render context. Provide a new render context so that the - // cacheability metadata of the rendered HTML will be captured correctly. - $expected_hash = $this->renderer->executeInRenderContext(new RenderContext(), function () { - return _toolbar_get_subtrees_hash()[0]; - }); + $expected_hash = _toolbar_get_subtrees_hash()[0]; return AccessResult::allowedIf($this->currentUser()->hasPermission('access toolbar') && Crypt::hashEquals($expected_hash, $hash))->cachePerPermissions(); } diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index 7bb9875..a52ddad 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -8,6 +8,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Menu\MenuTreeParameters; use Drupal\Core\Render\Element; +use Drupal\Core\Render\RenderContext; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Template\Attribute; use Drupal\Component\Utility\Crypt; @@ -306,7 +307,11 @@ function toolbar_get_rendered_subtrees() { ], '#cache_properties' => ['#subtrees'], ]; - \Drupal::service('renderer')->render($data); + $renderer = \Drupal::service('renderer'); + $renderer->executeInRenderContext(new RenderContext(), function() use ($renderer, $data) { + $renderer->render($data); + }); + \Drupal::service('renderer')->renderPlain($data); return [$data['#subtrees'], CacheableMetadata::createFromRenderArray($data)]; } @@ -315,6 +320,7 @@ function toolbar_get_rendered_subtrees() { */ function _toolbar_do_get_rendered_subtrees(array $data) { $menu_tree = \Drupal::service('toolbar.menu_tree'); + $renderer = \Drupal::service('renderer'); // Load the administration menu. The first level is the "Administration" link. // In order to load the children of that link and the subsequent two levels, // start at the second level and end at the fourth. @@ -336,7 +342,9 @@ function _toolbar_do_get_rendered_subtrees(array $data) { $link = $element->link; if ($element->subtree) { $subtree = $menu_tree->build($element->subtree); - $output = \Drupal::service('renderer')->render($subtree); + $output = $renderer->executeInRenderContext(new RenderContext(), function() use ($renderer, $subtree) { + return $renderer->render($subtree); + }); $cacheability = $cacheability->merge(CacheableMetadata::createFromRenderArray($subtree)); } else {