core/includes/common.inc | 2 +- .../Core/EventSubscriber/HtmlViewSubscriber.php | 40 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index 12830e5..2d6753c 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3241,7 +3241,7 @@ function drupal_page_set_cache(Response $response, Request $request) { // because by the time it is read, the configuration might change. 'page_compressed' => $page_compressed, ), - 'tags' => explode(' ', $response->headers->get('X-Drupal-Cache-Tags')), + 'tags' => Drupal\Core\EventSubscriber\HtmlViewSubscriber::convertHeaderToCacheTags($response->headers->get('X-Drupal-Cache-Tags')), 'expire' => Cache::PERMANENT, 'created' => REQUEST_TIME, ); diff --git a/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php index c71d86a..4da4d43 100644 --- a/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php @@ -77,7 +77,7 @@ public function onHtmlPage(GetResponseForControllerResultEvent $event) { // recommended. $response = new Response((string) $this->pageRenderer->render($page), $page->getStatusCode()); if ($tags = $page->getCacheTags()) { - $response->headers->set('X-Drupal-Cache-Tags', $this->flattenCacheTags($tags)); + $response->headers->set('X-Drupal-Cache-Tags', $this->convertCacheTagsToHeader($tags)); } if ($keys = $page->getCacheKeys()) { $response->headers->set('cache_keys', serialize($keys)); @@ -106,15 +106,15 @@ static function getSubscribedEvents() { } /** - * 'Flattens' a tags array into an array of strings. + * Converts a cache tags array into a X-Drupal-Cache-Tags header value. * * @param array $tags * Associative array of cache tags to flatten. * * @return string - * A space-separated liste of flattened cache tag identifiers. + * A space-separated list of flattened cache tag identifiers. */ - protected function flattenCacheTags(array $tags) { + public static function convertCacheTagsToHeader(array $tags) { $flat_tags = array(); foreach ($tags as $namespace => $values) { if (is_array($values)) { @@ -129,4 +129,36 @@ protected function flattenCacheTags(array $tags) { return implode(' ', $flat_tags); } + /** + * 'Flattens' a tags array into an array of strings. + * + * @param string $tags + * A space-separated list of flattened cache tag identifiers. + * + * @return array + * Associative array of cache tags to flatten. + */ + public static function convertHeaderToCacheTags($tags_header) { + if (!is_string($tags_header) || strlen(trim($tags_header)) == 0) { + return array(); + } + + $flat_tags = explode(' ', trim($tags_header)); + $tags = array(); + foreach ($flat_tags as $flat_tag) { + list($namespace, $value) = explode(':', $flat_tag); + if (!isset($tags[$namespace])) { + $tags[$namespace] = $value; + } + // Multiple values in this namespace. + else { + if (!is_array($tags[$namespace])) { + $tags[$namespace] = array($tags[$namespace]); + } + $tags[$namespace][] = $value; + } + } + return $tags; + } + }