From ec7c5ca6c39910555a54dbe880b0d2657869e108 Mon Sep 17 00:00:00 2001 From: catch <6915-catch@users.noreply.drupalcode.org> Date: Wed, 5 Mar 2025 14:21:18 +0000 Subject: [PATCH] Issue #3509565 by kristiaanvandeneynde: ConfigTranslationController::itemPage() does not specify cacheable metadata --- .../Controller/ConfigTranslationController.php | 17 +++++++++++++++-- .../ConfigTranslationUiModulesTest.php | 3 +-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/core/modules/config_translation/src/Controller/ConfigTranslationController.php b/core/modules/config_translation/src/Controller/ConfigTranslationController.php index f6ec1a474aaa..1b8287d80cf1 100644 --- a/core/modules/config_translation/src/Controller/ConfigTranslationController.php +++ b/core/modules/config_translation/src/Controller/ConfigTranslationController.php @@ -6,6 +6,7 @@ use Drupal\config_translation\ConfigMapperManagerInterface; use Drupal\config_translation\Exception\ConfigMapperLanguageException; use Drupal\Core\Access\AccessManagerInterface; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageInterface; @@ -131,6 +132,8 @@ public static function create(ContainerInterface $container) { * Page render array. */ public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) { + $cacheable_metadata = new CacheableMetadata(); + /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ $mapper = $this->configMapperManager->createInstance($plugin_id); $mapper->populateFromRouteMatch($route_match); @@ -204,11 +207,12 @@ public function itemPage(Request $request, RouteMatchInterface $route_match, $pl // Check access for the path/route for editing, so we can decide to // include a link to edit or not. - $edit_access = $this->accessManager->checkNamedRoute($mapper->getBaseRouteName(), $route_match->getRawParameters()->all(), $this->account); + $edit_access = $this->accessManager->checkNamedRoute($mapper->getBaseRouteName(), $route_match->getRawParameters()->all(), $this->account, TRUE); + $cacheable_metadata->addCacheableDependency($edit_access); // Build list of operations. $operations = []; - if ($edit_access) { + if ($edit_access->isAllowed()) { $operations['edit'] = [ 'title' => $this->t('Edit'), 'url' => Url::fromRoute($mapper->getBaseRouteName(), $mapper->getBaseRouteParameters(), ['query' => ['destination' => $mapper->getOverviewPath()]]), @@ -218,6 +222,14 @@ public function itemPage(Request $request, RouteMatchInterface $route_match, $pl else { $language_name = $language->getName(); + // The translation check below might change if the configs change, so we + // need to add their cache tags. + $cache_tags = []; + foreach ($mapper->getConfigNames() as $configName) { + $cache_tags[] = "config:$configName"; + } + $cacheable_metadata->addCacheTags($cache_tags); + $operations = []; // If no translation exists for this language, link to add one. if (!$mapper->hasTranslation($language)) { @@ -274,6 +286,7 @@ public function itemPage(Request $request, RouteMatchInterface $route_match, $pl '#attached' => ['library' => ['core/drupal.dialog.ajax']], ]; } + $cacheable_metadata->applyTo($page); return $page; } diff --git a/core/modules/config_translation/tests/src/Functional/ConfigTranslationUiModulesTest.php b/core/modules/config_translation/tests/src/Functional/ConfigTranslationUiModulesTest.php index ccf0475d1312..eb2af2b21451 100644 --- a/core/modules/config_translation/tests/src/Functional/ConfigTranslationUiModulesTest.php +++ b/core/modules/config_translation/tests/src/Functional/ConfigTranslationUiModulesTest.php @@ -392,9 +392,8 @@ public function testNodeFieldTranslation(): void { 'entity_type' => 'node', 'type' => 'text', ]); - - $field_storage->setSetting('translatable_storage_setting', 'translatable_storage_setting'); $field_storage->save(); + $field = FieldConfig::create([ 'field_name' => $field_name, 'entity_type' => 'node', -- GitLab