Commit 2405d102 authored by catch's avatar catch

Issue #2274791 by Wim Leers: Fixed Rendered entities should be cached by time zone.

parent cda051c4
......@@ -22,6 +22,10 @@ services:
arguments: ['@request_stack', '@theme.negotiator']
tags:
- { name: cache.context}
cache_context.timezone:
class: Drupal\Core\Cache\TimeZoneCacheContext
tags:
- { name: cache.context}
cache.backend.database:
class: Drupal\Core\Cache\DatabaseBackendFactory
arguments: ['@database']
......
<?php
/**
* @file
* Contains \Drupal\Core\Cache\TimeZoneCacheContext.
*/
namespace Drupal\Core\Cache;
/**
* Defines the TimeZoneCacheContext service, for "per time zone" caching.
*
* @see \Drupal\Core\Session\SessionManager::initialize()
*/
class TimeZoneCacheContext implements CacheContextInterface {
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("Time zone");
}
/**
* {@inheritdoc}
*/
public function getContext() {
// date_default_timezone_set() is called in SessionManager::initialize(), so
// we can safely retrieve the timezone.
return date_default_timezone_get();
}
}
......@@ -174,6 +174,9 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
$view_mode,
'cache_context.theme',
'cache_context.user.roles',
// @todo Move this out of here and into field formatters that depend
// on the timezone. Blocked on https://drupal.org/node/2099137.
'cache_context.timezone',
),
'bin' => $this->cacheBin,
);
......
......@@ -263,7 +263,7 @@ public function testReferencedEntity() {
$this->verifyPageCache($referencing_entity_path, 'HIT', $tags);
// Also verify the existence of an entity render cache entry.
$cid = 'entity_view:entity_test:' . $this->referencing_entity->id() . ':full:stark:r.anonymous';
$cid = 'entity_view:entity_test:' . $this->referencing_entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$this->verifyRenderCache($cid, $referencing_entity_cache_tags);
// Prime the page cache for the non-referencing entity.
......@@ -274,7 +274,7 @@ public function testReferencedEntity() {
$this->verifyPageCache($non_referencing_entity_path, 'HIT', $tags);
// Also verify the existence of an entity render cache entry.
$cid = 'entity_view:entity_test:' . $this->non_referencing_entity->id() . ':full:stark:r.anonymous';
$cid = 'entity_view:entity_test:' . $this->non_referencing_entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$this->verifyRenderCache($cid, $non_referencing_entity_cache_tags);
......
......@@ -41,7 +41,7 @@ public function testEntityUri() {
// Also verify the existence of an entity render cache entry, if this entity
// type supports render caching.
if (\Drupal::entityManager()->getDefinition($entity_type)->isRenderCacheable()) {
$cid = 'entity_view:' . $entity_type . ':' . $this->entity->id() . ':full:stark:r.anonymous';
$cid = 'entity_view:' . $entity_type . ':' . $this->entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$cache_entry = \Drupal::cache('render')->get($cid);
$expected_cache_tags = array_merge(array($view_cache_tag, $cache_tag), $this->getAdditionalCacheTagsForEntity($this->entity));
$this->assertIdentical($cache_entry->tags, $expected_cache_tags);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment