diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 2034cda..68acd32 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::getCountNewComments()
+ *
+ * @see \Drupal\comment\CommentManager::getCountNewComments()
+ *
* @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')->getCountNewComments($entity_type, $entity_id, $field_name, $timestamp);
}
/**
diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml
index e633fe5..4aeeedc 100644
--- a/core/modules/comment/comment.services.yml
+++ b/core/modules/comment/comment.services.yml
@@ -7,4 +7,4 @@ services:
comment.manager:
class: Drupal\comment\CommentManager
- arguments: ['@field.info', '@entity.manager']
+ arguments: ['@field.info', '@entity.manager', '@database', '@module_handler']
diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc
index eab4d3e..1b4d584 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')->getCountNewComments($entity->entityType(), $entity->id());
break;
}
}
diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php
index 4b3ab0b..f6341fb 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentManager.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php
@@ -10,6 +10,9 @@
use Drupal\Component\Utility\String;
use Drupal\field\FieldInfo;
use Drupal\Core\Entity\EntityManager;
+use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Database\Connection;
+use Drupal\Core\Session\AccountInterface;
/**
* Comment manager contains common functions to manage comment fields.
@@ -31,6 +34,20 @@ class CommentManager {
protected $entityManager;
/**
+ * Database connection
+ *
+ * @var \Drupal\Core\Database\Connection
+ */
+ protected $connection;
+
+ /**
+ * The module handler.
+ *
+ * @var \Drupal\Core\Extension\ModuleHandlerInterface
+ */
+ protected $moduleHandler;
+
+ /**
* Construct the CommentManager object.
*
* @param \Drupal\field\FieldInfo $field_info
@@ -38,9 +55,11 @@ class CommentManager {
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
*/
- public function __construct(FieldInfo $field_info, EntityManager $entity_manager) {
+ public function __construct(FieldInfo $field_info, EntityManager $entity_manager, Connection $connection, ModuleHandlerInterface $module_handler) {
$this->fieldInfo = $field_info;
$this->entityManager = $entity_manager;
+ $this->connection = $connection;
+ $this->moduleHandler = $module_handler;
}
/**
@@ -241,4 +260,65 @@ public function getFieldUIPageTitle($field_name) {
return String::checkPlain($sample_instance->label);
}
+ /**
+ * Returns the number of new comments for an user and the specified entity.
+ *
+ * @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 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).
+ * @param \Drupal\Core\Session\AccountInterface $account
+ * The user for which to count the new comments. Defaults to NULL.
+ *
+ * @return int|FALSE
+ * The number of new comments or FALSE if the user is not logged in.
+ */
+ public function getCountNewComments($entity_type, $entity_id, $field_name = NULL, $timestamp = 0, AccountInterface $account = NULL) {
+ if (!isset($account)) {
+ $account = \Drupal::currentUser();
+ }
+ if ($account->isAuthenticated() && $this->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 = $this->connection->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..0a7b394 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 = $this->commentManager->getCountNewComments('node', $node->id());
$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/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
index 451831a..ba93373 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
@@ -49,7 +49,6 @@ function testCommentTokenReplacement() {
// Add HTML to ensure that sanitation of some fields tested directly.
$comment->subject->value = '';
- $instance = field_info_instance('comment', 'body', 'comment_body');
// Generate and test sanitized tokens.
$tests = array();
@@ -104,10 +103,12 @@ function testCommentTokenReplacement() {
$tests['[entity:comment-count-new]'] = 2;
// Also test the deprecated legacy token.
$tests['[node:comment-count]'] = 2;
+ $tests['[node:comment-count-new]'] = 2;
foreach ($tests as $input => $expected) {
$output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->id));
$this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
}
}
+
}
diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module
index 4593c72..bf44710 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/forum.services.yml b/core/modules/forum/forum.services.yml
index 8ea4dac..ea5cba1 100644
--- a/core/modules/forum/forum.services.yml
+++ b/core/modules/forum/forum.services.yml
@@ -1,7 +1,7 @@
services:
forum_manager:
class: Drupal\forum\ForumManager
- arguments: ['@config.factory', '@plugin.manager.entity', '@database', '@field.info', '@string_translation']
+ arguments: ['@config.factory', '@plugin.manager.entity', '@database', '@field.info', '@string_translation', '@comment.manager']
forum.breadcrumb:
class: Drupal\forum\ForumBreadcrumbBuilder
arguments: ['@entity.manager', '@config.factory', '@forum_manager']
diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php
index 5d0e23c..b4fb55e 100644
--- a/core/modules/forum/lib/Drupal/forum/ForumManager.php
+++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php
@@ -11,6 +11,7 @@
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\StringTranslation\TranslationInterface;
+use Drupal\comment\CommentManager;
use Drupal\field\FieldInfo;
use Drupal\node\NodeInterface;
@@ -61,6 +62,13 @@ class ForumManager implements ForumManagerInterface {
protected $connection;
/**
+ * The comment manager service.
+ *
+ * @var \Drupal\comment\CommentManager
+ */
+ protected $commentManager;
+
+ /**
* Array of last post information keyed by forum (term) id.
*
* @var array
@@ -123,12 +131,13 @@ class ForumManager implements ForumManagerInterface {
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager service.
*/
- public function __construct(ConfigFactory $config_factory, EntityManager $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager) {
+ public function __construct(ConfigFactory $config_factory, EntityManager $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager, CommentManager $comment_manager) {
$this->configFactory = $config_factory;
$this->entityManager = $entity_manager;
$this->connection = $connection;
$this->fieldInfo = $field_info;
$this->translationManager = $translation_manager;
+ $this->commentManager = $comment_manager;
}
/**
@@ -312,7 +321,7 @@ protected function getTopicOrder($sortby) {
* Number of new comments.
*/
protected function numberNew($nid, $timestamp) {
- return comment_num_new($nid, $timestamp);
+ return $this->commentManager->getCountNewComments('node', $nid, 'comment_forum', $timestamp);
}
/**
diff --git a/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php b/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php
index 96675fb..0f3ad70 100644
--- a/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php
+++ b/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php
@@ -9,6 +9,11 @@
use Drupal\Tests\UnitTestCase;
+// @todo Remove once the constants are replaced with constants on classes.
+if (!defined('COMMENT_OPEN')) {
+ define('COMMENT_OPEN', 2);
+}
+
/**
* Tests the ForumManager.
*
@@ -75,12 +80,17 @@ public function testGetIndex() {
->disableOriginalConstructor()
->getMock();
+ $comment_manager = $this->getMockBuilder('\Drupal\comment\CommentManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+
$manager = $this->getMock('\Drupal\forum\ForumManager', array('getChildren'), array(
$config_factory,
$entity_manager,
$connection,
$field_info,
$translation_manager,
+ $comment_manager,
));
$manager->expects($this->once())
diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc
index 16b391e..500d981 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')->getCountNewComments('node', $node->id())) {
$comments .= '
';
$comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new'));
}