.../lib/Drupal/comment/CommentViewBuilder.php | 33 ++++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php index 0692599..68ef009 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php +++ b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php @@ -167,9 +167,36 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langcode) { $defaults = parent::getBuildDefaults($entity, $view_mode, $langcode); - // Add the page number to the cache key if render caching is enabled. - if (isset($defaults['#cache']) && $this->request->query->has('page')) { - $defaults['#cache']['keys'][] = 'comment-pager-' . $this->request->query->get('page'); + $entity_type = $entity->entity_type->value; + $entity_id = $entity->entity_id->value; + // We need to be able to access the "per_page" comment field instance + // setting, and unfortunately that means we need the commented entity's + // bundle, which is a piece of metadata that is not available on the Comment + // entity. Hence we must actually load the commented entity to figure out + // the bundle. + // @todo THIS IS BAD! Is there a reason why we shouldn't add a "bundle" + // property (equal to the commented entity's bundle) on the Comment + // entity? + $commented_entity = entity_load($entity_type, $entity_id); + $bundle = $commented_entity->bundle(); + // A Comment entity's "field_id" property contains the commented entity type + // and the comment field name, separated by two underscores. To retrieve the + // actual field name, we must use string parsing. + // @todo THIS IS BAD! Is there a reason why we shouldn't add a "field_name" + // proeprty (equal to the commented entity's comment field name) on + // the Comment entity? + $field_name = substr($entity->field_id->value, strlen($entity_type) + 2); + + // Retrieve the "per_page" comment field instance setting. + $instance = $this->fieldInfo->getInstance($entity_type, $bundle, $field_name); + $comments_per_page = $instance->getSetting('per_page'); + + // Add the comments per page and the page number to the cache key if render + // caching is enabled. + if (isset($defaults['#cache'])) { + $defaults['#cache']['keys'][] = 'comments-per-page-' . $comments_per_page; + $page = $this->request->query->has('page') ? $this->request->query->get('page') : 0; + $defaults['#cache']['keys'][] = 'comment-pager-' . $page; } return $defaults;