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

8.0.x
webchick 2014-06-26 12:03:19 -07:00
parent 81604eed23
commit 6532718c08
14 changed files with 126 additions and 67 deletions

View File

@ -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);
}
/**

View File

@ -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

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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,

View File

@ -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();
}

View File

@ -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)));
}
}
}

View File

@ -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<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_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.

View File

@ -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']

View File

@ -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.
*

View File

@ -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())

View File

@ -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 .= '<br />';
$comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->id(), array('fragment' => 'new'));
}