diff --git a/core/core.services.yml b/core/core.services.yml index 15f1196..a9821b5 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1521,6 +1521,11 @@ services: class: Drupal\Core\Render\Placeholder\SingleFlushStrategy tags: - { name: placeholder_strategy, priority: -1000 } + placeholder_strategy.static_cache: + class: Drupal\Core\Render\Placeholder\StaticCacheStrategy + arguments: ['@render_placeholder_generator'] + tags: + - { name: placeholder_strategy, priority: 10 } email.validator: class: Egulias\EmailValidator\EmailValidator diff --git a/core/lib/Drupal/Core/Render/Placeholder/StaticCacheStrategy.php b/core/lib/Drupal/Core/Render/Placeholder/StaticCacheStrategy.php new file mode 100644 index 0000000..fd2a5e0 --- /dev/null +++ b/core/lib/Drupal/Core/Render/Placeholder/StaticCacheStrategy.php @@ -0,0 +1,59 @@ +placeholderGenerator = $placeholder_generator; + } + + /** + * {@inheritdoc} + */ + public function processPlaceholders(array $placeholders) { + $rendered_placeholders = $this->placeholderGenerator->getRenderedPlaceholders(); + + if (empty($rendered_placeholders)) { + return []; + } + + $return = []; + + foreach ($rendered_placeholders as $placeholder => $rendered_element) { + // Only replace those placeholders we are asked to process. + if (isset($placeholders[$placeholder])) { + $return[$placeholder] = $rendered_element; + } + } + + return $return; + } +} diff --git a/core/lib/Drupal/Core/Render/PlaceholderGenerator.php b/core/lib/Drupal/Core/Render/PlaceholderGenerator.php index a4ac138..a37ada8 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderGenerator.php +++ b/core/lib/Drupal/Core/Render/PlaceholderGenerator.php @@ -25,6 +25,13 @@ class PlaceholderGenerator implements PlaceholderGeneratorInterface { protected $rendererConfig; /** + * Stores already rendered placeholders. + * + * @var array + */ + protected $renderedPlaceholders; + + /** * Constructs a new Placeholder service. * * @param array $renderer_config @@ -73,7 +80,7 @@ public function shouldAutomaticallyPlaceholder(array $element) { /** * {@inheritdoc} */ - public function createPlaceholder(array $element) { + public function createPlaceholder(array $element, array $rendered_element = NULL) { $placeholder_render_array = array_intersect_key($element, [ // Placeholders are replaced with markup by executing the associated // #lazy_builder callback, which generates a render array, and which the @@ -98,7 +105,20 @@ public function createPlaceholder(array $element) { $placeholder_element = []; $placeholder_element['#markup'] = $placeholder_markup; $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array; + + // Store the rendered element in a per placeholder cache. + if (isset($rendered_element)) { + $this->renderedPlaceholders[$placeholder_markup] = $rendered_element; + } + return $placeholder_element; } + /** + * {@inheritdoc} + */ + public function getRenderedPlaceholders() { + return $this->renderedPlaceholders; + } + } diff --git a/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php b/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php index 89802a2..74728e8 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php +++ b/core/lib/Drupal/Core/Render/PlaceholderGeneratorInterface.php @@ -55,11 +55,21 @@ public function shouldAutomaticallyPlaceholder(array $element); * * @param array $element * The render array to create a placeholder for. + * @param array $rendered_element + * (optional) The rendered render array to store statically per + * placeholder. * * @return array * Render array with placeholder markup and the attached placeholder * replacement metadata. */ - public function createPlaceholder(array $element); + public function createPlaceholder(array $element, array $rendered_element = NULL); + /** + * Returns the already rendered placeholders. + * + * @return array[] + * Array of render arrays keyed by placeholder ID. + */ + public function getRenderedPlaceholders(); } diff --git a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php index 8460f0a..b93d933 100644 --- a/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php +++ b/core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php @@ -83,7 +83,7 @@ public function get(array $elements) { } else { if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($cached_element)) { - return $this->placeholderGenerator->createPlaceholder($elements); + return $this->placeholderGenerator->createPlaceholder($elements, $cached_element); } return $cached_element; @@ -100,7 +100,8 @@ public function set(array &$elements, array $pre_bubbling_elements) { // Overwrite $elements with a placeholder. The Renderer (which called this // method) will update the context with the bubbleable metadata of the // overwritten $elements. - $elements = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements); + $rendered_element = $this->getCacheableRenderArray($elements); + $elements = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements, $rendered_element); } return $result;