Commit f0d4b5ad authored by alexpott's avatar alexpott

Revert "Issue #2097123 by slashrsm, roderik, mgifford, pfrenssen, andypost,...

Revert "Issue #2097123 by slashrsm, roderik, mgifford, pfrenssen, andypost, das-peter | larowlan: Deprecate comment_num_new() in favour of method on CommentManager."

This reverts commit 6532718c.
parent 6532718c
...@@ -1042,13 +1042,47 @@ function comment_load($cid, $reset = FALSE) { ...@@ -1042,13 +1042,47 @@ function comment_load($cid, $reset = FALSE) {
* *
* @return int|false * @return int|false
* The number of new comments or FALSE if the user is not logged in. * 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) { function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) {
$entity = \Drupal::entityManager()->getStorage($entity_type)->load($entity_id); if (\Drupal::currentUser()->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) {
return \Drupal::service('comment.manager')->getCountNewComments($entity, $field_name, $timestamp); // 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;
}
} }
/** /**
......
...@@ -7,7 +7,7 @@ services: ...@@ -7,7 +7,7 @@ services:
comment.manager: comment.manager:
class: Drupal\comment\CommentManager class: Drupal\comment\CommentManager
arguments: ['@entity.manager', '@entity.query', '@config.factory', '@string_translation', '@url_generator', '@module_handler', '@current_user'] arguments: ['@entity.manager', '@config.factory', '@string_translation', '@url_generator']
comment.statistics: comment.statistics:
class: Drupal\comment\CommentStatistics class: Drupal\comment\CommentStatistics
......
...@@ -264,7 +264,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = ...@@ -264,7 +264,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
break; break;
case 'comment-count-new': case 'comment-count-new':
$replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity); $replacements[$original] = comment_num_new($entity->id(), $entity->getEntityTypeId());
break; break;
} }
} }
......
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
...@@ -35,13 +33,6 @@ class CommentManager implements CommentManagerInterface { ...@@ -35,13 +33,6 @@ class CommentManager implements CommentManagerInterface {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity query factory.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $queryFactory;
/** /**
* Whether the DRUPAL_AUTHENTICATED_RID can post comments. * Whether the DRUPAL_AUTHENTICATED_RID can post comments.
* *
...@@ -63,46 +54,23 @@ class CommentManager implements CommentManagerInterface { ...@@ -63,46 +54,23 @@ class CommentManager implements CommentManagerInterface {
*/ */
protected $urlGenerator; 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. * Construct the CommentManager object.
* *
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service. * The entity manager service.
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query factory.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory. * The config factory.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service. * The string translation service.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator service. * 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, QueryFactory $query_factory, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator) {
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
$this->queryFactory = $query_factory;
$this->userConfig = $config_factory->get('user.settings'); $this->userConfig = $config_factory->get('user.settings');
$this->stringTranslation = $string_translation; $this->stringTranslation = $string_translation;
$this->urlGenerator = $url_generator; $this->urlGenerator = $url_generator;
$this->moduleHandler = $module_handler;
$this->currentUser = $current_user;
} }
/** /**
...@@ -314,45 +282,4 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) { ...@@ -314,45 +282,4 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
return ''; 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;
}
} }
...@@ -6,9 +6,8 @@ ...@@ -6,9 +6,8 @@
*/ */
namespace Drupal\comment; 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; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
/** /**
...@@ -85,19 +84,4 @@ public function addBodyField($comment_type); ...@@ -85,19 +84,4 @@ public function addBodyField($comment_type);
*/ */
public function forbiddenMessage(EntityInterface $entity, $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);
} }
...@@ -327,8 +327,7 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie ...@@ -327,8 +327,7 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
*/ */
public static function attachNewCommentsLinkMetadata(array $element, array $context) { public static function attachNewCommentsLinkMetadata(array $element, array $context) {
// Build "X new comments" link metadata. // Build "X new comments" link metadata.
$new = \Drupal::service('comment.manager') $new = (int)comment_num_new($context['entity_id'], $context['entity_type']);
->getCountNewComments(entity_load($context['entity_type'], $context['entity_id']));
// Early-return if there are zero new comments for the current user. // Early-return if there are zero new comments for the current user.
if ($new === 0) { if ($new === 0) {
return $element; return $element;
......
...@@ -289,7 +289,7 @@ public function renderNewCommentsNodeLinks(Request $request) { ...@@ -289,7 +289,7 @@ public function renderNewCommentsNodeLinks(Request $request) {
$links = array(); $links = array();
foreach ($nids as $nid) { foreach ($nids as $nid) {
$node = node_load($nid); $node = node_load($nid);
$new = $this->commentManager->getCountNewComments($node); $new = comment_num_new($node->id(), 'node');
$query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node); $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node);
$links[$nid] = array( $links[$nid] = array(
'new_comment_count' => (int) $new, 'new_comment_count' => (int) $new,
......
...@@ -78,7 +78,7 @@ function testCommentInstallAfterContentModule() { ...@@ -78,7 +78,7 @@ function testCommentInstallAfterContentModule() {
// Drop default comment field added in CommentTestBase::setup(). // Drop default comment field added in CommentTestBase::setup().
FieldConfig::loadByName('node', 'comment')->delete(); FieldConfig::loadByName('node', 'comment')->delete();
if ($field = FieldConfig::loadByName('node', 'comment_forum')) { if ($field = FieldConfig::loadByName('node', 'comment_node_forum')) {
$field->delete(); $field->delete();
} }
......
...@@ -106,12 +106,10 @@ function testCommentTokenReplacement() { ...@@ -106,12 +106,10 @@ function testCommentTokenReplacement() {
$tests['[entity:comment-count-new]'] = 2; $tests['[entity:comment-count-new]'] = 2;
// Also test the deprecated legacy token. // Also test the deprecated legacy token.
$tests['[node:comment-count]'] = 2; $tests['[node:comment-count]'] = 2;
$tests['[node:comment-count-new]'] = 2;
foreach ($tests as $input => $expected) { foreach ($tests as $input => $expected) {
$output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->id)); $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))); $this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
} }
} }
} }
...@@ -591,7 +591,7 @@ function template_preprocess_forums(&$variables) { ...@@ -591,7 +591,7 @@ function template_preprocess_forums(&$variables) {
if ($topic->new_replies) { if ($topic->new_replies) {
$variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post<span class="visually-hidden"> in topic %title</span>', '@count new posts<span class="visually-hidden"> in topic %title</span>', array('%title' => $variables['topics'][$id]->label())); $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post<span class="visually-hidden"> in topic %title</span>', '@count new posts<span class="visually-hidden"> in topic %title</span>', 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_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_node_forum'), 'fragment' => 'new'));
} }
// Build table rows from topics. // Build table rows from topics.
......
services: services:
forum_manager: forum_manager:
class: Drupal\forum\ForumManager class: Drupal\forum\ForumManager
arguments: ['@config.factory', '@entity.manager', '@database', '@string_translation', '@comment.manager'] arguments: ['@config.factory', '@entity.manager', '@database', '@string_translation']
forum.breadcrumb.node: forum.breadcrumb.node:
class: Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder class: Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder
arguments: ['@entity.manager', '@config.factory', '@forum_manager'] arguments: ['@entity.manager', '@config.factory', '@forum_manager']
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\comment\CommentManagerInterface;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
/** /**
...@@ -68,13 +67,6 @@ class ForumManager implements ForumManagerInterface { ...@@ -68,13 +67,6 @@ class ForumManager implements ForumManagerInterface {
*/ */
protected $connection; protected $connection;
/**
* The comment manager service.
*
* @var \Drupal\comment\CommentManagerInterface
*/
protected $commentManager;
/** /**
* Array of last post information keyed by forum (term) id. * Array of last post information keyed by forum (term) id.
* *
...@@ -121,15 +113,12 @@ class ForumManager implements ForumManagerInterface { ...@@ -121,15 +113,12 @@ class ForumManager implements ForumManagerInterface {
* The current database connection. * The current database connection.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager service. * 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, CommentManagerInterface $comment_manager) { public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, Connection $connection, TranslationInterface $string_translation) {
$this->configFactory = $config_factory; $this->configFactory = $config_factory;
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
$this->connection = $connection; $this->connection = $connection;
$this->stringTranslation = $string_translation; $this->translationManager = $string_translation;
$this->commentManager = $comment_manager;
} }
/** /**
...@@ -236,7 +225,7 @@ public function getTopics($tid, AccountInterface $account) { ...@@ -236,7 +225,7 @@ public function getTopics($tid, AccountInterface $account) {
} }
else { else {
$history = $this->lastVisit($topic->id(), $account); $history = $this->lastVisit($topic->id(), $account);
$topic->new_replies = $this->commentManager->getCountNewComments($topic, 'comment_forum', $history); $topic->new_replies = $this->numberNew($topic->id(), $history);
$topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history); $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
} }
} }
...@@ -299,6 +288,21 @@ protected function getTopicOrder($sortby) { ...@@ -299,6 +288,21 @@ 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. * Gets the last time the user viewed a node.
* *
......
...@@ -70,16 +70,11 @@ public function testGetIndex() { ...@@ -70,16 +70,11 @@ public function testGetIndex() {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$comment_manager = $this->getMockBuilder('\Drupal\comment\CommentManagerInterface')
->disableOriginalConstructor()
->getMock();
$manager = $this->getMock('\Drupal\forum\ForumManager', array('getChildren'), array( $manager = $this->getMock('\Drupal\forum\ForumManager', array('getChildren'), array(
$config_factory, $config_factory,
$entity_manager, $entity_manager,
$connection, $connection,
$translation_manager, $translation_manager,
$comment_manager,
)); ));
$manager->expects($this->once()) $manager->expects($this->once())
......
...@@ -76,7 +76,7 @@ function tracker_page($account = NULL) { ...@@ -76,7 +76,7 @@ function tracker_page($account = NULL) {
if ($node->comment_count) { if ($node->comment_count) {
$comments = $node->comment_count; $comments = $node->comment_count;
if ($new = \Drupal::service('comment.manager')->getCountNewComments($node)) { if ($new = comment_num_new($node->id(), 'node')) {
$comments .= '<br />'; $comments .= '<br />';
$comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new')); $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new'));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment