Commit 47d6bc0d authored by alexpott's avatar alexpott

Issue #2262407 by slashrsm, roderik: Deprecate comment_get_display_page/ordinal().

parent 090d1b3e
......@@ -12,6 +12,7 @@
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\String;
......@@ -30,16 +31,6 @@
use Drupal\user\EntityOwnerInterface;
use Drupal\node\NodeInterface;
/**
* 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.
*/
......@@ -261,7 +252,7 @@ function comment_new_page_count($num_comments, $new_replies, ContentEntityInterf
$mode = $field_definition->getSetting('default_mode');
$comments_per_page = $field_definition->getSetting('per_page');
$pagenum = NULL;
$flat = $mode == COMMENT_MODE_FLAT ? TRUE : FALSE;
$flat = $mode == CommentManagerInterface::COMMENT_MODE_FLAT ? TRUE : FALSE;
if ($num_comments <= $comments_per_page) {
// Only one page of comments.
$pageno = 0;
......@@ -611,7 +602,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 {
......@@ -991,35 +982,16 @@ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestam
* Field definition of the comments.
*
* @return int
* The display ordinal for the comment.
* The display ordinal for the comment, or 0 if the comment is not found.
*
* @see comment_get_display_page()
* @deprecated Deprecated since Drupal 8.x-dev, to be removed in Drupal 8.0.
* Use \Drupal\comment\CommentStorageInterface::getDisplayOrdinal().
*/
function comment_get_display_ordinal($cid, FieldDefinitionInterface $field_definition) {
// Count how many comments (c1) are before $cid (c2) in display order. This is
// the 0-based display ordinal.
$query = db_select('comment', 'c1');
$query->innerJoin('comment', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
$query->addExpression('COUNT(*)', 'count');
$query->condition('c2.cid', $cid);
if (!user_access('administer comments')) {
$query->condition('c1.status', CommentInterface::PUBLISHED);
}
if ($field_definition->getSetting('default_mode') == 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.
$query->condition('c1.cid', $cid, '<');
}
else {
// For threaded comments, the c.thread column is used for ordering. We can
// use the sorting code for comparison, but must remove the trailing slash.
// See CommentViewBuilder.
$query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) -1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) -1))');
}
return $query->execute()->fetchField();
$comment_storage = \Drupal::entityManager()->getStorage('comment');
$default_mode = $field_definition->getSetting('default_mode');
$comment = \Drupal::entityManager()->getStorage('comment')->load($cid);
return $comment ? $comment_storage->getDisplayOrdinal($comment, $default_mode) : 0;
}
/**
......@@ -1034,12 +1006,17 @@ function comment_get_display_ordinal($cid, FieldDefinitionInterface $field_defin
* Field definition of the comments.
*
* @return int
* The page number.
* The page number, or 0 if the comment is not found.
*
* @deprecated Deprecated since Drupal 8.x-dev, to be removed in Drupal 8.0.
* Use \Drupal\comment\CommentStorageInterface::getDisplayOrdinal().
*/
function comment_get_display_page($cid, FieldDefinitionInterface $field_definition) {
$ordinal = comment_get_display_ordinal($cid, $field_definition);
$comments_per_page = $field_definition->getSetting('per_page');
return floor($ordinal / $comments_per_page);
$comment_storage = \Drupal::entityManager()->getStorage('comment');
$default_mode = $field_definition->getSetting('default_mode');
$items_per_page = $field_definition->getSetting('per_page');
$comment = \Drupal::entityManager()->getStorage('comment')->load($cid);
return $comment ? $comment_storage->getDisplayOrdinal($comment, $default_mode, $items_per_page) : 0;
}
/**
......
......@@ -396,7 +396,7 @@ public function save(array $form, array &$form_state) {
$query = array();
// Find the current display page for this comment.
$field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name];
$page = comment_get_display_page($comment->id(), $field_definition);
$page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
if ($page > 0) {
$query['page'] = $page;
}
......
......@@ -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.
*
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\ContentEntityDatabaseStorage;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -29,6 +30,13 @@ class CommentStorage extends ContentEntityDatabaseStorage implements CommentStor
*/
protected $statistics;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Constructs a CommentStorage object.
*
......@@ -40,10 +48,13 @@ class CommentStorage extends ContentEntityDatabaseStorage implements CommentStor
* The entity manager.
* @param \Drupal\comment\CommentStatisticsInterface $comment_statistics
* The comment statistics service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, CommentStatisticsInterface $comment_statistics) {
public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, CommentStatisticsInterface $comment_statistics, AccountInterface $current_user) {
parent::__construct($entity_info, $database, $entity_manager);
$this->statistics = $comment_statistics;
$this->currentUser = $current_user;
}
/**
......@@ -54,7 +65,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
$entity_info,
$container->get('database'),
$container->get('entity.manager'),
$container->get('comment.statistics')
$container->get('comment.statistics'),
$container->get('current_user')
);
}
......@@ -92,6 +104,38 @@ public function getMaxThreadPerThread(EntityInterface $comment) {
->fetchField();
}
/**
* {@inheritdoc}
*/
public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
// Count how many comments (c1) are before $comment (c2) in display order.
// This is the 0-based display ordinal.
$query = $this->database->select('comment', 'c1');
$query->innerJoin('comment', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
$query->addExpression('COUNT(*)', 'count');
$query->condition('c2.cid', $comment->id());
if (!$this->currentUser->hasPermission('administer comments')) {
$query->condition('c1.status', CommentInterface::PUBLISHED);
}
if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
// For rendering flat comments, cid is used for ordering comments due to
// unpredictable behavior with timestamp, so we make the same assumption
// here.
$query->condition('c1.cid', $comment->id(), '<');
}
else {
// For threaded comments, the c.thread column is used for ordering. We can
// use the sorting code for comparison, but must remove the trailing
// slash.
$query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))');
}
$ordinal = $query->execute()->fetchField();
return ($divisor > 1) ? floor($ordinal / $divisor) : $ordinal;
}
/**
* {@inheritdoc}
*/
......
......@@ -38,6 +38,25 @@ public function getMaxThread(EntityInterface $comment);
*/
public function getMaxThreadPerThread(EntityInterface $comment);
/**
* Gets the display ordinal or page number for a comment.
*
* @param \Drupal\comment\CommentInterface $comment
* The comment to use as a reference point.
* @param int $comment_mode
* Comment mode (CommentManagerInterface::COMMENT_MODE_FLAT or
* CommentManagerInterface::COMMENT_MODE_THREADED).
* @param int $divisor
* Defaults to 1, which returns the display ordinal for a comment. If the
* number of comments per page is provided, the returned value will be the
* page number. (The return value will be divided by $divisor.)
*
* @return int
* The display ordinal or page number for the comment. It is 0-based, so
* will represent the number of items before the given comment/page.
*/
public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1);
/**
* Gets the comment ids of the passed comment entities' children.
*
......
......@@ -290,7 +290,7 @@ protected function alterBuild(array &$build, EntityInterface $comment, EntityVie
$commented_entity = $comment->getCommentedEntity();
$field_definition = $this->entityManager->getFieldDefinitions($commented_entity->getEntityTypeId(), $commented_entity->bundle())[$comment->getFieldName()];
$is_threaded = isset($comment->divs)
&& $field_definition->getSetting('default_mode') == COMMENT_MODE_THREADED;
&& $field_definition->getSetting('default_mode') == CommentManagerInterface::COMMENT_MODE_THREADED;
// Add indentation div or close open divs as needed.
if ($is_threaded) {
......
......@@ -114,7 +114,7 @@ public function commentPermalink(Request $request, CommentInterface $comment) {
$field_definition = $this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
// Find the current display page for this comment.
$page = comment_get_display_page($comment->id(), $field_definition);
$page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
// @todo: Cleaner sub request handling.
$redirect_request = Request::create($entity->getSystemPath(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
$redirect_request->query->set('page', $page);
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Plugin\Field\FieldType;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\CommentType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
......@@ -40,7 +41,7 @@ public static function defaultSettings() {
*/
public static function defaultInstanceSettings() {
return array(
'default_mode' => COMMENT_MODE_THREADED,
'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
'per_page' => 50,
'form_location' => COMMENT_FORM_BELOW,
'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT,
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Tests;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Entity\Comment;
......@@ -32,7 +33,7 @@ function testCommentInterface() {
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Post comment #1 without subject or preview.
......
......@@ -7,7 +7,7 @@
namespace Drupal\comment\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\comment\CommentManagerInterface;
/**
* Tests comments with node access.
......@@ -60,7 +60,7 @@ function testThreadedCommentView() {
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Post comment.
......
......@@ -7,6 +7,8 @@
namespace Drupal\comment\Tests;
use Drupal\comment\CommentManagerInterface;
/**
* Verifies pagination of comments.
*/
......@@ -37,7 +39,7 @@ function testCommentPaging() {
$comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
$comments[] = $this->postComment($node, $this->randomName(), $this->randomName(), TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
......@@ -77,7 +79,7 @@ function testCommentPaging() {
// If we switch to threaded mode, the replies on the oldest comment
// should be bumped to the first page and comment 6 should be bumped
// to the second page.
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 0)));
$this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.');
$this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.');
......@@ -137,7 +139,7 @@ function testCommentOrderingThreading() {
// - 2
// - 5
$this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
$expected_order = array(
0,
......@@ -151,7 +153,7 @@ function testCommentOrderingThreading() {
$this->drupalGet('node/' . $node->id());
$this->assertCommentOrder($comments, $expected_order);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$expected_order = array(
0,
......@@ -232,7 +234,7 @@ function testCommentNewPageIndicator() {
// - 2
// - 5
$this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
$expected_pages = array(
1 => 5, // Page of comment 5
......@@ -250,7 +252,7 @@ function testCommentNewPageIndicator() {
$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)));
}
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
$expected_pages = array(
1 => 5, // Page of comment 5
......@@ -313,7 +315,7 @@ function testTwoPagers() {
$this->setCommentForm(TRUE, $field_name);
$this->setCommentSubject(TRUE, $field_name);
$this->setCommentPreview(DRUPAL_OPTIONAL, $field_name);
$this->setCommentSettings('default_mode', COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
// Set comments to one per page so that we are able to test paging without
// needing to insert large numbers of comments.
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Tests;
use Drupal\comment\CommentManagerInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\comment\Entity\Comment;
......@@ -41,7 +42,7 @@ function testCommentPreview() {
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Login as web user and add a signature and a user picture.
......@@ -84,7 +85,7 @@ function testCommentEditPreviewSave() {
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$edit = array();
$date = new DrupalDateTime('2008-03-02 17:23');
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Tests;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
/**
......@@ -46,7 +47,7 @@ function testCommentNodeCommentStatistics() {
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Checks the initial values of node comment statistics with no comment.
......
......@@ -7,6 +7,8 @@
namespace Drupal\comment\Tests;
use Drupal\comment\CommentManagerInterface;
/**
* Tests comment threading.
*/
......@@ -28,7 +30,7 @@ function testCommentThreading() {
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Create a node.
......
......@@ -8,6 +8,7 @@
namespace Drupal\rdf\Tests;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Tests\CommentTestBase;
/**
......@@ -44,7 +45,7 @@ public function setUp() {
$this->setCommentPreview(DRUPAL_OPTIONAL);
$this->setCommentForm(TRUE);
$this->setCommentSubject(TRUE);
$this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
// Prepares commonly used URIs.
$this->base_uri = url('<front>', array('absolute' => TRUE));
......
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