CommentController::commentPermalink

public CommentController::commentPermalink(Request $request, CommentInterface $comment)

Redirects comment links to the correct page depending on comment settings.

Since comments are paged there is no way to guarantee which page a comment appears on. Comment paging and threading settings may be changed at any time. With threaded comments, an individual comment may move between pages as comments can be added either before or after it in the overall discussion. Therefore we use a central routing function for comment links, which calculates the page number based on current comment settings and returns the full comment view with the pager set dynamically.

Parameters

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

\Drupal\comment\CommentInterface $comment: A comment entity.

Return value

\Symfony\Component\HttpFoundation\Response The comment listing set to the page on which the comment appears.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

File

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

Class

CommentController
Controller for the comment entity.

Namespace

Drupal\comment\Controller

Code

public function commentPermalink(Request $request, CommentInterface $comment) {
  if ($entity = $comment->getCommentedEntity()) {
    // Check access permissions for the entity.
    if (!$entity->access('view')) {
      throw new AccessDeniedHttpException();
    }
    $field_definition = $this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];

    // Find the current display page for this comment.
    $page = $this->entityManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
    // @todo: Cleaner sub request handling.
    $subrequest_url = $entity->urlInfo()->setOption('query', ['page' => $page])->toString(TRUE);
    $redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
    // Carry over the session to the subrequest.
    if ($session = $request->getSession()) {
      $redirect_request->setSession($session);
    }
    $request->query->set('page', $page);
    $response = $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST);
    if ($response instanceof CacheableResponseInterface) {
      // @todo Once path aliases have cache tags (see
      //   https://www.drupal.org/node/2480077), add test coverage that
      //   the cache tag for a commented entity's path alias is added to the
      //   comment's permalink response, because there can be blocks or
      //   other content whose renderings depend on the subrequest's URL.
      $response->addCacheableDependency($subrequest_url);
    }
    return $response;
  }
  throw new NotFoundHttpException();
}
doc_Drupal
2016-10-29 08:50:33
Comments
Leave a Comment

Please login to continue.