diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index a9e24f3..93920c2 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -12,6 +12,7 @@ use Drupal\comment\CommentInterface; use Drupal\comment\Entity\Comment; +use Drupal\comment\CommentManagerInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; @@ -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; @@ -324,7 +315,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; @@ -699,7 +690,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 { @@ -1061,12 +1052,9 @@ function comment_user_predelete($account) { /** * Loads comment entities from the database. * -<<<<<<< HEAD * @deprecated in Drupal 8.x, will be removed before Drupal 9.0. * Use \Drupal\comment\Entity\Comment::loadMultiple(). * -======= ->>>>>>> Initial. * @param array $cids * (optional) An array of entity IDs. If omitted, all entities are loaded. * @param bool $reset @@ -1184,11 +1172,10 @@ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestam * Use \Drupal\comment\CommentStorageInterface::getDisplayOrdinal(). */ function comment_get_display_ordinal($cid, FieldDefinitionInterface $field_definition) { + $comment_storage = \Drupal::entityManager()->getStorage('comment'); + $default_mode = $field_definition->getSetting('default_mode'); $comment = \Drupal::entityManager()->getStorage('comment')->load($cid); - return $comment ? - \Drupal::entityManager()->getStorage('comment') - ->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode')) - : 0; + return $comment ? $comment_storage->getDisplayOrdinal($comment, $default_mode) : 0; } /** @@ -1209,11 +1196,11 @@ function comment_get_display_ordinal($cid, FieldDefinitionInterface $field_defin * Use \Drupal\comment\CommentStorageInterface::getDisplayOrdinal(). */ function comment_get_display_page($cid, FieldDefinitionInterface $field_definition) { + $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 ? - \Drupal::entityManager()->getStorage('comment') - ->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')) - : 0; + return $comment ? $comment_storage->getDisplayOrdinal($comment, $default_mode, $items_per_page) : 0; } /** 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 cad6942..b380a28 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -30,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. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info @@ -40,10 +47,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 +64,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') ); } @@ -126,11 +137,11 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div $query->innerJoin('comment', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_id = c1.field_id'); $query->addExpression('COUNT(*)', 'count'); $query->condition('c2.cid', $comment->id()); - if (!user_access('administer comments')) { + if (!$this->currentUser->hasPermission('administer comments')) { $query->condition('c1.status', CommentInterface::PUBLISHED); } - if ($comment_mode == COMMENT_MODE_FLAT) { + 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. diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index e993e7a..be47a43 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -276,7 +276,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) { diff --git a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php index 50799b8..38ab3eb 100644 --- a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Plugin\Field\FieldType; +use Drupal\comment\CommentManagerInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\Field\FieldItemBase; @@ -39,7 +40,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, diff --git a/core/modules/comment/src/Tests/CommentInterfaceTest.php b/core/modules/comment/src/Tests/CommentInterfaceTest.php index 42157e1..40f77f6 100644 --- a/core/modules/comment/src/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/src/Tests/CommentInterfaceTest.php @@ -8,6 +8,7 @@ namespace Drupal\comment\Tests; use Drupal\comment\CommentInterface; +use Drupal\comment\CommentManagerInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; /** @@ -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. diff --git a/core/modules/comment/src/Tests/CommentNodeAccessTest.php b/core/modules/comment/src/Tests/CommentNodeAccessTest.php index a3eba40..7e4525c 100644 --- a/core/modules/comment/src/Tests/CommentNodeAccessTest.php +++ b/core/modules/comment/src/Tests/CommentNodeAccessTest.php @@ -7,6 +7,8 @@ namespace Drupal\comment\Tests; +use Drupal\comment\CommentManagerInterface; +use Drupal\comment\Entity\Comment; use Drupal\simpletest\WebTestBase; /** @@ -60,7 +62,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. diff --git a/core/modules/comment/src/Tests/CommentPagerTest.php b/core/modules/comment/src/Tests/CommentPagerTest.php index f712ce0..295c5e7 100644 --- a/core/modules/comment/src/Tests/CommentPagerTest.php +++ b/core/modules/comment/src/Tests/CommentPagerTest.php @@ -6,6 +6,7 @@ */ namespace Drupal\comment\Tests; +use Drupal\comment\CommentManagerInterface; /** * Verifies pagination of comments. @@ -37,7 +38,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 +78,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 +138,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 +152,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 +233,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 +251,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 +314,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. diff --git a/core/modules/comment/src/Tests/CommentPreviewTest.php b/core/modules/comment/src/Tests/CommentPreviewTest.php index 1de62da..be76ccf 100644 --- a/core/modules/comment/src/Tests/CommentPreviewTest.php +++ b/core/modules/comment/src/Tests/CommentPreviewTest.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Tests; +use Drupal\comment\CommentManagerInterface; use Drupal\Core\Datetime\DrupalDateTime; /** @@ -40,7 +41,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. @@ -83,7 +84,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'); diff --git a/core/modules/comment/src/Tests/CommentStatisticsTest.php b/core/modules/comment/src/Tests/CommentStatisticsTest.php index 6906b55..7172298 100644 --- a/core/modules/comment/src/Tests/CommentStatisticsTest.php +++ b/core/modules/comment/src/Tests/CommentStatisticsTest.php @@ -6,6 +6,8 @@ */ namespace Drupal\comment\Tests; +use Drupal\comment\CommentManagerInterface; +use Drupal\comment\Entity\Comment; /** * Tests the comment module administrative and end-user-facing interfaces. @@ -44,7 +46,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. diff --git a/core/modules/comment/src/Tests/CommentThreadingTest.php b/core/modules/comment/src/Tests/CommentThreadingTest.php index f69b55f..1eb67d0 100644 --- a/core/modules/comment/src/Tests/CommentThreadingTest.php +++ b/core/modules/comment/src/Tests/CommentThreadingTest.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Tests; +use Drupal\comment\CommentManagerInterface; use Drupal\Core\Language\Language; /** @@ -30,7 +31,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. diff --git a/core/modules/rdf/src/Tests/CommentAttributesTest.php b/core/modules/rdf/src/Tests/CommentAttributesTest.php index d3cacfd..3359462 100644 --- a/core/modules/rdf/src/Tests/CommentAttributesTest.php +++ b/core/modules/rdf/src/Tests/CommentAttributesTest.php @@ -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('', array('absolute' => TRUE));