diff --git a/core/modules/comment/src/CommentLinkBuilder.php b/core/modules/comment/src/CommentLinkBuilder.php index 7ce175c..8d43f2f 100644 --- a/core/modules/comment/src/CommentLinkBuilder.php +++ b/core/modules/comment/src/CommentLinkBuilder.php @@ -208,7 +208,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra if ($new_comments > 0) { $page_number = $this->entityManager ->getStorage('comment') - ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity); + ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity, $field_name); $query = $page_number ? ['page' => $page_number] : NULL; $value = [ 'new_comment_count' => (int) $new_comments, diff --git a/core/modules/comment/src/CommentStorage.php b/core/modules/comment/src/CommentStorage.php index 7737d44..d387c84 100644 --- a/core/modules/comment/src/CommentStorage.php +++ b/core/modules/comment/src/CommentStorage.php @@ -135,7 +135,7 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div /** * {@inheritdoc} */ - public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name = 'comment') { + public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) { $field = $entity->getFieldDefinition($field_name); $comments_per_page = $field->getSetting('per_page'); diff --git a/core/modules/comment/src/CommentStorageInterface.php b/core/modules/comment/src/CommentStorageInterface.php index 5be4776..318c0b4 100644 --- a/core/modules/comment/src/CommentStorageInterface.php +++ b/core/modules/comment/src/CommentStorageInterface.php @@ -54,7 +54,7 @@ public function getMaxThreadPerThread(CommentInterface $comment); * @return array|null * The page number where first new comment appears. (First page returns 0.) */ - public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name = 'comment'); + public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name); /** * Gets the display ordinal or page number for a comment. diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index ef86d5d..bcc6a8d 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -335,7 +335,7 @@ public function renderNewCommentsNodeLinks(Request $request) { $node = $this->entityManager->getStorage('node')->load($nid); $new = $this->commentManager->getCountNewComments($node); $page_number = $this->entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node); + ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name); $query = $page_number ? array('page' => $page_number) : NULL; $links[$nid] = array( 'new_comment_count' => (int) $new, diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php index 55d1613..e680edc 100644 --- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php @@ -161,12 +161,25 @@ public function preRender(&$values) { */ protected function renderLink($data, ResultRow $values) { if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') { + $node_type = $this->getValue($values, 'type'); $node = entity_create('node', array( 'nid' => $this->getValue($values, 'nid'), - 'type' => $this->getValue($values, 'type'), + 'type' => $node_type, )); - $page_number = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node); + // Identify the "first" comment field available. + $entity_manager = \Drupal::entityManager(); + $field_map = $entity_manager->getFieldMapByFieldType('comment'); + $comment_field_name = 'comment'; + foreach ($field_map['node'] as $field_name => $field_data) { + foreach ($field_data['bundles'] as $bundle_name) { + if ($node_type == $bundle_name) { + $comment_field_name = $field_name; + break 2; + } + } + } + $page_number = $entity_manager->getStorage('comment') + ->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $comment_field_name); $this->options['alter']['make_link'] = TRUE; $this->options['alter']['url'] = $node->urlInfo(); $this->options['alter']['query'] = $page_number ? array('page' => $page_number) : NULL; diff --git a/core/modules/comment/src/Tests/CommentFieldsTest.php b/core/modules/comment/src/Tests/CommentFieldsTest.php index 77cbbc0..ccfea18 100644 --- a/core/modules/comment/src/Tests/CommentFieldsTest.php +++ b/core/modules/comment/src/Tests/CommentFieldsTest.php @@ -95,6 +95,27 @@ public function testCommentFieldDelete() { } /** + * Tests link building with non-default comment field names. + */ + public function testCommentFieldLinksNonDefaultName() { + $this->drupalCreateContentType(array('type' => 'test_node_type')); + $this->addDefaultCommentField('node', 'test_node_type', 'comment2'); + + // Create a sample node. + $node = $this->drupalCreateNode(array( + 'title' => 'Baloney', + 'type' => 'test_node_type', + )); + + // Test that buildCommentedEntityLinks() does not break when the 'comment' + // field does not exist. Requires at least one comment. + $this->drupalLogin($this->webUser); + $this->postComment($node, 'Here is a comment', '', NULL, 'comment2'); + $context = ['view_mode' => 'teaser']; + \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context); + } + + /** * Tests creating a comment field through the interface. */ public function testCommentFieldCreate() { diff --git a/core/modules/comment/src/Tests/CommentPagerTest.php b/core/modules/comment/src/Tests/CommentPagerTest.php index 92fc9ff..9061f64 100644 --- a/core/modules/comment/src/Tests/CommentPagerTest.php +++ b/core/modules/comment/src/Tests/CommentPagerTest.php @@ -250,7 +250,7 @@ function testCommentNewPageIndicator() { $node = Node::load($node->id()); foreach ($expected_pages as $new_replies => $expected_page) { $returned_page = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node); + ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment'); $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))); } @@ -269,7 +269,7 @@ function testCommentNewPageIndicator() { $node = Node::load($node->id()); foreach ($expected_pages as $new_replies => $expected_page) { $returned_page = \Drupal::entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node); + ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment'); $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))); } }