diff --git a/core/lib/Drupal/Core/Entity/EntityForm.php b/core/lib/Drupal/Core/Entity/EntityForm.php index 6d61d0c..e216099 100644 --- a/core/lib/Drupal/Core/Entity/EntityForm.php +++ b/core/lib/Drupal/Core/Entity/EntityForm.php @@ -101,6 +101,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { $this->init($form_state); } + // Ensure that forms have the correct cacheable metadata so they can be + // cached. + \Drupal::service('renderer')->addCacheableDependency($form, $this->entity); + // Retrieve the form array using the possibly updated entity in form state. $form = $this->form($form, $form_state); diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index 6923220..d9ae407 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -71,11 +71,9 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco ->getFieldDefinition($entity->getFieldName()) ->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED; // If threading is enabled, don't render cache individual comments, but do - // keep the cache tags, so they can bubble up. + // keep the cacheable metadata, so it can bubble up. if ($build['#comment_threaded']) { - $cache_tags = $build['#cache']['tags']; - $build['#cache'] = []; - $build['#cache']['tags'] = $cache_tags; + unset($build['#cache']['keys']); } return $build; diff --git a/core/modules/comment/src/Tests/CommentTranslationUITest.php b/core/modules/comment/src/Tests/CommentTranslationUITest.php index cfb63c8..7edf1af 100644 --- a/core/modules/comment/src/Tests/CommentTranslationUITest.php +++ b/core/modules/comment/src/Tests/CommentTranslationUITest.php @@ -33,6 +33,13 @@ class CommentTranslationUITest extends ContentTranslationUITestBase { protected $adminUser; /** + * Default cache contexts expected on a non-translated entity. + * + * @var array + */ + protected $defaultCacheContexts = ['languages:language_interface', 'theme' , 'user.permissions', 'timezone', 'url.query_args.pagers:0', 'user.roles']; + + /** * Modules to install. * * @var array diff --git a/core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php b/core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php index 4974edb..a9c87ec 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php @@ -7,6 +7,7 @@ namespace Drupal\content_translation\Tests; +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageInterface; @@ -37,6 +38,15 @@ protected $testLanguageSelector = TRUE; /** + * Default cache contexts expected on a non-translated entity. + * + * Cache contexts will not be checked if this list is empty. + * + * @var array + */ + protected $defaultCacheContexts = ['languages:language_interface', 'theme' , 'user.permissions']; + + /** * Tests the basic translation UI. */ function testTranslationUI() { @@ -67,6 +77,11 @@ protected function doTestBasicTranslation() { $this->assertTrue($entity, 'Entity found in the database.'); $this->drupalGet($entity->urlInfo()); $this->assertResponse(200, 'Entity URL is valid.'); + + // Ensure that the content language cache context is not yet added to the + // page. + $this->assertCacheContexts($this->defaultCacheContexts); + $this->drupalGet($entity->urlInfo('drupal:content-translation-overview')); $this->assertNoText('Source language', 'Source language column correctly hidden.'); @@ -90,11 +105,11 @@ protected function doTestBasicTranslation() { ], array('language' => $language)); $this->drupalPostForm($add_url, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode)); - // Ensure that the content languge cache context is not yet added to the + // Ensure that the content language cache context is not yet added to the // page. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); $this->drupalGet($entity->urlInfo()); - $this->assertCacheContexts(['languages:language_interface', 'theme' , 'user.permissions']); + $this->assertCacheContexts(Cache::mergeContexts(['languages:language_content'], $this->defaultCacheContexts)); // Reset the cache of the entity, so that the new translation gets the // updated values. @@ -156,12 +171,6 @@ protected function doTestBasicTranslation() { ], array('language' => $language)); $this->drupalPostForm($add_url, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode)); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); - $this->drupalGet($entity->urlInfo()); - - // Ensure that the language content cache context has been added as there - // are translations. - $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme' , 'user.permissions']); - $this->drupalGet($entity->urlInfo('drupal:content-translation-overview')); $this->assertText('Source language', 'Source language column correctly shown.'); diff --git a/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php b/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php index b068808..faab9e6 100644 --- a/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php +++ b/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php @@ -18,6 +18,13 @@ class MenuLinkContentTranslationUITest extends ContentTranslationUITestBase { /** + * Default cache contexts expected on a non-translated entity. + * + * @var array + */ + protected $defaultCacheContexts = ['languages:language_interface', 'theme' , 'user.permissions', 'user.roles:authenticated']; + + /** * Modules to enable. * * @var array diff --git a/core/modules/node/src/Tests/NodeTranslationUITest.php b/core/modules/node/src/Tests/NodeTranslationUITest.php index 287920b..5476ed9 100644 --- a/core/modules/node/src/Tests/NodeTranslationUITest.php +++ b/core/modules/node/src/Tests/NodeTranslationUITest.php @@ -22,6 +22,23 @@ class NodeTranslationUITest extends ContentTranslationUITestBase { /** + * Default cache contexts expected on a non-translated entity. + * + * @var array + */ + protected $defaultCacheContexts = [ + 'languages:language_interface', + 'theme', + 'user.permissions', + 'route.menu_active_trails:account', + 'route.menu_active_trails:footer', + 'route.menu_active_trails:main', + 'route.menu_active_trails:tools', + 'timezone', + 'user.roles' + ]; + + /** * Modules to enable. * * @var array diff --git a/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php b/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php index 3fc8232..007baa4 100644 --- a/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php +++ b/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php @@ -19,6 +19,13 @@ class ShortcutTranslationUITest extends ContentTranslationUITestBase { /** + * Default cache contexts expected on a non-translated entity. + * + * @var array + */ + protected $defaultCacheContexts = ['languages:language_interface', 'theme' , 'user']; + + /** * Modules to enable. * * @var array