diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 13575d9..84d7b94 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -214,7 +214,7 @@ function comment_entity_build_defaults_alter(array &$build, EntityInterface $ent } /** - * Implements hook_preprocess_hook() for theme_node(). + * Implements hook_node_links_alter(). */ function comment_node_links_alter(array &$node_links, NodeInterface $node, array &$context) { // Comment links are only added to node entity type for backwards diff --git a/core/modules/comment/js/node-new-comments-link.js b/core/modules/comment/js/node-new-comments-link.js index 9b72498..e1e10b2 100644 --- a/core/modules/comment/js/node-new-comments-link.js +++ b/core/modules/comment/js/node-new-comments-link.js @@ -40,12 +40,9 @@ return; } - var perPage = $placeholder.closest('[data-comment-per-page]').attr('data-comment-per-page'); - var threadingMode = $placeholder.closest('[data-comment-threading-mode]').attr('data-comment-threading-mode'); - // Perform an AJAX request to retrieve node read timestamps. Drupal.history.fetchTimestamps(nodeIDs, function () { - processNodeNewCommentLinks($placeholders, perPage, threadingMode); + processNodeNewCommentLinks($placeholders); }); } }; @@ -74,7 +71,7 @@ .end().show(); } - function processNodeNewCommentLinks($placeholders, perPage, threadingMode) { + function processNodeNewCommentLinks($placeholders) { // Figure out which placeholders need the "x new comments" links. var $placeholdersToUpdate = {}; var fieldName = 'comment'; @@ -124,7 +121,7 @@ $.ajax({ url: Drupal.url('comments/render_new_comments_node_links'), type: 'POST', - data: { 'node_ids[]': nodeIDs, 'field_name': fieldName, 'per_page': perPage, 'threading_mode': threadingMode }, + data: { 'node_ids[]': nodeIDs, 'field_name': fieldName }, dataType: 'json', success: render }); diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 7901bda..8fb4a34 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -384,17 +384,23 @@ public function save(array $form, FormStateInterface $form_state) { else { drupal_set_message($this->t('Your comment has been posted.')); } - // Find the current display page for this comment. - $display_settings = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'default') - ->getComponent($field_name); - $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; - $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); - $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $comment_type->getThreadingMode(), $display_settings['settings']['per_page']); - $query = array(); - if ($page > 0) { - $query['page'] = $page; + // Find the current display page for this comment. We link to it using + // the standard entity urlInfo, i.e. the full/default view mode. + $page_number = 0; + $display = $this->entityManager->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.full'); + if (!$display) { + $display = $this->entityManager->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.default'); + } + $formatter_options = $display ? $display->getComponent($field_name): NULL; + if ($formatter_options) { + $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; + $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); + $page_number = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $comment_type->getThreadingMode(), $formatter_options['settings']['per_page']); } // Redirect to the newly posted comment. + $query = $page_number ? array('page' => $page_number) : NULL; $uri->setOption('query', $query); $uri->setOption('fragment', 'comment-' . $comment->id()); } diff --git a/core/modules/comment/src/CommentLinkBuilder.php b/core/modules/comment/src/CommentLinkBuilder.php index 168cb03..abc1f66 100644 --- a/core/modules/comment/src/CommentLinkBuilder.php +++ b/core/modules/comment/src/CommentLinkBuilder.php @@ -22,6 +22,12 @@ * Defines a class for building markup for comment links on a commented entity. * * Comment links include 'login to post new comment', 'add new comment' etc. + * + * Note this class does not build the regular links displayed with a comment + * entity. (Those have a regular field formatter; see + * CommentViewBuilder::renderLinks() for those.) The links generated here can + * be added to the regular links displayed with the commentED entity. (This is + * done automatically for nodes by comment_node_links_alter().) */ class CommentLinkBuilder implements CommentLinkBuilderInterface { @@ -113,9 +119,6 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra ); } else { - $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; - $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); - // We create links to the first new comment using the standard entity // urlInfo, i.e. the full/default view mode. $display = $this->entityManager->getStorage('entity_view_display') @@ -134,7 +137,10 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra && !empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments')) { // Comments can theoretically be displayed inside a teaser, but even - // then we still choose to link to the 'full page' comments. + // then we still choose to link to the 'full page' comments. These + // are the only ones that have a pager, i.e. have all comments + // reachable. (Only for 'full' mode we are sure we can reach the + // first new comment.) $links['comment-comments'] = array( 'title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'), 'attributes' => array('title' => $this->t('Jump to the first comment of this posting.')), @@ -144,14 +150,12 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra if ($this->moduleHandler->moduleExists('history')) { $links['comment-new-comments'] = array( 'title' => '', - 'url' => Url::fromRoute(''), + 'url' => $entity->urlInfo(), 'attributes' => array( 'class' => 'hidden', 'title' => $this->t('Jump to the first new comment of this posting.'), 'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp, 'data-history-node-field-name' => $field_name, - 'data-comment-threading-mode' => $comment_type->getThreadingMode(), - 'data-comment-per-page' => $formatter_options_full['settings']['per_page'], ), ); } @@ -256,8 +260,6 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra 'entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, - 'threading_mode' => $comment_type->getThreadingMode(), - 'per_page' => $formatter_options_full['settings']['per_page'], ), ); } diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index fe879ee..93b8d6d 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -358,13 +358,26 @@ public static function attachNewCommentsLinkMetadata(array $element, array $cont if ($new === 0) { return $element; } - $entity = \Drupal::entityManager() - ->getStorage($context['entity_type']) + $entity = \Drupal::entityManager()->getStorage($context['entity_type']) ->load($context['entity_id']); $field_name = $context['field_name']; - $page_number = \Drupal::entityManager() - ->getStorage('comment') - ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity, $context['threading_mode'], $context['per_page']); + + // Find the current display page for this comment. We link to it using the + // standard node urlInfo, i.e. the full/default view mode. + $page_number = 0; + $display = \Drupal::entityManager()->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.full'); + if (!$display) { + $display = \Drupal::entityManager()->getStorage('entity_view_display') + ->load($entity->getEntityTypeId() . '.' . $entity->bundle() . '.default'); + } + $formatter_options = $display ? $display->getComponent($field_name) : NULL; + if ($formatter_options) { + $field_definition = \Drupal::entityManager()->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; + $comment_type = \Drupal::entityManager()->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); + $page_number = \Drupal::entityManager()->getStorage('comment') + ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new, $entity, $comment_type->getThreadingMode(), $formatter_options['settings']['per_page']); + } $query = $page_number ? array('page' => $page_number) : NULL; // Attach metadata. diff --git a/core/modules/comment/src/Controller/CommentController.php b/core/modules/comment/src/Controller/CommentController.php index 245e13c..cae9e22 100644 --- a/core/modules/comment/src/Controller/CommentController.php +++ b/core/modules/comment/src/Controller/CommentController.php @@ -292,9 +292,7 @@ public function renderNewCommentsNodeLinks(Request $request) { $nids = $request->request->get('node_ids'); $field_name = $request->request->get('field_name'); - $per_page = $request->request->get('per_page'); - $threading_mode = $request->request->get('threading_mode'); - if (!isset($nids) || !in_array($threading_mode, array(CommentTypeInterface::THREADING_MODE_THREADED, CommentTypeInterface::THREADING_MODE_FLAT))) { + if (!isset($nids)) { throw new NotFoundHttpException(); } // Only handle up to 100 nodes. @@ -304,8 +302,24 @@ public function renderNewCommentsNodeLinks(Request $request) { $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids); foreach ($nodes as $nid => $node) { $new = $this->commentManager->getCountNewComments($node); - $page_number = $this->entityManager()->getStorage('comment') - ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $threading_mode, $per_page); + + // Find the current display page for this comment. We link to it using the + // standard node urlInfo, i.e. the full/default view mode. + $page_number = 0; + $display = $this->entityManager->getStorage('entity_view_display') + ->load('node.' . $node->bundle() . '.full'); + if (!$display) { + $display = $this->entityManager->getStorage('entity_view_display') + ->load('node.' . $node->bundle() . '.default'); + } + $formatter_options = $display ? $display->getComponent($field_name) : NULL; + if ($formatter_options) { + $field_definition = $this->entityManager->getFieldStorageDefinitions('node')[$field_name]; + $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); + $page_number = $this->entityManager->getStorage('comment') + ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $comment_type->getThreadingMode(), $formatter_options['settings']['per_page']); + } + $query = $page_number ? array('page' => $page_number) : NULL; $links[$nid] = array( 'new_comment_count' => (int) $new, diff --git a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php index 0be4f01..d057415 100644 --- a/core/modules/comment/src/Tests/CommentNewIndicatorTest.php +++ b/core/modules/comment/src/Tests/CommentNewIndicatorTest.php @@ -109,8 +109,6 @@ public function testCommentNewCommentsIndicator() { // perform an HTTP request to render the "new comments" node link. $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-history-node-last-comment-timestamp attribute is set to the correct value.'); $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-field-name="comment"]')), 'data-history-node-field-name attribute is set to the correct value.'); - $this->assertIdentical(1, count($this->xpath('//*[@data-comment-per-page="50"]')), 'data-comment-per-page attribute is set to the correct value.'); - $this->assertIdentical(1, count($this->xpath('//*[@data-comment-threading-mode="1"]')), 'data-comment-threading-mode is set to the correct value.'); $response = $this->renderNewCommentsNodeLinks(array($this->node->id())); $this->assertResponse(200); $json = Json::decode($response);