Commit e57b339e authored by catch's avatar catch

Issue #2430341 by Wim Leers: Comment pager-dependent cache key should be a cache context

parent 9305a47f
......@@ -19,6 +19,11 @@ services:
arguments: ['@request_stack']
tags:
- { name: cache.context}
cache_context.pager:
class: Drupal\Core\Cache\PagerCacheContext
arguments: ['@request_stack']
tags:
- { name: cache.context}
cache_context.language:
class: Drupal\Core\Cache\LanguageCacheContext
arguments: ['@language_manager']
......
......@@ -225,9 +225,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
// choose to modify by: they must always be applied.
$context_labels = array();
$all_contexts = \Drupal::service("cache_contexts")->getLabels(TRUE);
foreach (array_keys(CacheContexts::parseTokens($this->getRequiredCacheContexts())) as $context) {
$context_labels[] = $all_contexts[$context];
unset($form['cache']['contexts']['#options'][$context]);
foreach (CacheContexts::parseTokens($this->getRequiredCacheContexts()) as $context) {
$context_id = $context[0];
$context_labels[] = $all_contexts[$context_id];
unset($form['cache']['contexts']['#options'][$context_id]);
}
$required_context_list = implode(', ', $context_labels);
$form['cache']['contexts']['#description'] .= ' ' . t('This block is <em>always</em> varied by the following contexts: %required-context-list.', array('%required-context-list' => $required_context_list));
......
......@@ -106,7 +106,8 @@ public function getLabels($include_calculated_cache_contexts = FALSE) {
*/
public function convertTokensToKeys(array $context_tokens) {
$keys = [];
foreach (static::parseTokens($context_tokens) as $context_id => $parameter) {
foreach (static::parseTokens($context_tokens) as $context) {
list($context_id, $parameter) = $context;
if (!in_array($context_id, $this->contexts)) {
throw new \InvalidArgumentException(String::format('"@context" is not a valid cache context ID.', ['@context' => $context_id]));
}
......@@ -136,9 +137,11 @@ protected function getService($context_id) {
* An array of cache context tokens.
*
* @return array
* An array with the parsed results, with the cache context IDs as keys, and
* the associated parameter as value (for a calculated cache context), or
* NULL if there is no parameter.
* An array with the parsed results, with each result being an array
* containing:
* 1. the cache context ID
* 2. the associated parameter (for a calculated cache context), or NULL if
* there is no parameter.
*/
public static function parseTokens(array $context_tokens) {
$contexts_with_parameters = [];
......@@ -148,7 +151,7 @@ public static function parseTokens(array $context_tokens) {
if (strpos($context, ':') !== FALSE) {
list($context_id, $parameter) = explode(':', $context, 2);
}
$contexts_with_parameters[$context_id] = $parameter;
$contexts_with_parameters[] = [$context_id, $parameter];
}
return $contexts_with_parameters;
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Cache\PagerCacheContext.
*/
namespace Drupal\Core\Cache;
/**
* Defines a cache context for "per page in a pager" caching.
*/
class PagerCacheContext implements CalculatedCacheContextInterface {
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t('Pager');
}
/**
* {@inheritdoc}
*/
public function getContext($pager_id) {
return 'pager.' . $pager_id . '.' . pager_find_page($pager_id);
}
}
......@@ -197,17 +197,18 @@ function comment_field_config_delete(FieldConfigInterface $field) {
/**
* Implements hook_entity_build_defaults_alter().
*
* @todo Remove this hook implementation in https://www.drupal.org/node/2433599
*/
function comment_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
// Get the corresponding display settings.
$display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
// Add the comment page number to the cache key if render caching is enabled.
if (isset($build['#cache']) && isset($build['#cache']['keys']) && \Drupal::request()->query->has('page')) {
if (isset($build['#cache']) && isset($build['#cache']['keys'])) {
foreach ($entity->getFieldDefinitions() as $field_name => $definition) {
if ($definition->getType() === 'comment' && ($display_options = $display->getComponent($field_name))) {
$pager_id = $display_options['settings']['pager_id'];
$page = pager_find_page($pager_id);
$build['#cache']['keys'][] = $field_name . '-pager-' . $page;
$build['#cache']['contexts'][] = 'pager:' . $pager_id;
}
}
}
......
......@@ -28,10 +28,15 @@ public function testConvertTokensToKeys() {
$new_keys = $cache_contexts->convertTokensToKeys([
'foo',
'baz:parameter',
'baz:parameterA',
'baz:parameterB',
]);
$expected = ['bar', 'baz.cnenzrgre'];
$expected = [
'bar',
'baz.cnenzrgreN',
'baz.cnenzrgreO',
];
$this->assertEquals($expected, $new_keys);
}
......
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