diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 08e938db29b..93ba78fba69 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -1042,47 +1042,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_name', $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 58736341ee7..94540d16427 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 8d0620ea7d6..b34fdcbfbdb 100644
--- a/core/modules/comment/comment.tokens.inc
+++ b/core/modules/comment/comment.tokens.inc
@@ -264,7 +264,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 2b639a23ea5..3bc137920ae 100644
--- a/core/modules/comment/src/CommentManager.php
+++ b/core/modules/comment/src/CommentManager.php
@@ -13,6 +13,8 @@ use Drupal\Component\Utility\Unicode;
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;
@@ -33,6 +35,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.
*
@@ -54,23 +63,46 @@ 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;
}
/**
@@ -282,4 +314,45 @@ class CommentManager implements CommentManagerInterface {
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_name', $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 879706574aa..53ca6c81f2e 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;
/**
@@ -84,4 +85,19 @@ interface CommentManagerInterface {
*/
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 b7cba52325c..9b0838de70c 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -327,7 +327,8 @@ class CommentViewBuilder extends EntityViewBuilder {
*/
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 94fb25efffb..b56600d6286 100644
--- a/core/modules/comment/src/Controller/CommentController.php
+++ b/core/modules/comment/src/Controller/CommentController.php
@@ -289,7 +289,7 @@ class CommentController extends ControllerBase {
$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 5dd3bb7f6b4..43d5f8a7c73 100644
--- a/core/modules/comment/src/Tests/CommentFieldsTest.php
+++ b/core/modules/comment/src/Tests/CommentFieldsTest.php
@@ -78,7 +78,7 @@ class CommentFieldsTest extends CommentTestBase {
// 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 ec76fb45604..b2f0f8a0a12 100644
--- a/core/modules/comment/src/Tests/CommentTokenReplaceTest.php
+++ b/core/modules/comment/src/Tests/CommentTokenReplaceTest.php
@@ -106,10 +106,12 @@ class CommentTokenReplaceTest extends CommentTestBase {
$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 941c112d9f6..4271a3c4d4f 100644
--- a/core/modules/forum/forum.module
+++ b/core/modules/forum/forum.module
@@ -591,7 +591,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 f434c0cfb05..92f04af8d18 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 add0760846d..fc61f3260e4 100644
--- a/core/modules/forum/src/ForumManager.php
+++ b/core/modules/forum/src/ForumManager.php
@@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\comment\CommentManagerInterface;
use Drupal\node\NodeInterface;
/**
@@ -67,6 +68,13 @@ class ForumManager implements ForumManagerInterface {
*/
protected $connection;
+ /**
+ * The comment manager service.
+ *
+ * @var \Drupal\comment\CommentManagerInterface
+ */
+ protected $commentManager;
+
/**
* Array of last post information keyed by forum (term) id.
*
@@ -113,12 +121,15 @@ class ForumManager implements ForumManagerInterface {
* 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;
}
/**
@@ -225,7 +236,7 @@ class ForumManager implements ForumManagerInterface {
}
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);
}
}
@@ -288,21 +299,6 @@ class ForumManager implements ForumManagerInterface {
}
}
- /**
- * 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.
*
diff --git a/core/modules/forum/tests/src/ForumManagerTest.php b/core/modules/forum/tests/src/ForumManagerTest.php
index c49a604b4f3..0b0c7fa137f 100644
--- a/core/modules/forum/tests/src/ForumManagerTest.php
+++ b/core/modules/forum/tests/src/ForumManagerTest.php
@@ -70,11 +70,16 @@ class ForumManagerTest extends UnitTestCase {
->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 e2fbad18ce1..7baf977bb8b 100644
--- a/core/modules/tracker/tracker.pages.inc
+++ b/core/modules/tracker/tracker.pages.inc
@@ -76,7 +76,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'));
}