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.services.yml b/core/modules/comment/comment.services.yml index ec127e5..5a8051d 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -13,4 +13,4 @@ services: comment.manager: class: Drupal\comment\CommentManager - arguments: ['@field.info', '@entity.manager'] + arguments: ['@field.info', '@entity.manager', '@database', '@module_handler', '@current_user'] 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..6d59775 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,27 @@ class CommentManager { protected $entityManager; /** + * Database connection + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The current user service. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Construct the CommentManager object. * * @param \Drupal\field\FieldInfo $field_info @@ -38,9 +62,12 @@ 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, AccountInterface $current_user) { $this->fieldInfo = $field_info; $this->entityManager = $entity_manager; + $this->connection = $connection; + $this->moduleHandler = $module_handler; + $this->currentUser = $current_user; } /** @@ -241,4 +268,61 @@ 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 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) { + if ($this->currentUser->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..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/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..6564d36 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->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')); }