function CommentController::renderNewCommentsNodeLinks

Same name in other branches
  1. 9 core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController::renderNewCommentsNodeLinks()
  2. 8.9.x core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController::renderNewCommentsNodeLinks()
  3. 11.x core/modules/comment/src/Controller/CommentController.php \Drupal\comment\Controller\CommentController::renderNewCommentsNodeLinks()

Returns a set of nodes' last read timestamps.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request of the page.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

1 string reference to 'CommentController::renderNewCommentsNodeLinks'
comment.routing.yml in core/modules/comment/comment.routing.yml
core/modules/comment/comment.routing.yml

File

core/modules/comment/src/Controller/CommentController.php, line 319

Class

CommentController
Controller for the comment entity.

Namespace

Drupal\comment\Controller

Code

public function renderNewCommentsNodeLinks(Request $request) {
    if ($this->currentUser()
        ->isAnonymous()) {
        throw new AccessDeniedHttpException();
    }
    if (!$request->request
        ->has('node_ids') || !$request->request
        ->has('field_name')) {
        throw new NotFoundHttpException();
    }
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.