diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index fb2f320..8bc7359 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -1252,6 +1252,10 @@ function comment_load($cid, $reset = FALSE) {
/**
* Gets the number of new comments for the current user and the specified node.
*
+ * @deprecated Use \Drupal\comment\CommentManager::countNewComments()
+ *
+ * @see \Drupal\comment\CommentManager::countNewComments()
+ *
* @param int $entity_id
* Entity ID of the entity to which the comments are attached.
* @param string $entity_type
@@ -1262,51 +1266,11 @@ function comment_load($cid, $reset = FALSE) {
* Time to count from (defaults to time of last user access
* to node).
*
- * @return
+ * @return int|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) {
- global $user;
-
- if ($user->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);
- }
- else {
- $function = $entity_type . '_last_viewed';
- if (function_exists($function)) {
- $timestamp = $function($entity_id);
- }
- else {
- // Default to 30 days ago.
- // @todo Remove once http://drupal.org/node/1029708 lands.
- $timestamp = COMMENT_NEW_LIMIT;
- }
- }
- }
- $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
-
- // Use the timestamp to retrieve the number of new comments.
- $query = db_select('comment', 'c');
- $query->addExpression('COUNT(cid)');
- $query->condition('c.entity_type', $entity_type)
- ->condition('c.entity_id', $entity_id)
- ->condition('c.status', COMMENT_PUBLISHED)
- ->condition('c.created', $timestamp, '>');
- if ($field_name) {
- // Limit to a particular field.
- $query->condition('c.field_id', $entity_type . '__' . $field_name);
- }
-
- return $query->execute()
- ->fetchField();
- }
- else {
- return FALSE;
- }
-
+ return \Drupal::service('comment.manager')->countNewComments($entity_id, $entity_type, $field_name, $timestamp);
}
/**
diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc
index eab4d3e..40e85cf 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] = comment_num_new($entity->id(), $entity->entityType());
+ $replacements[$original] = \Drupal::service('comment.manager')->countNewComments($entity->id(), $entity->entityType());
break;
}
}
diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php
index 4b3ab0b..35f723c 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentManager.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php
@@ -241,4 +241,64 @@ public function getFieldUIPageTitle($field_name) {
return String::checkPlain($sample_instance->label);
}
+ /**
+ * Returns the number of new comments for an user and the specified entity.
+ *
+ * @todo Help, this uses procedural code! We've to get rid of that.
+ *
+ * @param int $entity_id
+ * Entity ID of the entity to which the comments are attached.
+ * @param string $entity_type
+ * Entity type of the entity to which the comments are attached.
+ * @param string $field_name
+ * (optional) The field_name to count comments for. Defaults to NULL.
+ * @param int $timestamp
+ * Time to count from (defaults to time of last user access
+ * to node).
+ *
+ * @return int|FALSE
+ * The number of new comments or FALSE if the user is not logged in.
+ */
+ public function countNewComments($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) {
+ $user = \Drupal::currentUser();
+
+ if ($user->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) {
+ // Retrieve the timestamp when the current user last viewed this entity.
+ if (!$timestamp) {
+ if ($entity_type == 'node') {
+ $timestamp = history_read($entity_id);
+ }
+ else {
+ $function = $entity_type . '_last_viewed';
+ if (function_exists($function)) {
+ $timestamp = $function($entity_id);
+ }
+ else {
+ // Default to 30 days ago.
+ // @todo Remove once http://drupal.org/node/1029708 lands.
+ $timestamp = COMMENT_NEW_LIMIT;
+ }
+ }
+ }
+ $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
+
+ // Use the timestamp to retrieve the number of new comments.
+ $query = db_select('comment', 'c');
+ $query->addExpression('COUNT(cid)');
+ $query->condition('c.entity_type', $entity_type)
+ ->condition('c.entity_id', $entity_id)
+ ->condition('c.status', COMMENT_PUBLISHED)
+ ->condition('c.created', $timestamp, '>');
+ if ($field_name) {
+ // Limit to a particular field.
+ $query->condition('c.field_id', $entity_type . '__' . $field_name);
+ }
+
+ return $query->execute()
+ ->fetchField();
+ }
+ else {
+ return FALSE;
+ }
+ }
}
diff --git a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php
index 7775219..97ac2cf 100644
--- a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php
+++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php
@@ -330,7 +330,7 @@ public function renderNewCommentsNodeLinks(Request $request) {
$links = array();
foreach ($nids as $nid) {
$node = node_load($nid);
- $new = comment_num_new($node->id(), 'node');
+ $new = \Drupal::service('comment.manager')->countNewComments($node->id(), '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/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php
index 8f34413..dc8cf1d 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentFieldsTest.php
@@ -69,7 +69,7 @@ function testCommentInstallAfterContentModule() {
// Drop default comment field added in CommentTestBase::setup().
entity_load('field_entity', 'node.comment')->delete();
- if ($field = $this->container->get('field.info')->getField('node', 'comment_node_forum')) {
+ if ($field = $this->container->get('field.info')->getField('node', 'comment_forum')) {
$field->delete();
}
diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module
index 090d593..27d8c34 100644
--- a/core/modules/forum/forum.module
+++ b/core/modules/forum/forum.module
@@ -791,7 +791,7 @@ function template_preprocess_forum_topic_list(&$variables) {
$variables['topics'][$id]->new_url = '';
if ($topic->new_replies) {
$variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post in topic %title', '@count new posts in topic %title', array('%title' => $variables['topics'][$id]->label()));
- $variables['topics'][$id]->new_url = url('node/' . $topic->id(), array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic, 'comment_node_forum'), 'fragment' => 'new'));
+ $variables['topics'][$id]->new_url = url('node/' . $topic->id(), array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic, 'comment_forum'), 'fragment' => 'new'));
}
}
diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php
index 5d0e23c..0b42def 100644
--- a/core/modules/forum/lib/Drupal/forum/ForumManager.php
+++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php
@@ -312,7 +312,7 @@ protected function getTopicOrder($sortby) {
* Number of new comments.
*/
protected function numberNew($nid, $timestamp) {
- return comment_num_new($nid, $timestamp);
+ return \Drupal::service('comment.manager')->countNewComments($nid, 'node', 'comment_forum', $timestamp);
}
/**
diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc
index 16b391e..dc9b4c3 100644
--- a/core/modules/tracker/tracker.pages.inc
+++ b/core/modules/tracker/tracker.pages.inc
@@ -81,7 +81,7 @@ function tracker_page($account = NULL, $set_title = FALSE) {
if ($node->comment_count) {
$comments = $node->comment_count;
- if ($new = comment_num_new($node->id(), 'node')) {
+ if ($new = \Drupal::service('comment.manager')->countNewComments($node->id(), 'node')) {
$comments .= '
';
$comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new'));
}