You are here

public function CommentController::renderNewCommentsNodeLinks in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 322
Contains \Drupal\comment\Controller\CommentController.

Class

CommentController
Controller for the comment entity.

Namespace

Drupal\comment\Controller

Code

public function renderNewCommentsNodeLinks(Request $request) {
  if ($this
    ->currentUser()
    ->isAnonymous()) {
    throw new AccessDeniedHttpException();
  }
  $nids = $request->request
    ->get('node_ids');
  $field_name = $request->request
    ->get('field_name');
  if (!isset($nids)) {
    throw new NotFoundHttpException();
  }

  // Only handle up to 100 nodes.
  $nids = array_slice($nids, 0, 100);
  $links = array();
  foreach ($nids as $nid) {
    $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, $field_name);
    $query = $page_number ? array(
      'page' => $page_number,
    ) : NULL;
    $links[$nid] = array(
      'new_comment_count' => (int) $new,
      'first_new_comment_link' => $this
        ->getUrlGenerator()
        ->generateFromRoute('entity.node.canonical', array(
        'node' => $node
          ->id(),
      ), array(
        'query' => $query,
        'fragment' => 'new',
      )),
    );
  }
  return new JsonResponse($links);
}