diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 07b177e..d7d8092 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -1134,6 +1134,8 @@ 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()
+ *
* @param int $entity_id
* Entity ID of the entity to which the comments are attached.
* @param string $entity_type
@@ -1147,45 +1149,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) {
- 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);
- }
- 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', CommentInterface::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;
- }
-
+ $entity = entity_load($entity_type, $entity_id);
+ return \Drupal::service('comment.manager')->getCountNewComments($entity, $field_name, $timestamp);
}
/**
diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml
index bcfb009..edbca77 100644
--- a/core/modules/comment/comment.services.yml
+++ b/core/modules/comment/comment.services.yml
@@ -7,7 +7,7 @@ services:
comment.manager:
class: Drupal\comment\CommentManager
- arguments: ['@field.info', '@entity.manager', '@current_user', '@config.factory', '@string_translation', '@url_generator']
+ arguments: ['@field.info', '@entity.manager', '@entity.query', '@current_user', '@config.factory', '@string_translation', '@url_generator']
comment.route_enhancer:
class: Drupal\comment\Routing\CommentBundleEnhancer
diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc
index b58230a..e042353 100644
--- a/core/modules/comment/comment.tokens.inc
+++ b/core/modules/comment/comment.tokens.inc
@@ -261,7 +261,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
break;
case 'comment-count-new':
- $replacements[$original] = comment_num_new($entity->id(), $entity->getEntityTypeId());
+ $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 6976e42..e4670f4 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentManager.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php
@@ -12,6 +12,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
@@ -37,6 +38,13 @@ class CommentManager implements CommentManagerInterface {
protected $entityManager;
/**
+ * The entity query factory.
+ *
+ * @var \Drupal\Core\Entity\Query\QueryFactory
+ */
+ protected $queryFactory;
+
+ /**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface $current_user
@@ -78,6 +86,8 @@ class CommentManager implements CommentManagerInterface {
* The field info service.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
+ * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
+ * The entity query factory.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
@@ -90,6 +100,7 @@ class CommentManager implements CommentManagerInterface {
public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory, TranslationInterface $translation_manager, UrlGeneratorInterface $url_generator) {
$this->fieldInfo = $field_info;
$this->entityManager = $entity_manager;
+ $this->queryFactory = $query_factory;
$this->currentUser = $current_user;
$this->userConfig = $config_factory->get('user.settings');
$this->translationManager = $translation_manager;
@@ -299,6 +310,48 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
}
/**
+ * {@inheritdoc}
+ */
+ 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 ($this->currentUser->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) {
+ // Retrieve the timestamp at which the current user last viewed this entity.
+ if (!$timestamp) {
+ if ($entity->entityType() == 'node') {
+ $timestamp = history_read($entity->id());
+ }
+ else {
+ $function = $entity->entityType() . '_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->queryFactory->get('comment')
+ ->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->entityType() . '__' . $field_name);
+ }
+
+ return $query->count()->execute();
+ }
+ return FALSE;
+ }
+
+ /**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
diff --git a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php
index 723d173..6501061 100644
--- a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php
+++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php
@@ -7,6 +7,7 @@
namespace Drupal\comment;
use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Session\AccountInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
@@ -107,4 +108,22 @@ public function getFieldUIPageTitle($commented_entity_type, $field_name);
*/
public function forbiddenMessage(EntityInterface $entity, $field_name);
+ /**
+ * Returns the number of new comments available on a given entity for a user.
+ *
+ * @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
+ * Time to count from. Defaults to time of last user access the entity.
+ * @param \Drupal\Core\Session\AccountInterface $account
+ * The user for which to count the new comments. Defaults to the current
+ * user.
+ *
+ * @return int|false
+ * The number of new comments or FALSE if the user is not authenticated.
+ */
+ 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 7a4115f..e2973ff 100644
--- a/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php
+++ b/core/modules/comment/lib/Drupal/comment/Controller/CommentController.php
@@ -303,7 +303,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);
$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/CommentTokenReplaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
index bd300fc..09079fd 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
@@ -105,10 +105,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 faca699..0c357da 100644
--- a/core/modules/forum/forum.module
+++ b/core/modules/forum/forum.module
@@ -662,7 +662,7 @@ function template_preprocess_forums(&$variables) {
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'));
}
// Build table rows from topics.
diff --git a/core/modules/forum/forum.services.yml b/core/modules/forum/forum.services.yml
index 3035062..cd4916d 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', '@entity.manager', '@database', '@field.info', '@string_translation']
+ arguments: ['@config.factory', '@entity.manager', '@database', '@field.info', '@string_translation', '@comment.manager']
forum.breadcrumb.node:
class: Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder
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 fc3f933..6253132 100644
--- a/core/modules/forum/lib/Drupal/forum/ForumManager.php
+++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php
@@ -14,6 +14,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\comment\CommentInterface;
+use Drupal\comment\CommentManagerInterface;
use Drupal\field\FieldInfo;
use Drupal\node\NodeInterface;
@@ -64,6 +65,13 @@ class ForumManager extends DependencySerialization implements ForumManagerInterf
protected $connection;
/**
+ * The comment manager service.
+ *
+ * @var \Drupal\comment\CommentManagerInterface
+ */
+ protected $commentManager;
+
+ /**
* Array of last post information keyed by forum (term) id.
*
* @var array
@@ -125,13 +133,16 @@ class ForumManager extends DependencySerialization implements ForumManagerInterf
* The field info service.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager service.
+ * @param \Drupal\comment\CommentManagerInterface $comment_manager
+ * The comment manager service.
*/
- public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager) {
+ public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, FieldInfo $field_info, TranslationInterface $translation_manager, CommentManagerInterface $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;
}
/**
@@ -302,21 +313,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 comment_num_new($nid, $timestamp);
- }
-
- /**
* Gets the last time the user viewed a node.
*
* @param int $nid
diff --git a/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php b/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php
index b0aa836..58cbbaa 100644
--- a/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php
+++ b/core/modules/forum/tests/Drupal/forum/Tests/ForumManagerTest.php
@@ -9,6 +9,12 @@
use Drupal\Tests\UnitTestCase;
+// @todo Remove once the constants are replaced with constants on classes.
+// https://drupal.org/node/2169361
+if (!defined('COMMENT_OPEN')) {
+ define('COMMENT_OPEN', 2);
+}
+
/**
* Tests the ForumManager.
*
@@ -74,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 8922672..1fb083b 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 = comment_num_new($node->id(), 'node')) {
+ if ($new = \Drupal::service('comment.manager')->getCountNewComments($node)) {
$comments .= '
';
$comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new'));
}