getNewCommentPageNumber example


        if ($view_mode == 'teaser' && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) {
          $entity_links['comment__' . $field_name]['#cache']['contexts'][] = 'user';
          $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link';
          // Embed the metadata for the "X new comments" link (if any) on this           // entity.           $entity_links['comment__' . $field_name]['#attached']['drupalSettings']['history']['lastReadTimestamps'][$entity->id()] = (int) history_read($entity->id());
          $new_comments = $this->commentManager->getCountNewComments($entity);
          if ($new_comments > 0) {
            $page_number = $this->entityTypeManager
              ->getStorage('comment')
              ->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,
              'first_new_comment_link' => $entity->toUrl('canonical', [
                'query' => $query,
                'fragment' => 'new',
              ])->toString(),
            ];
            $parents = ['comment', 'newCommentsLinks', $entity->getEntityTypeId()$field_name$entity->id()];
            NestedArray::setValue($entity_links['comment__' . $field_name]['#attached']['drupalSettings']$parents$value);
          }
        }
// Page of comment 2       4 => 2,
      // Page of comment 1       5 => 1,
      // Page of comment 0       6 => 0,
    ];

    $node = Node::load($node->id());
    foreach ($expected_pages as $new_replies => $expected_page) {
      $returned_page = \Drupal::entityTypeManager()->getStorage('comment')
        ->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies$node, 'comment');
      $this->assertSame($expected_page$returned_pagenew FormattableMarkup('Flat mode, @new replies: expected page @expected, returned page @returned.', ['@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page]));
    }

    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');

    $expected_pages = [
      // Page of comment 5       1 => 5,
      // Page of comment 4       2 => 1,
      // Page of comment 4
$nids = $request->request->all('node_ids');
    $field_name = $request->request->get('field_name');

    // Only handle up to 100 nodes.     $nids = array_slice($nids, 0, 100);

    $links = [];
    foreach ($nids as $nid) {
      $node = $this->entityTypeManager()->getStorage('node')->load($nid);
      $new = $this->commentManager->getCountNewComments($node);
      $page_number = $this->entityTypeManager()->getStorage('comment')
        ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new$node$field_name);
      $query = $page_number ? ['page' => $page_number] : NULL;
      $links[$nid] = [
        'new_comment_count' => (int) $new,
        'first_new_comment_link' => Url::fromRoute('entity.node.canonical', ['node' => $node->id()]['query' => $query, 'fragment' => 'new'])->toString(),
      ];
    }

    return new JsonResponse($links);
  }

}
      $field_map = $this->entityFieldManager->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 = $this->entityTypeManager->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->toUrl();
      $this->options['alter']['query'] = $page_number ? ['page' => $page_number] : NULL;
      $this->options['alter']['fragment'] = 'new';
    }

    return $data;
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.