diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index 2466231..f9b1f93 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -331,17 +331,27 @@ public function renderNewCommentsNodeLinks(Request $request) { } // Only handle up to 100 nodes. $nids = array_slice($nids, 0, 100); + /** @var \Drupal\node\NodeInterface[] $nodes */ + $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids); + /** @var \Drupal\comment\CommentStorageInterface $comment_storage */ + $comment_storage = $this->entityTypeManager->getStorage('comment'); $links = array(); - foreach ($nids as $nid) { - $node = $this->entityManager->getStorage('node')->load($nid); - $new = $this->commentManager->getCountNewComments($node); - $page_number = $this->entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name); + foreach ($nodes as $nid => $node) { + if (!$node->hasField($field_name)) { + // Field name required to calculate comment's page. + continue; + } + $new = $this->commentManager->getCountNewComments($node, $field_name); + if (!$new) { + // No history module enabled or user is not authorized. + continue; + } + $page_number = $comment_storage->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name); $query = $page_number ? array('page' => $page_number) : NULL; $links[$nid] = array( 'new_comment_count' => (int) $new, - 'first_new_comment_link' => $this->getUrlGenerator()->generateFromRoute('entity.node.canonical', array('node' => $node->id()), array('query' => $query, 'fragment' => 'new')), + 'first_new_comment_link' => $node->toUrl('canonical', array('query' => $query, 'fragment' => 'new')), ); }