diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module
index 5b70f42..7a62c16 100644
--- a/core/modules/comment/comment.module
+++ b/core/modules/comment/comment.module
@@ -11,6 +11,7 @@
*/
use Drupal\comment\CommentInterface;
+use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Entity\ContentEntityInterface;
@@ -28,16 +29,6 @@
use Symfony\Component\HttpFoundation\Request;
/**
- * Comments are displayed in a flat list - expanded.
- */
-const COMMENT_MODE_FLAT = 0;
-
-/**
- * Comments are displayed as a threaded list - expanded.
- */
-const COMMENT_MODE_THREADED = 1;
-
-/**
* Anonymous posters cannot enter their contact information.
*/
const COMMENT_ANONYMOUS_MAYNOT_CONTACT = 0;
@@ -320,27 +311,16 @@ function comment_permission() {
*
* @return array|null
* An array "page=X" if the page number is greater than zero; NULL otherwise.
+ *
+ * @deprecated Deprecated since Drupal 8.x-dev, to be removed in Drupal 8.0.
+ * Use \Drupal\comment\CommentStorageInterface::getNewCommentPageNr();
+ * Note this returns an integer instead of array|null.
*/
function comment_new_page_count($num_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment') {
- $instance = $entity->getFieldDefinition($field_name);
- $comments_per_page = $instance->getSetting('per_page');
-
- if ($num_comments <= $comments_per_page) {
- // Only one page of comments.
- $pageno = 0;
- }
- elseif ($instance->getSetting('default_mode') == COMMENT_MODE_FLAT) {
- // Flat comments.
- $count = $num_comments - $new_comments;
- $pageno = $count / $comments_per_page;
- }
- else {
- // Threaded comments.
- $pageno = \Drupal::entityManager()->getStorage('comment')
- ->getThreadedNewCommentPageNr($entity, $new_comments, $comments_per_page, $field_name);
- }
+ $page_nr = \Drupal::entityManager()->getStorage('comment')
+ ->getNewCommentPageNr($num_comments, $new_comments, $entity, $field_name);
- return $pageno ? array('page' => $pageno) : NULL;
+ return $page_nr ? array('page' => $page_nr) : NULL;
}
/**
@@ -658,7 +638,7 @@ function comment_get_thread(EntityInterface $entity, $field_name, $mode, $commen
$query->condition('c.status', CommentInterface::PUBLISHED);
$count_query->condition('c.status', CommentInterface::PUBLISHED);
}
- if ($mode == COMMENT_MODE_FLAT) {
+ if ($mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
$query->orderBy('c.cid', 'ASC');
}
else {
@@ -1146,7 +1126,7 @@ function comment_get_display_ordinal($cid, FieldDefinitionInterface $field_defin
$query->condition('c1.status', CommentInterface::PUBLISHED);
}
- if ($field_definition->getSetting('default_mode') == COMMENT_MODE_FLAT) {
+ if ($field_definition->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
// For flat comments, cid is used for ordering comments due to
// unpredictable behavior with timestamp, so we make the same assumption
// here.
diff --git a/core/modules/comment/src/CommentManagerInterface.php b/core/modules/comment/src/CommentManagerInterface.php
index f302e18..7f3519c 100644
--- a/core/modules/comment/src/CommentManagerInterface.php
+++ b/core/modules/comment/src/CommentManagerInterface.php
@@ -16,6 +16,16 @@
interface CommentManagerInterface {
/**
+ * Comments are displayed in a flat list - expanded.
+ */
+ const COMMENT_MODE_FLAT = 0;
+
+ /**
+ * Comments are displayed as a threaded list - expanded.
+ */
+ const COMMENT_MODE_THREADED = 1;
+
+ /**
* Utility function to return an array of comment fields.
*
* @param string $entity_type_id
diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php
index f58dbeb..64b1274 100644
--- a/core/modules/comment/src/CommentStorage.php
+++ b/core/modules/comment/src/CommentStorage.php
@@ -8,7 +8,7 @@
namespace Drupal\comment;
use Drupal\Core\Database\Connection;
-use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
@@ -92,7 +92,7 @@ public function updateEntityStatistics(CommentInterface $comment) {
/**
* {@inheritdoc}
*/
- public function getMaxThread(EntityInterface $comment) {
+ public function getMaxThread(CommentInterface $comment) {
$query = $this->database->select('comment', 'c')
->condition('entity_id', $comment->getCommentedEntityId())
->condition('field_id', $comment->getFieldId())
@@ -105,7 +105,7 @@ public function getMaxThread(EntityInterface $comment) {
/**
* {@inheritdoc}
*/
- public function getMaxThreadPerThread(EntityInterface $comment) {
+ public function getMaxThreadPerThread(CommentInterface $comment) {
$query = $this->database->select('comment', 'c')
->condition('entity_id', $comment->getCommentedEntityId())
->condition('field_id', $comment->getFieldId())
@@ -119,44 +119,59 @@ public function getMaxThreadPerThread(EntityInterface $comment) {
/**
* {@inheritdoc}
*/
- public function getThreadedNewCommentPageNr(EntityInterface $entity, $new_replies, $comments_per_page, $field_name) {
- // 1. Find all the threads with a new comment.
- $unread_threads_query = $this->database->select('comment')
- ->fields('comment', array('thread'))
- ->condition('entity_id', $entity->id())
- ->condition('entity_type', $entity->getEntityTypeId())
- ->condition('field_id', $entity->getEntityTypeId() . '__' . $field_name)
- ->condition('status', CommentInterface::PUBLISHED)
- ->orderBy('created', 'DESC')
- ->orderBy('cid', 'DESC')
- ->range(0, $new_replies);
-
- // 2. Find the first thread.
- $first_thread_query = $this->database->select($unread_threads_query, 'thread');
- $first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder');
- $first_thread = $first_thread_query
- ->fields('thread', array('thread'))
- ->orderBy('torder')
- ->range(0, 1)
- ->execute()
- ->fetchField();
+ public function getNewCommentPageNr($num_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment') {
+ $instance = $entity->getFieldDefinition($field_name);
+ $comments_per_page = $instance->getSetting('per_page');
+
+ if ($num_comments <= $comments_per_page) {
+ // Only one page of comments.
+ $count = 0;
+ }
+ elseif ($instance->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_FLAT) {
+ // Flat comments.
+ $count = $num_comments - $new_comments;
+ }
+ else {
+ // Threaded comments.
+
+ // 1. Find all the threads with a new comment.
+ $unread_threads_query = $this->database->select('comment')
+ ->fields('comment', array('thread'))
+ ->condition('entity_id', $entity->id())
+ ->condition('entity_type', $entity->getEntityTypeId())
+ ->condition('field_id', $entity->getEntityTypeId() . '__' . $field_name)
+ ->condition('status', CommentInterface::PUBLISHED)
+ ->orderBy('created', 'DESC')
+ ->orderBy('cid', 'DESC')
+ ->range(0, $new_comments);
+
+ // 2. Find the first thread.
+ $first_thread_query = $this->database->select($unread_threads_query, 'thread');
+ $first_thread_query->addExpression('SUBSTRING(thread, 1, (LENGTH(thread) - 1))', 'torder');
+ $first_thread = $first_thread_query
+ ->fields('thread', array('thread'))
+ ->orderBy('torder')
+ ->range(0, 1)
+ ->execute()
+ ->fetchField();
+
+ // Remove the final '/'.
+ $first_thread = substr($first_thread, 0, -1);
+
+ // Find the number of the first comment of the first unread thread.
+ $count = $this->database->query('SELECT COUNT(*) FROM {comment} WHERE entity_id = :entity_id
+ AND entity_type = :entity_type
+ AND field_id = :field_id
+ AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
+ ':status' => CommentInterface::PUBLISHED,
+ ':entity_id' => $entity->id(),
+ ':field_id' => $entity->getEntityTypeId() . '__' . $field_name,
+ ':entity_type' => $entity->getEntityTypeId(),
+ ':thread' => $first_thread,
+ ))->fetchField();
+ }
- // Remove the final '/'.
- $first_thread = substr($first_thread, 0, -1);
-
- // Find the number of the first comment of the first unread thread.
- $count = $this->database->query('SELECT COUNT(*) FROM {comment} WHERE entity_id = :entity_id
- AND entity_type = :entity_type
- AND field_id = :field_id
- AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
- ':status' => CommentInterface::PUBLISHED,
- ':entity_id' => $entity->id(),
- ':field_id' => $entity->getEntityTypeId() . '__' . $field_name,
- ':entity_type' => $entity->getEntityTypeId(),
- ':thread' => $first_thread,
- ))->fetchField();
-
- return floor($count / $comments_per_page);
+ return $comments_per_page > 0 ? floor($count / $comments_per_page) : 0;
}
/**
diff --git a/core/modules/comment/src/CommentStorageInterface.php b/core/modules/comment/src/CommentStorageInterface.php
index 4dc606a..5b17682 100644
--- a/core/modules/comment/src/CommentStorageInterface.php
+++ b/core/modules/comment/src/CommentStorageInterface.php
@@ -7,7 +7,7 @@
namespace Drupal\comment;
-use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
/**
@@ -16,45 +16,44 @@
interface CommentStorageInterface extends EntityStorageInterface {
/**
- * Get the maximum encoded thread value for the top level comments.
+ * Gets the maximum encoded thread value for the top level comments.
*
- * @param EntityInterface $comment
+ * @param \Drupal\comment\CommentInterface $comment
* A comment entity.
*
* @return string
* The maximum encoded thread value among the top level comments of the
* node $comment belongs to.
*/
- public function getMaxThread(EntityInterface $comment);
+ public function getMaxThread(CommentInterface $comment);
/**
- * Get the maximum encoded thread value for the children of this comment.
+ * Gets the maximum encoded thread value for the children of this comment.
*
- * @param EntityInterface $comment
+ * @param \Drupal\comment\CommentInterface $comment
* A comment entity.
*
* @return string
* The maximum encoded thread value among all replies of $comment.
*/
- public function getMaxThreadPerThread(EntityInterface $comment);
+ public function getMaxThreadPerThread(CommentInterface $comment);
/**
- * Gets the page number (location) of the first unread comment, if displaying
- * as a threaded list.
+ * Calculates the page number for the first new comment.
*
- * @param \Drupal\Core\Entity\EntityInterface $node
- * Node object.
- * @param int $new_replies
- * Number of new replies.
- * @param int $comments_per_page
- * Number of comments per page.
+ * @param int $num_comments
+ * The total number of comments that the entity has.
+ * @param int $new_comments
+ * The number of new comments that the entity has.
+ * @param \Drupal\Core\Entity\ContentEntityInterface $entity
+ * The entity that has the comments.
* @param string $field_name
* The field name on the entity to which comments are attached.
*
- * @return int
- * The page number (0-based).
+ * @return array|null
+ * An array "page=X" if the page number is greater than zero; NULL otherwise.
*/
- public function getThreadedNewCommentPageNr(EntityInterface $node, $new_replies, $comments_per_page, $field_name);
+ public function getNewCommentPageNr($num_comments, $new_comments, ContentEntityInterface $entity, $field_name = 'comment');
/**
* Gets the comment ids of the passed comment entities' children.
diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php
index e993e7a..bf5de6f 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -322,7 +322,10 @@ public static function attachNewCommentsLinkMetadata(array $element, array $cont
->getStorage($context['entity_type'])
->load($context['entity_id']);
$field_name = $context['field_name'];
- $query = comment_new_page_count($entity->{$field_name}->comment_count, $new, $entity);
+ $page_nr = \Drupal::entityManager()
+ ->getStorage('comment')
+ ->getNewCommentPageNr($entity->{$field_name}->comment_count, $new, $entity);
+ $query = $page_nr ? array('page' => $page_nr) : NULL;
// Attach metadata.
$element['#attached']['js'][] = array(
diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php
index 1e73008..8b2f427 100644
--- a/core/modules/comment/src/Controller/CommentController.php
+++ b/core/modules/comment/src/Controller/CommentController.php
@@ -290,7 +290,9 @@ public function renderNewCommentsNodeLinks(Request $request) {
foreach ($nids as $nid) {
$node = node_load($nid);
$new = comment_num_new($node->id(), 'node');
- $query = comment_new_page_count($node->{$field_name}->comment_count, $new, $node);
+ $page_nr = $this->entityManager()->getStorage('comment')
+ ->getNewCommentPageNr($node->{$field_name}->comment_count, $new, $node);
+ $query = $page_nr ? array('page' => $page_nr) : NULL;
$links[$nid] = array(
'new_comment_count' => (int) $new,
'first_new_comment_link' => $this->urlGenerator()->generateFromPath('node/' . $node->id(), array('query' => $query, 'fragment' => 'new')),
diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
index 5ccf4e2..c9a1fad 100644
--- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
+++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
@@ -151,9 +151,11 @@ protected function renderLink($data, ResultRow $values) {
'nid' => $this->getValue($values, 'nid'),
'type' => $this->getValue($values, 'type'),
));
+ $page_nr = \Drupal::entityManager()->getStorage('comment')
+ ->getNewCommentPageNr($this->getValue($values, 'comment_count'), $this->getValue($values), $node);
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = 'node/' . $node->id();
- $this->options['alter']['query'] = comment_new_page_count($this->getValue($values, 'comment_count'), $this->getValue($values), $node);
+ $this->options['alter']['query'] = $page_nr ? array('page' => $page_nr) : NULL;
$this->options['alter']['fragment'] = 'new';
}
diff --git a/core/modules/comment/src/Tests/CommentPagerTest.php b/core/modules/comment/src/Tests/CommentPagerTest.php
index f712ce0..1c29d14 100644
--- a/core/modules/comment/src/Tests/CommentPagerTest.php
+++ b/core/modules/comment/src/Tests/CommentPagerTest.php
@@ -245,8 +245,8 @@ function testCommentNewPageIndicator() {
$node = node_load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
- $returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node);
- $returned_page = is_array($returned) ? $returned['page'] : 0;
+ $returned_page = \Drupal::entityManager()->getStorage('comment')
+ ->getNewCommentPageNr($node->get('comment')->comment_count, $new_replies, $node);
$this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
}
@@ -264,8 +264,8 @@ function testCommentNewPageIndicator() {
\Drupal::entityManager()->getStorage('node')->resetCache(array($node->id()));
$node = node_load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
- $returned = comment_new_page_count($node->get('comment')->comment_count, $new_replies, $node);
- $returned_page = is_array($returned) ? $returned['page'] : 0;
+ $returned_page = \Drupal::entityManager()->getStorage('comment')
+ ->getNewCommentPageNr($node->get('comment')->comment_count, $new_replies, $node);
$this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
}
}
diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module
index 1b9b8f8..50b47a1 100644
--- a/core/modules/forum/forum.module
+++ b/core/modules/forum/forum.module
@@ -649,8 +649,11 @@ function template_preprocess_forums(&$variables) {
$variables['topics'][$id]->new_url = '';
if ($topic->new_replies) {
+ $page_nr = \Drupal::entityManager()->getStorage('comment')
+ ->getNewCommentPageNr($topic->comment_count, $topic->new_replies, $topic, 'comment_node_forum');
+ $query = $page_nr ? array('page' => $page_nr) : NULL;
$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' => $query, 'fragment' => 'new'));
}
// Build table rows from topics.