core/core.services.yml | 2 +- core/includes/common.inc | 27 -------------- core/lib/Drupal/Core/Render/Renderer.php | 43 ++++++++++++++++++++-- core/lib/Drupal/Core/Render/RendererInterface.php | 3 +- .../block/src/Tests/BlockViewBuilderTest.php | 13 ++++--- .../modules/system/src/Tests/Common/RenderTest.php | 27 +++++--------- .../src/Tests/Entity/EntityViewBuilderTest.php | 10 +++-- 7 files changed, 65 insertions(+), 60 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 3252a82..a3899ab 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1125,4 +1125,4 @@ services: - { name: mime_type_guesser } renderer: class: Drupal\Core\Render\Renderer - arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info', '@request_stack'] + arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info', '@request_stack', '@cache_contexts'] diff --git a/core/includes/common.inc b/core/includes/common.inc index 21930d6..5d063fb 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2413,33 +2413,6 @@ function drupal_render_cache_generate_placeholder($callback, array &$context) { } /** - * Creates the cache ID for a renderable element. - * - * This creates the cache ID string, either by returning the #cache['cid'] - * property if present or by building the cache ID out of the #cache['keys']. - * - * @param $elements - * A renderable array. - * - * @return - * The cache ID string, or FALSE if the element may not be cached. - */ -function drupal_render_cid_create($elements) { - if (isset($elements['#cache']['cid'])) { - return $elements['#cache']['cid']; - } - elseif (isset($elements['#cache']['keys'])) { - // Cache keys may either be static (just strings) or tokens (placeholders - // that are converted to static keys by the @cache_contexts service, - // depending on the request). - $cache_contexts = \Drupal::service("cache_contexts"); - $keys = $cache_contexts->convertTokensToKeys($elements['#cache']['keys']); - return implode(':', $keys); - } - return FALSE; -} - -/** * Retrieves the default properties for the defined element type. * * @param $type diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index eebd558..bd5fbc7 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Render; +use Drupal\Core\Cache\CacheContexts; use Drupal\Core\Controller\ControllerResolverInterface; use Drupal\Core\Theme\ThemeManagerInterface; use Drupal\Component\Utility\SafeMarkup; @@ -48,6 +49,13 @@ class Renderer implements RendererInterface { protected $requestStack; /** + * The cache contexts service. + * + * @var \Drupal\Core\Cache\CacheContexts + */ + protected $cacheContexts; + + /** * The stack containing bubbleable rendering metadata. * * @var \SplStack|null @@ -65,12 +73,15 @@ class Renderer implements RendererInterface { * The element info. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. + * @param \Drupal\Core\Cache\CacheContexts $cache_contexts + * The cache contexts service. */ - public function __construct(ControllerResolverInterface $controller_resolver, ThemeManagerInterface $theme, ElementInfoManagerInterface $element_info, RequestStack $request_stack) { + public function __construct(ControllerResolverInterface $controller_resolver, ThemeManagerInterface $theme, ElementInfoManagerInterface $element_info, RequestStack $request_stack, CacheContexts $cache_contexts) { $this->controllerResolver = $controller_resolver; $this->theme = $theme; $this->elementInfo = $element_info; $this->requestStack = $request_stack; + $this->cacheContexts = $cache_contexts; } /** @@ -462,7 +473,7 @@ protected function processPostRenderCache(array &$elements) { * @see ::saveToCache() */ protected function cacheGet(array $elements) { - if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = drupal_render_cid_create($elements)) { + if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = $this->createCacheID($elements)) { return FALSE; } $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render'; @@ -490,7 +501,7 @@ protected function cacheGet(array $elements) { */ protected function cacheSet(array &$elements) { // Create the cache ID for the element. - if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = drupal_render_cid_create($elements)) { + if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = $this->createCacheID($elements)) { return FALSE; } @@ -507,6 +518,32 @@ protected function cacheSet(array &$elements) { } /** + * Creates the cache ID for a renderable element. + * + * This creates the cache ID string, either by returning the #cache['cid'] + * property if present or by building the cache ID out of the #cache['keys']. + * + * @param array $elements + * A renderable array. + * + * @return string + * The cache ID string, or FALSE if the element may not be cached. + */ + protected function createCacheID(array $elements) { + if (isset($elements['#cache']['cid'])) { + return $elements['#cache']['cid']; + } + elseif (isset($elements['#cache']['keys'])) { + // Cache keys may either be static (just strings) or tokens (placeholders + // that are converted to static keys by the @cache_contexts service, + // depending on the request). + $keys = $this->cacheContexts->convertTokensToKeys($elements['#cache']['keys']); + return implode(':', $keys); + } + return FALSE; + } + + /** * {@inheritdoc} */ public function getCacheableRenderArray(array $elements) { diff --git a/core/lib/Drupal/Core/Render/RendererInterface.php b/core/lib/Drupal/Core/Render/RendererInterface.php index 43f4f44..579ce5e 100644 --- a/core/lib/Drupal/Core/Render/RendererInterface.php +++ b/core/lib/Drupal/Core/Render/RendererInterface.php @@ -97,8 +97,7 @@ public function renderPlain(&$elements); * 'keys' is set, the cache ID is created automatically from these keys. * Cache keys may either be static (just strings) or tokens * (placeholders that are converted to static keys by the - * @cache_contexts service, depending on the request). See - * drupal_render_cid_create(). + * 'cache_contexts' service, depending on the request). * - 'cid': Specify the cache ID directly. Either 'keys' or 'cid' is * required. If 'cid' is set, 'keys' is ignored. Use only if you have * special requirements. diff --git a/core/modules/block/src/Tests/BlockViewBuilderTest.php b/core/modules/block/src/Tests/BlockViewBuilderTest.php index 2ddbcab..b7a1083 100644 --- a/core/modules/block/src/Tests/BlockViewBuilderTest.php +++ b/core/modules/block/src/Tests/BlockViewBuilderTest.php @@ -160,7 +160,7 @@ protected function verifyRenderCacheHandling() { // Test that a cache entry is created. $build = $this->getBlockRenderArray(); - $cid = drupal_render_cid_create($build); + $cid = 'entity_view:block:test_block:en:core'; drupal_render($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); @@ -223,10 +223,10 @@ public function testBlockViewBuilderAlter() { )); $alter_add_key = $this->randomMachineName(); \Drupal::state()->set('block_test_view_alter_cache_key', $alter_add_key); + $cid = 'entity_view:block:test_block:en:core:' . $alter_add_key; $expected_keys = array_merge($default_keys, array($alter_add_key)); $build = $this->getBlockRenderArray(); $this->assertIdentical($expected_keys, $build['#cache']['keys'], 'An altered cacheable block has the expected cache keys.'); - $cid = drupal_render_cid_create(array('#cache' => array('keys' => $expected_keys))); $this->assertIdentical(drupal_render($build), ''); $cache_entry = $this->container->get('cache.render')->get($cid); $this->assertTrue($cache_entry, 'The block render element has been cached with the expected cache ID.'); @@ -242,7 +242,6 @@ public function testBlockViewBuilderAlter() { $build = $this->getBlockRenderArray(); sort($build['#cache']['tags']); $this->assertIdentical($expected_tags, $build['#cache']['tags'], 'An altered cacheable block has the expected cache tags.'); - $cid = drupal_render_cid_create(array('#cache' => array('keys' => $expected_keys))); $this->assertIdentical(drupal_render($build), ''); $cache_entry = $this->container->get('cache.render')->get($cid); $this->assertTrue($cache_entry, 'The block render element has been cached with the expected cache ID.'); @@ -272,6 +271,8 @@ public function testBlockViewBuilderAlter() { * @see \Drupal\block\Tests\BlockCacheTest */ public function testBlockViewBuilderCacheContexts() { + $cache_contexts = \Drupal::service("cache_contexts"); + // Force a request via GET so we can get drupal_render() cache working. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); @@ -282,7 +283,7 @@ public function testBlockViewBuilderCacheContexts() { 'max_age' => 600, )); $build = $this->getBlockRenderArray(); - $cid = drupal_render_cid_create($build); + $cid = implode(':', $build['#cache']['keys']); drupal_render($build); $this->assertTrue($this->container->get('cache.render', $cid), 'The block render element has been cached.'); @@ -293,7 +294,7 @@ public function testBlockViewBuilderCacheContexts() { )); $old_cid = $cid; $build = $this->getBlockRenderArray(); - $cid = drupal_render_cid_create($build); + $cid = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys'])); drupal_render($build); $this->assertTrue($this->container->get('cache.render', $cid), 'The block render element has been cached.'); $this->assertNotEqual($cid, $old_cid, 'The cache ID has changed.'); @@ -306,7 +307,7 @@ public function testBlockViewBuilderCacheContexts() { $this->container->set('cache_context.url', $temp_context); $old_cid = $cid; $build = $this->getBlockRenderArray(); - $cid = drupal_render_cid_create($build); + $cid = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys'])); drupal_render($build); $this->assertTrue($this->container->get('cache.render', $cid), 'The block render element has been cached.'); $this->assertNotEqual($cid, $old_cid, 'The cache ID has changed.'); diff --git a/core/modules/system/src/Tests/Common/RenderTest.php b/core/modules/system/src/Tests/Common/RenderTest.php index 6848dc9..b137d19 100644 --- a/core/modules/system/src/Tests/Common/RenderTest.php +++ b/core/modules/system/src/Tests/Common/RenderTest.php @@ -525,7 +525,7 @@ function testDrupalRenderPostRenderCache() { // GET request: validate cached data. $element = array('#cache' => array('cid' => 'post_render_cache_test_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; + $cached_element = \Drupal::cache('render')->get('post_render_cache_test_GET')->data; $expected_element = array( '#markup' => '

#cache enabled, GET

', '#attached' => $test_element['#attached'], @@ -566,8 +566,7 @@ function testDrupalRenderPostRenderCache() { $this->assertIdentical($element['#attached']['drupalSettings'], $expected_js_settings, '#attached is modified; both the original JavaScript setting and the one added by the #post_render_cache callback exist.'); // POST request: Ensure no data was cached. - $element = array('#cache' => array('cid' => 'post_render_cache_test_POST')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element)); + $cached_element = \Drupal::cache('render')->get('post_render_cache_test_POST'); $this->assertFalse($cached_element, 'No data is cached because this is a POST request.'); // Restore the previous request method. @@ -630,8 +629,7 @@ function testDrupalRenderChildrenPostRenderCache() { $this->assertIdentical($element['#attached']['drupalSettings'], $expected_js_settings, '#attached is modified; both the original JavaScript setting and the ones added by each #post_render_cache callback exist.'); // GET request: validate cached data. - $element = array('#cache' => $element['#cache']); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; + $cached_element = \Drupal::cache('render')->get('simpletest:drupal_render:children_post_render_cache')->data; $expected_element = array( '#attached' => array( 'drupalSettings' => [ @@ -697,11 +695,8 @@ function testDrupalRenderChildrenPostRenderCache() { $this->assertIdentical($element['#attached']['drupalSettings'], $expected_js_settings, '#attached is modified; both the original JavaScript setting and the ones added by each #post_render_cache callback exist.'); // GET request: validate cached data for both the parent and child. - $element = $test_element; - $element['#cache']['keys'] = array('simpletest', 'drupal_render', 'children_post_render_cache', 'nested_cache_parent'); - $element['child']['#cache']['keys'] = array('simpletest', 'drupal_render', 'children_post_render_cache', 'nested_cache_child'); - $cached_parent_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; - $cached_child_element = \Drupal::cache('render')->get(drupal_render_cid_create($element['child']))->data; + $cached_parent_element = \Drupal::cache('render')->get('simpletest:drupal_render:children_post_render_cache:nested_cache_parent')->data; + $cached_child_element = \Drupal::cache('render')->get('simpletest:drupal_render:children_post_render_cache:nested_cache_child')->data; $expected_parent_element = array( '#attached' => array( 'drupalSettings' => [ @@ -834,8 +829,7 @@ function testDrupalRenderRenderCachePlaceholder() { // GET request: validate cached data. $expected_token = $context['token']; - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; + $cached_element = \Drupal::cache('render')->get('render_cache_placeholder_test_GET')->data; // Parse unique token out of the cached markup. $dom = Html::load($cached_element['#markup']); $xpath = new \DOMXPath($dom); @@ -927,8 +921,7 @@ function testDrupalRenderChildElementRenderCachePlaceholder() { // GET request: validate cached data for child element. $expected_token = $context['token']; - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_child_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; + $cached_element = \Drupal::cache('render')->get('render_cache_placeholder_test_child_GET')->data; // Parse unique token out of the cached markup. $dom = Html::load($cached_element['#markup']); $xpath = new \DOMXPath($dom); @@ -953,8 +946,7 @@ function testDrupalRenderChildElementRenderCachePlaceholder() { $this->assertIdentical($cached_element, $expected_element, 'The correct data is cached for the child element: the stored #markup and #attached properties are not affected by #post_render_cache callbacks.'); // GET request: validate cached data (for the parent/entire render array). - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; + $cached_element = \Drupal::cache('render')->get('render_cache_placeholder_test_GET')->data; // Parse unique token out of the cached markup. $dom = Html::load($cached_element['#markup']); $xpath = new \DOMXPath($dom); @@ -981,8 +973,7 @@ function testDrupalRenderChildElementRenderCachePlaceholder() { // GET request: validate cached data. // Check the cache of the child element again after the parent has been // rendered. - $element = array('#cache' => array('cid' => 'render_cache_placeholder_test_child_GET')); - $cached_element = \Drupal::cache('render')->get(drupal_render_cid_create($element))->data; + $cached_element = \Drupal::cache('render')->get('render_cache_placeholder_test_child_GET')->data; // Verify that the child element contains the correct // render_cache_placeholder markup. $dom = Html::load($cached_element['#markup']); diff --git a/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php b/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php index d57b79c..2535bc7 100644 --- a/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php +++ b/core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php @@ -33,6 +33,8 @@ protected function setUp() { * Tests entity render cache handling. */ public function testEntityViewBuilderCache() { + $cache_contexts = \Drupal::service("cache_contexts"); + // Force a request via GET so we can get drupal_render() cache working. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); @@ -48,7 +50,7 @@ public function testEntityViewBuilderCache() { // Get a fully built entity view render array. $entity_test->save(); $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full'); - $cid = drupal_render_cid_create($build); + $cid = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys'])); $bin = $build['#cache']['bin']; // Mock the build array to not require the theme registry. @@ -79,6 +81,8 @@ public function testEntityViewBuilderCache() { * Tests entity render cache with references. */ public function testEntityViewBuilderCacheWithReferences() { + $cache_contexts = \Drupal::service("cache_contexts"); + // Force a request via GET so we can get drupal_render() cache working. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); @@ -95,7 +99,7 @@ public function testEntityViewBuilderCacheWithReferences() { // Get a fully built entity view render array for the referenced entity. $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test_reference, 'full'); - $cid_reference = drupal_render_cid_create($build); + $cid_reference = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys'])); $bin_reference = $build['#cache']['bin']; // Mock the build array to not require the theme registry. @@ -114,7 +118,7 @@ public function testEntityViewBuilderCacheWithReferences() { // Get a fully built entity view render array. $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full'); - $cid = drupal_render_cid_create($build); + $cid = implode(':', $cache_contexts->convertTokensToKeys($build['#cache']['keys'])); $bin = $build['#cache']['bin']; // Mock the build array to not require the theme registry.