core/modules/comment/src/CommentViewBuilder.php | 9 ++++++--- core/modules/comment/src/Tests/CommentCacheTagsTest.php | 10 ++++++++-- .../comment/src/Tests/CommentDefaultFormatterCacheTagsTest.php | 5 +---- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index cf8b7b2..524349d 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -73,9 +73,12 @@ public function __construct(EntityTypeInterface $entity_type, EntityManagerInter protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langcode) { $build = parent::getBuildDefaults($entity, $view_mode, $langcode); - // If threading is enabled, don't render cache individual comments. - if ($entity->getCommentedEntity()->getFieldDefinition($entity->getFieldName())->getSetting('default_mode')) { - unset($build['#cache']); + // If threading is enabled, don't render cache individual comments, but do + // keep the cache tags, so they can bubble up. + if ($entity->getCommentedEntity()->getFieldDefinition($entity->getFieldName())->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED) { + $cache_tags = $build['#cache']['tags']; + $build['#cache'] = []; + $build['#cache']['tags'] = $cache_tags; } return $build; diff --git a/core/modules/comment/src/Tests/CommentCacheTagsTest.php b/core/modules/comment/src/Tests/CommentCacheTagsTest.php index c15bd29..ee69cc0 100644 --- a/core/modules/comment/src/Tests/CommentCacheTagsTest.php +++ b/core/modules/comment/src/Tests/CommentCacheTagsTest.php @@ -7,7 +7,9 @@ namespace Drupal\comment\Tests; +use Drupal\comment\CommentManagerInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\field\Entity\FieldConfig; use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase; /** @@ -44,7 +46,12 @@ protected function createEntity() { entity_test_create_bundle($bundle, NULL, 'entity_test'); // Create a comment field on this bundle. - \Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar'); + \Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar', 'comment'); + + // Display comments in a flat list; threaded comments are not render cached. + $field = FieldConfig::loadByName('entity_test', 'bar', 'comment'); + $field->settings['default_mode'] = CommentManagerInterface::COMMENT_MODE_FLAT; + $field->save(); // Create a "Camelids" test entity. $entity_test = entity_create('entity_test', array( @@ -78,7 +85,6 @@ protected function createEntity() { protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) { /** @var \Drupal\comment\CommentInterface $entity */ return array( - 'entity_test:1', 'filter_format:plain_text', 'user:' . $entity->getOwnerId(), 'user_view:1', diff --git a/core/modules/comment/src/Tests/CommentDefaultFormatterCacheTagsTest.php b/core/modules/comment/src/Tests/CommentDefaultFormatterCacheTagsTest.php index af169ff..e5c1c41 100644 --- a/core/modules/comment/src/Tests/CommentDefaultFormatterCacheTagsTest.php +++ b/core/modules/comment/src/Tests/CommentDefaultFormatterCacheTagsTest.php @@ -102,10 +102,7 @@ public function testCacheTags() { drupal_render($build); $expected_cache_tags = array( 'entity_test_view' => TRUE, - // The tags of the commented entity are duplicated in - // drupal_merge_cache_tags(). - // @todo Remove the duplicate in https://www.drupal.org/node/2340123 - 'entity_test' => array($commented_entity->id(), $commented_entity->id()), + 'entity_test' => array($commented_entity->id()), 'comment_view' => TRUE, 'comment' => array(1 => $comment->id()), 'filter_format' => array(