Skip to content
Snippets Groups Projects

Resolve #2232375 "Make language switcher nocache"

Files
2
@@ -6,6 +6,7 @@
@@ -6,6 +6,7 @@
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableMetadata;
 
use Drupal\Core\Cache\CacheOptionalInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
@@ -50,7 +51,6 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod
@@ -50,7 +51,6 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod
// @see template_preprocess_block().
// @see template_preprocess_block().
$build[$entity_id] = [
$build[$entity_id] = [
'#cache' => [
'#cache' => [
'keys' => ['entity_view', 'block', $entity->id()],
'contexts' => Cache::mergeContexts(
'contexts' => Cache::mergeContexts(
$entity->getCacheContexts(),
$entity->getCacheContexts(),
$plugin->getCacheContexts()
$plugin->getCacheContexts()
@@ -61,6 +61,12 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod
@@ -61,6 +61,12 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod
'#weight' => $entity->getWeight(),
'#weight' => $entity->getWeight(),
];
];
 
// Only add cache keys if the block plugin does not implement
 
// CacheOptionalInterface.
 
if (!$plugin instanceof CacheOptionalInterface) {
 
$build[$entity_id]['#cache']['keys'] = ['entity_view', 'block', $entity->id()];
 
}
 
// Allow altering of cacheability metadata or setting #create_placeholder.
// Allow altering of cacheability metadata or setting #create_placeholder.
$this->moduleHandler->alter(['block_build', "block_build_" . $plugin->getBaseId()], $build[$entity_id], $plugin);
$this->moduleHandler->alter(['block_build', "block_build_" . $plugin->getBaseId()], $build[$entity_id], $plugin);
Loading