diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index b09bfa8..8730bf7 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -1117,47 +1117,13 @@ function comment_load($cid, $reset = FALSE) {
*
* @return int|false
* The number of new comments or FALSE if the user is not logged in.
+ *
+ * @deprecated Deprecated since Drupal 8.x-dev, to be removed in Drupal 8.0.
+ * Use \Drupal\comment\CommentManager::getCountNewComments().
*/
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 = \Drupal::entityManager()->getStorage($entity_type)->load($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 13846b1..ff9d756 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: ['@entity.manager', '@config.factory', '@string_translation', '@url_generator']
+ arguments: ['@entity.manager', '@entity.query', '@config.factory', '@string_translation', '@url_generator', '@module_handler', '@current_user']
comment.statistics:
class: Drupal\comment\CommentStatistics
diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc
index 058bdc5..b7ad13b 100644
--- a/core/modules/comment/comment.tokens.inc
+++ b/core/modules/comment/comment.tokens.inc
@@ -263,7 +263,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/src/CommentManager.php b/core/modules/comment/src/CommentManager.php
index 7bd9f0f..6ec96d3 100644
--- a/core/modules/comment/src/CommentManager.php
+++ b/core/modules/comment/src/CommentManager.php
@@ -12,6 +12,8 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\Query\QueryFactory;
+use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -31,6 +33,13 @@ class CommentManager implements CommentManagerInterface {
protected $entityManager;
/**
+ * The entity query factory.
+ *
+ * @var \Drupal\Core\Entity\Query\QueryFactory
+ */
+ protected $queryFactory;
+
+ /**
* Whether the DRUPAL_AUTHENTICATED_RID can post comments.
*
* @var bool
@@ -52,22 +61,45 @@ class CommentManager implements CommentManagerInterface {
protected $urlGenerator;
/**
+ * The module handler service.
+ *
+ * @var \Drupal\Core\Extension\ModuleHandlerInterface
+ */
+ protected $moduleHandler;
+
+ /**
+ * The current user.
+ *
+ * @var \Drupal\Core\Session\AccountInterface
+ */
+ protected $currentUser;
+
+ /**
* Construct the CommentManager object.
*
* @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\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service.
+ * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
+ * The module handler service.
+ * @param \Drupal\Core\Session\AccountInterface $current_user
+ * The current user.
*/
- public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator) {
+ public function __construct(EntityManagerInterface $entity_manager, QueryFactory $query_factory, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
$this->entityManager = $entity_manager;
+ $this->queryFactory = $query_factory;
$this->userConfig = $config_factory->get('user.settings');
$this->stringTranslation = $string_translation;
$this->urlGenerator = $url_generator;
+ $this->moduleHandler = $module_handler;
+ $this->currentUser = $current_user;
}
/**
@@ -276,4 +308,45 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
return '';
}
+ /*
+ * {@inheritdoc}
+ */
+ public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) {
+ // @todo Replace module handler with optional history service injection
+ // after http://drupal.org/node/2081585
+ if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) {
+ // Retrieve the timestamp at which the current user last viewed this entity.
+ if (!$timestamp) {
+ if ($entity->getEntityTypeId() == 'node') {
+ $timestamp = history_read($entity->id());
+ }
+ else {
+ $function = $entity->getEntityTypeId() . '_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->getEntityTypeId())
+ ->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->getEntityTypeId() . '__' . $field_name);
+ }
+
+ return $query->count()->execute();
+ }
+ return FALSE;
+ }
}
diff --git a/core/modules/comment/src/CommentManagerInterface.php b/core/modules/comment/src/CommentManagerInterface.php
index f302e18..a9a5295 100644
--- a/core/modules/comment/src/CommentManagerInterface.php
+++ b/core/modules/comment/src/CommentManagerInterface.php
@@ -6,8 +6,9 @@
*/
namespace Drupal\comment;
-use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Session\AccountInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
/**
@@ -96,4 +97,19 @@ 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.
+ *
+ * @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);
+
}
diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php
index e993e7a..ea4f03b 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -313,7 +313,8 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
*/
public static function attachNewCommentsLinkMetadata(array $element, array $context) {
// Build "X new comments" link metadata.
- $new = (int)comment_num_new($context['entity_id'], $context['entity_type']);
+ $new = \Drupal::service('comment.manager')
+ ->getCountNewComments(entity_load($context['entity_type'], $context['entity_id']));
// Early-return if there are zero new comments for the current user.
if ($new === 0) {
return $element;
diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php
index 1e73008..6e9fa2c 100644
--- a/core/modules/comment/src/Controller/CommentController.php
+++ b/core/modules/comment/src/Controller/CommentController.php
@@ -289,7 +289,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/src/Tests/CommentFieldsTest.php b/core/modules/comment/src/Tests/CommentFieldsTest.php
index d7657e1..8ba8853 100644
--- a/core/modules/comment/src/Tests/CommentFieldsTest.php
+++ b/core/modules/comment/src/Tests/CommentFieldsTest.php
@@ -78,7 +78,7 @@ function testCommentInstallAfterContentModule() {
// Drop default comment field added in CommentTestBase::setup().
FieldConfig::loadByName('node', 'comment')->delete();
- if ($field = FieldConfig::loadByName('node', 'comment_node_forum')) {
+ if ($field = FieldConfig::loadByName('node', 'comment_forum')) {
$field->delete();
}
diff --git a/core/modules/comment/src/Tests/CommentTokenReplaceTest.php b/core/modules/comment/src/Tests/CommentTokenReplaceTest.php
index 0d2a4b2..dcd3fb5 100644
--- a/core/modules/comment/src/Tests/CommentTokenReplaceTest.php
+++ b/core/modules/comment/src/Tests/CommentTokenReplaceTest.php
@@ -106,10 +106,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 1b9b8f8..0b79ddc 100644
--- a/core/modules/forum/forum.module
+++ b/core/modules/forum/forum.module
@@ -650,7 +650,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 68a135b..cea7335 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', '@string_translation']
+ arguments: ['@config.factory', '@entity.manager', '@database', '@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/src/ForumManager.php b/core/modules/forum/src/ForumManager.php
index 2f1dff9..c290c72 100644
--- a/core/modules/forum/src/ForumManager.php
+++ b/core/modules/forum/src/ForumManager.php
@@ -15,6 +15,7 @@
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\comment\CommentInterface;
+use Drupal\comment\CommentManagerInterface;
use Drupal\node\NodeInterface;
/**
@@ -65,6 +66,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
@@ -110,12 +118,15 @@ class ForumManager extends DependencySerialization implements ForumManagerInterf
* The current database connection.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* 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, TranslationInterface $string_translation) {
+ public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, TranslationInterface $string_translation, CommentManagerInterface $comment_manager) {
$this->configFactory = $config_factory;
$this->entityManager = $entity_manager;
$this->connection = $connection;
- $this->translationManager = $string_translation;
+ $this->stringTranslation = $string_translation;
+ $this->commentManager = $comment_manager;
}
/**
@@ -222,7 +233,7 @@ public function getTopics($tid, AccountInterface $account) {
}
else {
$history = $this->lastVisit($topic->id(), $account);
- $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);
}
}
@@ -286,21 +297,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/src/ForumManagerTest.php b/core/modules/forum/tests/src/ForumManagerTest.php
index c49a604..0b0c7fa 100644
--- a/core/modules/forum/tests/src/ForumManagerTest.php
+++ b/core/modules/forum/tests/src/ForumManagerTest.php
@@ -70,11 +70,16 @@ public function testGetIndex() {
->disableOriginalConstructor()
->getMock();
+ $comment_manager = $this->getMockBuilder('\Drupal\comment\CommentManagerInterface')
+ ->disableOriginalConstructor()
+ ->getMock();
+
$manager = $this->getMock('\Drupal\forum\ForumManager', array('getChildren'), array(
$config_factory,
$entity_manager,
$connection,
$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 30c7915..98f5255 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'));
}