/** * {@inheritdoc} */ public function update(CommentInterface $comment) { // Allow bulk updates and inserts to temporarily disable the maintenance of // the {comment_entity_statistics} table. if (!$this->state->get('comment.maintain_entity_statistics')) { return; } $query = $this->database->select('comment_field_data', 'c'); $query->addExpression('COUNT(cid)'); $count = $query->condition('c.entity_id', $comment->getCommentedEntityId())->condition('c.entity_type', $comment->getCommentedEntityTypeId())->condition('c.field_name', $comment->getFieldName())->condition('c.status', CommentInterface::PUBLISHED)->condition('c._deleted', FALSE)->condition('default_langcode', 1)->execute()->fetchField(); if ($count > 0) { // Comments exist. $last_reply = $this->database->select('comment_field_data', 'c')->fields('c', array('cid', 'name', 'changed', 'uid'))->condition('c.entity_id', $comment->getCommentedEntityId())->condition('c.entity_type', $comment->getCommentedEntityTypeId())->condition('c.field_name', $comment->getFieldName())->condition('c.status', CommentInterface::PUBLISHED)->condition('c._deleted', FALSE)->condition('default_langcode', 1)->orderBy('c.created', 'DESC')->range(0, 1)->execute()->fetchObject(); // Use merge here because entity could be created before comment field. $this->database->merge('comment_entity_statistics')->fields(array('cid' => $last_reply->cid, 'comment_count' => $count, 'last_comment_timestamp' => $last_reply->changed, 'last_comment_name' => $last_reply->uid ? '' : $last_reply->name, 'last_comment_uid' => $last_reply->uid))->keys(array('entity_id' => $comment->getCommentedEntityId(), 'entity_type' => $comment->getCommentedEntityTypeId(), 'field_name' => $comment->getFieldName()))->execute(); } else { // Comments do not exist. $entity = $comment->getCommentedEntity(); // Get the user ID from the entity if it's set, or default to the // currently logged in user. if ($entity instanceof EntityOwnerInterface) { $last_comment_uid = $entity->getOwnerId(); } if (!isset($last_comment_uid)) { // Default to current user when entity does not implement // EntityOwnerInterface or author is not set. $last_comment_uid = $this->currentUser->id(); } $this->database->update('comment_entity_statistics')->fields(array('cid' => 0, 'comment_count' => 0, 'last_comment_timestamp' => $entity instanceof EntityChangedInterface ? $entity->getChangedTimeAcrossTranslations() : REQUEST_TIME, 'last_comment_name' => '', 'last_comment_uid' => $last_comment_uid))->condition('entity_id', $comment->getCommentedEntityId())->condition('entity_type', $comment->getCommentedEntityTypeId())->condition('field_name', $comment->getFieldName())->execute(); } // Reset the cache of the commented entity so that when the entity is loaded // the next time, the statistics will be loaded again. But don't do this for // stub entities since they don't have all the necessary data at this point. if (!$comment->_rev->is_stub) { $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->resetCache([$comment->getCommentedEntityId()]); } }
/** * 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. * * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * @param \Drupal\comment\CommentInterface $comment * A comment entity. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * * @return \Symfony\Component\HttpFoundation\Response * The comment listing set to the page on which the comment appears. */ 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()->toString(TRUE); $redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all()); $redirect_request->query->set('page', $page); // Carry over the session to the subrequest. if ($session = $request->getSession()) { $redirect_request->setSession($session); } // @todo: Convert the pager to use the request object. $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(); }
/** * 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. * * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * @param \Drupal\comment\CommentInterface $comment * A comment entity. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * * @return \Symfony\Component\HttpFoundation\Response * The comment listing set to the page on which the comment appears. */ 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. $redirect_request = Request::create($entity->url(), 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all()); $redirect_request->query->set('page', $page); // Carry over the session to the subrequest. if ($session = $request->getSession()) { $redirect_request->setSession($session); } // @todo: Convert the pager to use the request object. $request->query->set('page', $page); return $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST); } throw new NotFoundHttpException(); }
/** * Gets the anonymous contact details setting from the comment. * * @param \Drupal\comment\CommentInterface $comment * The entity. * * @return int * The anonymous contact setting. */ protected function getAnonymousContactDetailsSetting(CommentInterface $comment) { return $comment->getCommentedEntity()->get($comment->getFieldName())->getFieldDefinition()->getSetting('anonymous'); }