From 4a99d0f575bc884602a4dde559e0edff9acfe68b Mon Sep 17 00:00:00 2001 From: "samit.khulve1" <samit.khulve1@srijan.net> Date: Thu, 10 Oct 2024 12:51:00 +0530 Subject: [PATCH 1/6] 2232375: Make language switcher block cacheable --- .../language/src/Plugin/Block/LanguageBlock.php | 13 ++++--------- .../tests/src/Functional/LanguageSwitchingTest.php | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index 8b146db4adbd..f6ccb4b1692b 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -113,16 +113,11 @@ public function build() { '#set_active_class' => TRUE, ]; } - return $build; - } - /** - * {@inheritdoc} - * - * @todo Make cacheable in https://www.drupal.org/node/2232375. - */ - public function getCacheMaxAge() { - return 0; + // Add cache contexts for things that might cause links to change. + $build['#cache']['contexts'] = ['user.permissions', 'url.path', 'url.query_args', 'languages:' . $this->getDerivativeId()]; + + return $build; } } diff --git a/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php index 32ec3a2c6e51..fa1794c234ae 100644 --- a/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php +++ b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php @@ -97,7 +97,7 @@ public function testLanguageBlock(): void { // @todo This is testing the current behavior with the big_pipe module // enabled. This behavior is a bug will be fixed in // https://www.drupal.org/project/drupal/issues/3349201. - $this->doTestLanguageBlock404($block->label(), '<front>'); + $this->doTestLanguageBlock404($block->label(), 'system/404'); } /** -- GitLab From a91a42321fefa56e176779078a7703a8bd7ddc2a Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher <saschagros@gmail.com> Date: Tue, 4 Feb 2025 00:17:12 +0100 Subject: [PATCH 2/6] Support CacheOptionalInterface in BlockViewBuilder --- core/modules/block/src/BlockViewBuilder.php | 8 +++++++- core/modules/language/src/Plugin/Block/LanguageBlock.php | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index f33092de869e..1b77690e8151 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -6,6 +6,7 @@ use Drupal\Core\Block\TitleBlockPluginInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; +use Drupal\Core\Cache\CacheOptionalInterface; use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -50,7 +51,6 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod // @see template_preprocess_block(). $build[$entity_id] = [ '#cache' => [ - 'keys' => ['entity_view', 'block', $entity->id()], 'contexts' => Cache::mergeContexts( $entity->getCacheContexts(), $plugin->getCacheContexts() @@ -61,6 +61,12 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod '#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. $this->moduleHandler->alter(['block_build', "block_build_" . $plugin->getBaseId()], $build[$entity_id], $plugin); diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index f6ccb4b1692b..bc47559c7859 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -5,6 +5,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\CacheOptionalInterface; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Language\LanguageManagerInterface; @@ -23,7 +24,7 @@ category: new TranslatableMarkup("System"), deriver: LanguageBlockDeriver::class )] -class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface { +class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface, CacheOptionalInterface { /** * The language manager. -- GitLab From 04cdeabaa9dfa84691b7197d97d76e19cfb376f2 Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher <saschagros@gmail.com> Date: Fri, 21 Mar 2025 20:58:24 +0100 Subject: [PATCH 3/6] make block cacheable --- .../language/src/Plugin/Block/LanguageBlock.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index bc47559c7859..698af1da1ebd 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -5,7 +5,6 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; -use Drupal\Core\Cache\CacheOptionalInterface; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Language\LanguageManagerInterface; @@ -24,7 +23,7 @@ category: new TranslatableMarkup("System"), deriver: LanguageBlockDeriver::class )] -class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface, CacheOptionalInterface { +class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The language manager. @@ -121,4 +120,12 @@ public function build() { return $build; } + /** + * {@inheritdoc} + */ + public function createPlaceholder(): bool { + return TRUE; + } + + } -- GitLab From 2f42cfbfc5da8300922939fe77ced9a503bdf90a Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher <saschagros@gmail.com> Date: Fri, 21 Mar 2025 21:11:06 +0100 Subject: [PATCH 4/6] phpcs --- core/modules/language/src/Plugin/Block/LanguageBlock.php | 1 - 1 file changed, 1 deletion(-) diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index 698af1da1ebd..47bf7b606e4f 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -127,5 +127,4 @@ public function createPlaceholder(): bool { return TRUE; } - } -- GitLab From 84fa6fd2d2132b23b45c2a872c68180698b58388 Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher <saschagros@gmail.com> Date: Fri, 21 Mar 2025 21:26:14 +0100 Subject: [PATCH 5/6] revert laanguage switching test --- .../language/tests/src/Functional/LanguageSwitchingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php index fa1794c234ae..32ec3a2c6e51 100644 --- a/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php +++ b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php @@ -97,7 +97,7 @@ public function testLanguageBlock(): void { // @todo This is testing the current behavior with the big_pipe module // enabled. This behavior is a bug will be fixed in // https://www.drupal.org/project/drupal/issues/3349201. - $this->doTestLanguageBlock404($block->label(), 'system/404'); + $this->doTestLanguageBlock404($block->label(), '<front>'); } /** -- GitLab From bc8f6884e687ded78d6754706469092cd2eb391d Mon Sep 17 00:00:00 2001 From: Sascha Grossenbacher <saschagros@gmail.com> Date: Sat, 22 Mar 2025 09:58:46 +0100 Subject: [PATCH 6/6] placeholder and nocache --- core/modules/language/src/Plugin/Block/LanguageBlock.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index 47bf7b606e4f..a9b3c1d5d35d 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -5,6 +5,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\CacheOptionalInterface; use Drupal\Core\Path\PathMatcherInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Language\LanguageManagerInterface; @@ -23,7 +24,7 @@ category: new TranslatableMarkup("System"), deriver: LanguageBlockDeriver::class )] -class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface { +class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface, CacheOptionalInterface { /** * The language manager. -- GitLab