core/lib/Drupal/Core/Render/BubbleableMetadata.php | 182 +++++++++++++++- core/lib/Drupal/Core/Render/Renderer.php | 2 +- core/modules/filter/src/Element/ProcessedText.php | 2 +- core/modules/filter/src/FilterProcessResult.php | 235 +-------------------- 4 files changed, 185 insertions(+), 236 deletions(-) diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index 467e02c..7a6ab56 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -59,14 +59,19 @@ class BubbleableMetadata { * An array of cache contexts. * @param string[] $tags * An array of cache tags. - * @param int[] $max_age + * @param int $max_age * Cache max-age, in seconds. Cache::PERMANENT to cache forever. * @param array $attached * An array of attached assets. * @param array $post_render_cache * An array of #post_render_cache metadata. + * + * @throws \InvalidArgumentException */ public function __construct(array $contexts = [], array $tags = [], $max_age = Cache::PERMANENT, array $attached = [], array $post_render_cache = []) { + if (!is_int($max_age)) { + throw new \InvalidArgumentException('$max_age must be an integer'); + } $this->contexts = $contexts; $this->tags = $tags; $this->maxAge = $max_age; @@ -129,4 +134,179 @@ public static function createFromRenderArray(array $build) { return $meta; } + /** + * Gets cache tags. + * + * @return string[] + */ + public function getCacheTags() { + return $this->tags; + } + + /** + * Adds cache tags. + * + * @param string[] $cache_tags + * The cache tags to be added. + * + * @return $this + */ + public function addCacheTags(array $cache_tags) { + $this->tags = Cache::mergeTags($this->tags, $cache_tags); + return $this; + } + + /** + * Sets cache tags. + * + * @param string[] $cache_tags + * The cache tags to be associated. + * + * @return $this + */ + public function setCacheTags(array $cache_tags) { + $this->tags = $cache_tags; + return $this; + } + + /** + * Gets cache contexts. + * + * @return string[] + */ + public function getCacheContexts() { + return $this->contexts; + } + + /** + * Adds cache contexts. + * + * @param string[] $cache_contexts + * The cache contexts to be added. + * + * @return $this + */ + public function addCacheContexts(array $cache_contexts) { + $this->contexts = Cache::mergeContexts($this->contexts, $cache_contexts); + return $this; + } + + /** + * Sets cache contexts. + * + * @param string[] $cache_contexts + * The cache contexts to be associated. + * + * @return $this + */ + public function setCacheContexts(array $cache_contexts) { + $this->cacheContexts = $cache_contexts; + return $this; + } + + /** + * Gets the maximum age (in seconds). + * + * @return int + */ + public function getCacheMaxAge() { + return $this->maxAge; + } + + /** + * Sets the maximum age (in seconds). + * + * Defaults to Cache::PERMANENT + * + * @param int $max_age + * The max age to associate. + * + * @return $this + * + * @throws \InvalidArgumentException + */ + public function setCacheMaxAge($max_age) { + if (!is_int($max_age)) { + throw new \InvalidArgumentException('$max_age must be an integer'); + } + + $this->maxAge = $max_age; + return $this; + } + + /** + * Gets assets. + * + * @return array + */ + public function getAssets() { + return $this->attached; + } + + /** + * Adds assets. + * + * @param array $assets + * The associated assets to be attached. + * + * @return $this + */ + public function addAssets(array $assets) { + $this->attached = NestedArray::mergeDeep($this->attached, $assets); + return $this; + } + + /** + * Sets assets. + * + * @param array $assets + * The associated assets to be attached. + * + * @return $this + */ + public function setAssets(array $assets) { + $this->attached = $assets; + return $this; + } + + /** + * Gets #post_render_cache callbacks. + * + * @return array + */ + public function getPostRenderCacheCallbacks() { + return $this->postRenderCache; + } + + /** + * Adds #post_render_cache callbacks. + * + * @param string $callback + * The #post_render_cache callback that will replace the placeholder with + * its eventual markup. + * @param array $context + * An array providing context for the #post_render_cache callback. + * + * @see \Drupal\Core\Render\RendererInterface::generateCachePlaceholder() + * + * @return $this + */ + public function addPostRenderCacheCallback($callback, array $context) { + $this->postRenderCache[$callback][] = $context; + return $this; + } + + /** + * Sets #post_render_cache callbacks. + * + * @param array $post_render_cache_callbacks + * The associated #post_render_cache callbacks to be executed. + * + * @return $this + */ + public function setPostRenderCacheCallbacks(array $post_render_cache_callbacks) { + $this->postRenderCache = $post_render_cache_callbacks; + return $this; + } + } diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 5c0c915..860a2e9 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -730,7 +730,7 @@ protected function cacheSet(array &$elements, $pre_bubbling_cid) { */ protected function createCacheID(array $elements) { // If the maximum age is zero, then caching is effectively prohibited. - if ($elements['#cache']['max-age'] === 0) { + if (!empty($elements['#cache']['max-age'])) { return FALSE; } diff --git a/core/modules/filter/src/Element/ProcessedText.php b/core/modules/filter/src/Element/ProcessedText.php index 52f0649..d007b5f 100644 --- a/core/modules/filter/src/Element/ProcessedText.php +++ b/core/modules/filter/src/Element/ProcessedText.php @@ -113,7 +113,7 @@ public static function preRenderText($element) { foreach ($filters as $filter) { if ($filter_must_be_applied($filter)) { $result = $filter->process($text, $langcode); - $metadata = $metadata->merge($result->getBubbleableMetadata()); + $metadata = $metadata->merge($result); $text = $result->getProcessedText(); } } diff --git a/core/modules/filter/src/FilterProcessResult.php b/core/modules/filter/src/FilterProcessResult.php index d3aeade..548faea 100644 --- a/core/modules/filter/src/FilterProcessResult.php +++ b/core/modules/filter/src/FilterProcessResult.php @@ -65,7 +65,7 @@ * } * @endcode */ -class FilterProcessResult { +class FilterProcessResult extends BubbleableMetadata { /** * The processed text. @@ -77,60 +77,14 @@ class FilterProcessResult { protected $processedText; /** - * An array of associated assets to be attached. - * - * @see drupal_process_attached() - * - * @var array - */ - protected $assets; - - /** - * The attached cache tags. - * - * @see drupal_render_collect_cache_tags() - * - * @var array - */ - protected $cacheTags; - - /** - * The associated cache contexts. - * - * @var string[] - */ - protected $cacheContexts; - - /** - * The maximum age. - * - * @var int - */ - protected $cacheMaxAge; - - /** - * The associated #post_render_cache callbacks. - * - * @see _drupal_render_process_post_render_cache() - * - * @var array - */ - protected $postRenderCacheCallbacks; - - /** * Constructs a FilterProcessResult object. * * @param string $processed_text * The text as processed by a text filter. */ public function __construct($processed_text) { + parent::__construct(); $this->processedText = $processed_text; - - $this->assets = array(); - $this->cacheTags = array(); - $this->cacheContexts = array(); - $this->cacheMaxAge = Cache::PERMANENT; - $this->postRenderCacheCallbacks = array(); } /** @@ -163,189 +117,4 @@ public function setProcessedText($processed_text) { $this->processedText = $processed_text; return $this; } - - /** - * Gets cache tags associated with the processed text. - * - * @return array - */ - public function getCacheTags() { - return $this->cacheTags; - } - - /** - * Adds cache tags associated with the processed text. - * - * @param array $cache_tags - * The cache tags to be added. - * - * @return $this - */ - public function addCacheTags(array $cache_tags) { - $this->cacheTags = Cache::mergeTags($this->cacheTags, $cache_tags); - return $this; - } - - /** - * Sets cache tags associated with the processed text. - * - * @param array $cache_tags - * The cache tags to be associated. - * - * @return $this - */ - public function setCacheTags(array $cache_tags) { - $this->cacheTags = $cache_tags; - return $this; - } - - /** - * Gets cache contexts associated with the processed text. - * - * @return string[] - */ - public function getCacheContexts() { - return $this->cacheContexts; - } - - /** - * Adds cache contexts associated with the processed text. - * - * @param string[] $cache_contexts - * The cache contexts to be added. - * - * @return $this - */ - public function addCacheContexts(array $cache_contexts) { - $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts); - return $this; - } - - /** - * Sets cache contexts associated with the processed text. - * - * @param string[] $cache_contexts - * The cache contexts to be associated. - * - * @return $this - */ - public function setCacheContexts(array $cache_contexts) { - $this->cacheContexts = $cache_contexts; - return $this; - } - - /** - * Gets the maximum age (in seconds) associated with the processed text. - * - * @return int - */ - public function getCacheMaxAge() { - return $this->cacheMaxAge; - } - - /** - * Sets the maximum age (in seconds) associated with the processed text. - * - * Defaults to Cache::PERMANENT - * - * @param int $max_age - * The max age to associate. - * - * @return $this - */ - public function setCacheMaxAge($max_age) { - $this->cacheMaxAge = $max_age; - return $this; - } - - /** - * Gets assets associated with the processed text. - * - * @return array - */ - public function getAssets() { - return $this->assets; - } - - /** - * Adds assets associated with the processed text. - * - * @param array $assets - * The associated assets to be attached. - * - * @return $this - */ - public function addAssets(array $assets) { - $this->assets = NestedArray::mergeDeep($this->assets, $assets); - return $this; - } - - /** - * Sets assets associated with the processed text. - * - * @param array $assets - * The associated assets to be attached. - * - * @return $this - */ - public function setAssets(array $assets) { - $this->assets = $assets; - return $this; - } - - /** - * Gets #post_render_cache callbacks associated with the processed text. - * - * @return array - */ - public function getPostRenderCacheCallbacks() { - return $this->postRenderCacheCallbacks; - } - - /** - * Adds #post_render_cache callbacks associated with the processed text. - * - * @param string $callback - * The #post_render_cache callback that will replace the placeholder with - * its eventual markup. - * @param array $context - * An array providing context for the #post_render_cache callback. - * - * @see drupal_render_cache_generate_placeholder() - * - * @return $this - */ - public function addPostRenderCacheCallback($callback, array $context) { - $this->postRenderCacheCallbacks[$callback][] = $context; - return $this; - } - - /** - * Sets #post_render_cache callbacks associated with the processed text. - * - * @param array $post_render_cache_callbacks - * The associated #post_render_cache callbacks to be executed. - * - * @return $this - */ - public function setPostRenderCacheCallbacks(array $post_render_cache_callbacks) { - $this->postRenderCacheCallbacks = $post_render_cache_callbacks; - return $this; - } - - /** - * Returns the attached asset libraries, etc. as a bubbleable metadata object. - * - * @return \Drupal\Core\Render\BubbleableMetadata - */ - public function getBubbleableMetadata() { - return new BubbleableMetadata( - $this->getCacheContexts(), - $this->getCacheTags(), - $this->getCacheMaxAge(), - $this->getAssets(), - $this->getPostRenderCacheCallbacks() - ); - } - }