.../Drupal/Core/Render/PlaceholderGenerator.php | 12 ++++++++++ .../Core/Render/PlaceholderGeneratorInterface.php | 11 +++++++++ .../Core/Render/PlaceholderingRenderCache.php | 26 ++++------------------ core/lib/Drupal/Core/Render/Renderer.php | 2 +- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/core/lib/Drupal/Core/Render/PlaceholderGenerator.php b/core/lib/Drupal/Core/Render/PlaceholderGenerator.php index eeeaf24..dc85f93 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderGenerator.php +++ b/core/lib/Drupal/Core/Render/PlaceholderGenerator.php @@ -37,6 +37,18 @@ public function __construct(array $renderer_config) { /** * {@inheritdoc} */ + public function canCreatePlaceholder(array $element) { + return + // If generated by a #lazy_builder callback, placeholdering is possible. + isset($element['#lazy_builder']) + && + // If #create_placeholder === FALSE, placeholdering is disallowed. + (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE); + } + + /** + * {@inheritdoc} + */ public function shouldAutomaticallyPlaceholder(array $element) { $conditions = $this->rendererConfig['auto_placeholder_conditions']; diff --git a/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php b/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php index eb96673..df9034d 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php +++ b/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php @@ -15,6 +15,17 @@ interface PlaceholderGeneratorInterface { /** + * Analyzes whether the given render array can be placeholdered. + * + * @param array $element + * A render array. Its #lazy_builder and #create_placeholder properties are + * analyzed. + * + * @return bool + */ + public function canCreatePlaceholder(array $element); + + /** * Whether the given render array should be automatically placeholdered. * * @param array $element diff --git a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php index 0e3adfe..8f3ff6e 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php +++ b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php @@ -92,8 +92,8 @@ public function get(array $elements) { return FALSE; } else { - if ($this->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($cached_element)) { - return $this->createPlaceholder($elements); + if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($cached_element)) { + return $this->placeholderGenerator->createPlaceholder($elements); } // PlaceholderingRenderCache::set() has cached a bubbled max-age = 0, and @@ -128,7 +128,7 @@ public function set(array &$elements, array $pre_bubbling_elements) { // a #lazy_builder callback. // Note: we can permanently cache the fact that this element's subtree // resulted in a bubbled max-age=0, because cache context variations - if ($result === FALSE && $this->canCreatePlaceholder($pre_bubbling_elements) && $this->requestStack->getCurrentRequest()->isMethodSafe() && !$this->maxAgeAllowsCaching($elements) && $this->maxAgeAllowsCaching($pre_bubbling_elements) && $cid = $this->createCacheID($elements)) { + if ($result === FALSE && $this->placeholderGenerator->canCreatePlaceholder($pre_bubbling_elements) && $this->requestStack->getCurrentRequest()->isMethodSafe() && !$this->maxAgeAllowsCaching($elements) && $this->maxAgeAllowsCaching($pre_bubbling_elements) && $cid = $this->createCacheID($elements)) { // Cache #cache['max-age'] = 0! We just want to cache the cacheability. $data = ['#cache' => $elements['#cache'], '#markup' => '', '#attached' => []]; @@ -138,7 +138,7 @@ public function set(array &$elements, array $pre_bubbling_elements) { $this->expireOverride = FALSE; } - if ($this->canCreatePlaceholder($pre_bubbling_elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { + if ($this->placeholderGenerator->canCreatePlaceholder($pre_bubbling_elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { // Overwrite $elements with a placeholder. The Renderer (which called this // method) will update the context with the bubbleable metadata of the // overwritten $elements. @@ -160,22 +160,4 @@ protected function maxAgeToExpire($max_age) { return parent::maxAgeToExpire($max_age); } - /** - * Analyzes whether the given render array can be placeholdered. - * - * @param array $element - * A render array. Its #lazy_builder and #create_placeholder properties are - * analyzed. - * - * @return bool - */ - protected function canCreatePlaceholder(array $element) { - return - // If generated by a #lazy_builder callback, placeholdering is possible. - isset($element['#lazy_builder']) - && - // If #create_placeholder === FALSE, placeholdering is disallowed. - (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE); - } - } diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 0981a98..c7ec3b9 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -352,7 +352,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) { } } // Determine whether to do auto-placeholdering. - if (isset($elements['#lazy_builder']) && (!isset($elements['#create_placeholder']) || $elements['#create_placeholder'] !== FALSE) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { + if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { $elements['#create_placeholder'] = TRUE; } // If instructed to create a placeholder, and a #lazy_builder callback is