diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php index 6874543..83dd5de 100644 --- a/core/lib/Drupal/Core/Block/BlockBase.php +++ b/core/lib/Drupal/Core/Block/BlockBase.php @@ -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. All blocks are varied by language and theme.'), '#default_value' => $this->configuration['cache']['contexts'], '#options' => $contexts, '#states' => array( diff --git a/core/lib/Drupal/Core/Cache/LanguageCacheContext.php b/core/lib/Drupal/Core/Cache/LanguageCacheContext.php index 0a3f16c..b9d19a7 100644 --- a/core/lib/Drupal/Core/Cache/LanguageCacheContext.php +++ b/core/lib/Drupal/Core/Cache/LanguageCacheContext.php @@ -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); } diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index 6f2a71d..5dcbd9f 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -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', ); diff --git a/core/modules/block/src/Tests/BlockInterfaceTest.php b/core/modules/block/src/Tests/BlockInterfaceTest.php index 769ed89..11b6762 100644 --- a/core/modules/block/src/Tests/BlockInterfaceTest.php +++ b/core/modules/block/src/Tests/BlockInterfaceTest.php @@ -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. All blocks are varied by language and theme.'), '#default_value' => array(), '#options' => $contexts, '#states' => array( diff --git a/core/modules/block/src/Tests/BlockViewBuilderTest.php b/core/modules/block/src/Tests/BlockViewBuilderTest.php index ce0d2b4..972ded1 100644 --- a/core/modules/block/src/Tests/BlockViewBuilderTest.php +++ b/core/modules/block/src/Tests/BlockViewBuilderTest.php @@ -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. diff --git a/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php b/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php index 6d10aff..1d99bb3 100644 --- a/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php @@ -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'); - } - } diff --git a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php index 819ff55..0bfade4 100644 --- a/core/modules/system/src/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMenuBlock.php @@ -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'); } }