diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index f505c7d..612ee80 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1129,7 +1129,8 @@ function comment_load($cid, $reset = FALSE) { * The number of new comments or FALSE if the user is not logged in. */ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) { - return \Drupal::service('comment.manager')->getCountNewComments($entity_type, $entity_id, $field_name, $timestamp); + $entity = entity_load($entity_type, $entity_id); + return \Drupal::service('comment.manager')->getCountNewComments($entity, $field_name, $timestamp); } /** diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index 1b4d584..0f25b41 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -266,7 +266,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = break; case 'comment-count-new': - $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity->entityType(), $entity->id()); + $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity); break; } } diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index 793c5fe..095aca6 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManager.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php @@ -309,19 +309,19 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) { /** * {@inheritdoc} */ - public function getCountNewComments($entity_type, $entity_id, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL) { + public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL) { // @todo Replace module handler with optional history service injection // after http://drupal.org/node/2081585 if (\Drupal::currentUser()->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) { // Retrieve the timestamp at which the current user last viewed this entity. if (!$timestamp) { - if ($entity_type == 'node') { - $timestamp = history_read($entity_id); + if ($entity->entityType() == 'node') { + $timestamp = history_read($entity->id()); } else { - $function = $entity_type . '_last_viewed'; + $function = $entity->entityType() . '_last_viewed'; if (function_exists($function)) { - $timestamp = $function($entity_id); + $timestamp = $function($entity->id()); } else { // Default to 30 days ago. @@ -334,13 +334,13 @@ public function getCountNewComments($entity_type, $entity_id, $field_name = NULL // Use the timestamp to retrieve the number of new comments. $query = $this->queryFactory->get('comment') - ->condition('entity_type', $entity_type) - ->condition('entity_id', $entity_id) + ->condition('entity_type', $entity->entityType()) + ->condition('entity_id', $entity->id()) ->condition('created', $timestamp, '>') ->condition('status', CommentInterface::PUBLISHED); if ($field_name) { // Limit to a particular field. - $query->condition('field_id', $entity_type . '__' . $field_name); + $query->condition('field_id', $entity->entityType() . '__' . $field_name); } return $query->count()->execute(); diff --git a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php index 96ffee2..029ed3c 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php @@ -109,10 +109,8 @@ public function forbiddenMessage(EntityInterface $entity, $field_name); /** * Returns the number of new comments available on a given entity for a user. * - * @param string $entity_type - * Entity type of the entity to which the comments are attached. - * @param int $entity_id - * Entity ID of the entity to which the comments are attached. + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to which the comments are attached to. * @param string $field_name * The field_name to count comments for. Defaults to any field. * @param int $timestamp @@ -124,6 +122,6 @@ public function forbiddenMessage(EntityInterface $entity, $field_name); * @return int|false * The number of new comments or FALSE if the user is not authenticated. */ - public function getCountNewComments($entity_type, $entity_id, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL); + public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL); } diff --git a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php index 1d8341e..9bae041 100644 --- a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php +++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php @@ -299,7 +299,7 @@ public function renderNewCommentsNodeLinks(Request $request) { $links = array(); foreach ($nids as $nid) { $node = node_load($nid); - $new = $this->commentManager->getCountNewComments('node', $node->id()); + $new = $this->commentManager->getCountNewComments($node); $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node); $links[$nid] = array( 'new_comment_count' => (int) $new, diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index 8d70641..07fad60 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -250,7 +250,7 @@ public function getTopics($tid) { } else { $history = $this->lastVisit($topic->id()); - $topic->new_replies = $this->numberNew($topic->id(), $history); + $topic->new_replies = $this->commentManager->getCountNewComments($topic, 'comment_forum', $history); $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history); } } @@ -314,21 +314,6 @@ protected function getTopicOrder($sortby) { } /** - * Wraps comment_num_new() in a method. - * - * @param int $nid - * Node ID. - * @param int $timestamp - * Timestamp of last read. - * - * @return int - * Number of new comments. - */ - protected function numberNew($nid, $timestamp) { - return $this->commentManager->getCountNewComments('node', $nid, 'comment_forum', $timestamp); - } - - /** * Gets the last time the user viewed a node. * * @param int $nid diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc index 80dfdb0..72987e6 100644 --- a/core/modules/tracker/tracker.pages.inc +++ b/core/modules/tracker/tracker.pages.inc @@ -74,7 +74,7 @@ function tracker_page($account = NULL) { if ($node->comment_count) { $comments = $node->comment_count; - if ($new = \Drupal::service('comment.manager')->getCountNewComments('node', $node->id())) { + if ($new = \Drupal::service('comment.manager')->getCountNewComments($node)) { $comments .= '
'; $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new')); }