diff --git a/core/core.services.yml b/core/core.services.yml index 3fbeb05..bc72d11 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -454,7 +454,7 @@ services: arguments: ['@html_fragment_renderer', '@html_page_renderer'] html_fragment_renderer: class: Drupal\Core\Page\DefaultHtmlFragmentRenderer - arguments: ['@language_manager', '@module_handler', '@http_kernel', '@url_generator', '@?current_user'] + arguments: ['@language_manager', '@module_handler', '@http_kernel', '@url_generator'] calls: - [setRequest, ['@?request=']] html_fragment_renderer_no_blocks: diff --git a/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php b/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php index e31a6a6..84a1539 100644 --- a/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php +++ b/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php @@ -37,13 +37,6 @@ class DefaultHtmlFragmentRenderer implements HtmlFragmentRendererInterface { protected $request; /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** * The module handler. * * @var \Drupal\Core\Extension\ModuleHandlerInterface @@ -82,15 +75,12 @@ class DefaultHtmlFragmentRenderer implements HtmlFragmentRendererInterface { * The HTTP kernel. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The URL generator. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. */ - public function __construct(LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, HttpKernel $http_kernel, UrlGeneratorInterface $url_generator, AccountInterface $current_user = NULL) { + public function __construct(LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, HttpKernel $http_kernel, UrlGeneratorInterface $url_generator) { $this->languageManager = $language_manager; $this->moduleHandler = $module_handler; $this->httpKernel = $http_kernel; $this->urlGenerator = $url_generator; - $this->currentUser = $current_user; } /** @@ -103,29 +93,6 @@ public function setRequest(Request $request) { $this->request = $request; } - /** - * Determines whether blocks are cacheable. - * - * @return bool - * Returns TRUE if blocks are cacheable, otherwise FALSE. - */ - protected function isBlockCachingEnabled() { - if (!isset($this->blockCachingEnabled)) { - // Block caching is not compatible with node_access modules. We also - // preserve the submission of forms in blocks, by fetching from cache - // only if the request method is 'GET' (or 'HEAD'). User 1 being out of - // the regular 'roles define permissions' schema, it brings too many - // chances of having unwanted output get in the cache and later be served - // to other users. We therefore exclude user 1 from block caching. - if (!$this->request) { - $this->blockCachingEnabled = FALSE; - } - else { - $this->blockCachingEnabled = $this->currentUser->id() !== 1 && count($this->moduleHandler->getImplementations('node_grants')) == 0 && $this->request->isMethodSafe(); - } - } - return $this->blockCachingEnabled; - } /** * Gathers all blocks to be added to the page array. @@ -270,10 +237,12 @@ protected function getBlocksByRegion($region) { $controller = 'Drupal\block\DefaultBlockController::respond'; $attributes = $this->request->attributes->all(); $attributes['block'] = $block; - $attributes['caching'] = $this->isBlockCachingEnabled(); - $response = $this->httpKernel->forward($controller, $attributes); - $fragment = $response->getHtmlFragment(); + $response = $this->httpKernel->forward($controller, $attributes, $this->request->query->all()); + // Ensure that the subrequest returned what we expected. + if ($response->isOk() && $response instanceof HtmlFragmentResponse) { + $fragment = $response->getHtmlFragment(); + } } catch (\Exception $e) { $fragment = NULL; diff --git a/core/lib/Drupal/Core/Page/HtmlFragmentResponse.php b/core/lib/Drupal/Core/Page/HtmlFragmentResponse.php index 7310e3d..53b0aa5 100644 --- a/core/lib/Drupal/Core/Page/HtmlFragmentResponse.php +++ b/core/lib/Drupal/Core/Page/HtmlFragmentResponse.php @@ -37,4 +37,4 @@ public function getHtmlFragment() { return $this->htmlFragment; } -} +} diff --git a/core/modules/block/block.routing.yml b/core/modules/block/block.routing.yml index 8196bd2..2d98606 100644 --- a/core/modules/block/block.routing.yml +++ b/core/modules/block/block.routing.yml @@ -56,9 +56,8 @@ block.category_autocomplete: _permission: 'administer blocks' block.view: - path: '/block-view/{block}/{caching}' + path: '/block-view/{block}' defaults: _controller: 'block_controller:respond' - caching: false requirements: _entity_access: 'block.view' diff --git a/core/modules/block/lib/Drupal/block/DefaultBlockController.php b/core/modules/block/lib/Drupal/block/DefaultBlockController.php index bbae480..9db7d0d 100644 --- a/core/modules/block/lib/Drupal/block/DefaultBlockController.php +++ b/core/modules/block/lib/Drupal/block/DefaultBlockController.php @@ -5,11 +5,88 @@ */ namespace Drupal\block; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Session\AccountInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpFoundation\Request; /** * Default controller for rendering a block. */ -class DefaultBlockController { +class DefaultBlockController implements ContainerInjectionInterface { + + /** + * The current request. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * Constructs a new DefaultBlockController. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The current request. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + */ + public function __construct(Request $request, AccountInterface $current_user, ModuleHandlerInterface $module_handler) { + $this->request = $request; + $this->currentUser = $current_user; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('request'), + $container->get('current_user'), + $container->get('module_handler') + ); + } + + /** + * Determines whether blocks are cacheable. + * + * @return bool + * Returns TRUE if blocks are cacheable, otherwise FALSE. + */ + protected function isBlockCachingEnabled() { + if (!isset($this->blockCachingEnabled)) { + // Block caching is not compatible with node_access modules. We also + // preserve the submission of forms in blocks, by fetching from cache + // only if the request method is 'GET' (or 'HEAD'). User 1 being out of + // the regular 'roles define permissions' schema, it brings too many + // chances of having unwanted output get in the cache and later be served + // to other users. We therefore exclude user 1 from block caching. + if (!$this->request) { + $this->blockCachingEnabled = FALSE; + } + else { + $this->blockCachingEnabled = $this->currentUser->id() !== 1 && count($this->moduleHandler->getImplementations('node_grants')) == 0 && $this->request->isMethodSafe(); + } + } + return $this->blockCachingEnabled; + } /** * Renders a provided block into an HtmlFragment. @@ -17,13 +94,10 @@ class DefaultBlockController { * @param \Drupal\block\BlockInterface $block * The block to render. * - * @param bool $caching - * Is caching enabled. - * * @return \Drupal\Core\Page\HtmlFragment */ - public function respond(BlockInterface $block, $caching) { - return $block->getHtmlFragment($caching); + public function respond(BlockInterface $block) { + return $block->getHtmlFragment($this->isBlockCachingEnabled()); } }