Skip to content
Snippets Groups Projects
Commit 27c9167e authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2318437 by Wim Leers, Gábor Hojtsy, geertvd: Replace the hardcoded...

Issue #2318437 by Wim Leers, Gábor Hojtsy, geertvd: Replace the hardcoded langcode key on blocks with the 'language' cache context
parent 26904bea
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -203,13 +203,14 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#options' => $period,
);
$contexts = \Drupal::service("cache_contexts")->getLabels();
// Blocks are always rendered in a "per theme" cache context. No need to
// show that option to the end user.
// Blocks are always rendered in the "per language" and "per theme" cache
// contexts. No need to show those options to the end user.
unset($contexts['cache_context.language']);
unset($contexts['cache_context.theme']);
$form['cache']['contexts'] = array(
'#type' => 'checkboxes',
'#title' => t('Vary by context'),
'#description' => t('The contexts this cached block must be varied by.'),
'#description' => t('The contexts this cached block must be varied by. <em>All</em> blocks are varied by language and theme.'),
'#default_value' => $this->configuration['cache']['contexts'],
'#options' => $contexts,
'#states' => array(
......
......@@ -48,6 +48,9 @@ public function getContext() {
$context_parts[] = $this->languageManager->getCurrentLanguage($type)->getId();
}
}
else {
$context_parts[] = $this->languageManager->getCurrentLanguage()->getId();
}
return implode(':', $context_parts);
}
......
......@@ -84,7 +84,8 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'entity_view',
'block',
$entity->id(),
$this->languageManager->getCurrentLanguage()->getId(),
// Blocks are always rendered in a "per language" cache context.
'cache_context.language',
// Blocks are always rendered in a "per theme" cache context.
'cache_context.theme',
);
......
......@@ -66,6 +66,7 @@ public function testBlockInterface() {
$period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever');
$contexts = \Drupal::service("cache_contexts")->getLabels();
unset($contexts['cache_context.theme']);
unset($contexts['cache_context.language']);
$expected_form = array(
'provider' => array(
'#type' => 'value',
......@@ -102,7 +103,7 @@ public function testBlockInterface() {
'contexts' => array(
'#type' => 'checkboxes',
'#title' => t('Vary by context'),
'#description' => t('The contexts this cached block must be varied by.'),
'#description' => t('The contexts this cached block must be varied by. <em>All</em> blocks are varied by language and theme.'),
'#default_value' => array(),
'#options' => $contexts,
'#states' => array(
......
......@@ -214,7 +214,7 @@ public function testBlockViewBuilderAlter() {
$request_method = $request->server->get('REQUEST_METHOD');
$request->setMethod('GET');
$default_keys = array('entity_view', 'block', 'test_block', 'en', 'cache_context.theme');
$default_keys = array('entity_view', 'block', 'test_block', 'cache_context.language', 'cache_context.theme');
$default_tags = array('block_view', 'config:block.block.test_block', 'block_plugin:test_cache');
// Advanced: cached block, but an alter hook adds an additional cache key.
......
......@@ -196,17 +196,4 @@ public function getCacheTags() {
return $cache_tags;
}
/**
* {@inheritdoc}
*/
protected function getRequiredCacheContexts() {
// The 'Site branding' block must be cached per theme and per language: the
// site logo, name and slogan are defined on a per-theme basis, and the name
// and slogan may be translated.
// We don't need to return 'cache_context.theme' also, because that cache
// context is automatically applied to all blocks.
// @see \Drupal\block\BlockViewBuilder::viewMultiple()
return array('cache_context.language');
}
}
......@@ -208,7 +208,7 @@ public function getCacheTags() {
protected function getRequiredCacheContexts() {
// Menu blocks must be cached per role: different roles may have access to
// different menu links.
return array('cache_context.user.roles', 'cache_context.language');
return array('cache_context.user.roles');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment