Chris@0: httpKernel = $http_kernel; Chris@0: $this->commentManager = $comment_manager; Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: if (!$entity_field_manager) { Chris@18: @trigger_error('The entity_field.manager service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $entity_field_manager = \Drupal::service('entity_field.manager'); Chris@18: } Chris@18: $this->entityFieldManager = $entity_field_manager; Chris@18: if (!$entity_repository) { Chris@18: @trigger_error('The entity.repository service must be passed to CommentController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $entity_repository = \Drupal::service('entity.repository'); Chris@18: } Chris@18: $this->entityRepository = $entity_repository; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('http_kernel'), Chris@0: $container->get('comment.manager'), Chris@18: $container->get('entity_type.manager'), Chris@18: $container->get('entity_field.manager'), Chris@18: $container->get('entity.repository') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Publishes the specified comment. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * A comment entity. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: */ Chris@0: public function commentApprove(CommentInterface $comment) { Chris@17: $comment->setPublished(); Chris@0: $comment->save(); Chris@0: Chris@17: $this->messenger()->addStatus($this->t('Comment approved.')); Chris@0: $permalink_uri = $comment->permalink(); Chris@0: $permalink_uri->setAbsolute(); Chris@0: return new RedirectResponse($permalink_uri->toString()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Redirects comment links to the correct page depending on comment settings. Chris@0: * Chris@0: * Since comments are paged there is no way to guarantee which page a comment Chris@0: * appears on. Comment paging and threading settings may be changed at any Chris@0: * time. With threaded comments, an individual comment may move between pages Chris@0: * as comments can be added either before or after it in the overall Chris@0: * discussion. Therefore we use a central routing function for comment links, Chris@0: * which calculates the page number based on current comment settings and Chris@0: * returns the full comment view with the pager set dynamically. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The request of the page. Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * A comment entity. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\Response Chris@0: * The comment listing set to the page on which the comment appears. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: */ Chris@0: public function commentPermalink(Request $request, CommentInterface $comment) { Chris@0: if ($entity = $comment->getCommentedEntity()) { Chris@0: // Check access permissions for the entity. Chris@0: if (!$entity->access('view')) { Chris@0: throw new AccessDeniedHttpException(); Chris@0: } Chris@18: $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; Chris@0: Chris@0: // Find the current display page for this comment. Chris@18: $page = $this->entityTypeManager()->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); Chris@0: // @todo: Cleaner sub request handling. Chris@18: $subrequest_url = $entity->toUrl()->setOption('query', ['page' => $page])->toString(TRUE); Chris@0: $redirect_request = Request::create($subrequest_url->getGeneratedUrl(), 'GET', $request->query->all(), $request->cookies->all(), [], $request->server->all()); Chris@0: // Carry over the session to the subrequest. Chris@18: if ($request->hasSession()) { Chris@18: $redirect_request->setSession($request->getSession()); Chris@0: } Chris@0: $request->query->set('page', $page); Chris@0: $response = $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST); Chris@0: if ($response instanceof CacheableResponseInterface) { Chris@0: // @todo Once path aliases have cache tags (see Chris@0: // https://www.drupal.org/node/2480077), add test coverage that Chris@0: // the cache tag for a commented entity's path alias is added to the Chris@0: // comment's permalink response, because there can be blocks or Chris@0: // other content whose renderings depend on the subrequest's URL. Chris@0: $response->addCacheableDependency($subrequest_url); Chris@0: } Chris@0: return $response; Chris@0: } Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * The _title_callback for the page that renders the comment permalink. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * The current comment. Chris@0: * Chris@0: * @return string Chris@0: * The translated comment subject. Chris@0: */ Chris@0: public function commentPermalinkTitle(CommentInterface $comment) { Chris@18: return $this->entityRepository->getTranslationFromContext($comment)->label(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Redirects legacy node links to the new path. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface $node Chris@0: * The node object identified by the legacy URL. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * Redirects user to new url. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: */ Chris@0: public function redirectNode(EntityInterface $node) { Chris@0: $fields = $this->commentManager->getFields('node'); Chris@0: // Legacy nodes only had a single comment field, so use the first comment Chris@0: // field on the entity. Chris@0: if (!empty($fields) && ($field_names = array_keys($fields)) && ($field_name = reset($field_names))) { Chris@0: return $this->redirect('comment.reply', [ Chris@0: 'entity_type' => 'node', Chris@0: 'entity' => $node->id(), Chris@0: 'field_name' => $field_name, Chris@0: ]); Chris@0: } Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form constructor for the comment reply form. Chris@0: * Chris@0: * There are several cases that have to be handled, including: Chris@0: * - replies to comments Chris@0: * - replies to entities Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The current request object. Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity this comment belongs to. Chris@0: * @param string $field_name Chris@0: * The field_name to which the comment belongs. Chris@0: * @param int $pid Chris@0: * (optional) Some comments are replies to other comments. In those cases, Chris@0: * $pid is the parent comment's comment ID. Defaults to NULL. Chris@0: * Chris@0: * @return array|\Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * An associative array containing: Chris@0: * - An array for rendering the entity or parent comment. Chris@0: * - comment_entity: If the comment is a reply to the entity. Chris@0: * - comment_parent: If the comment is a reply to another comment. Chris@0: * - comment_form: The comment form as a renderable array. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: */ Chris@0: public function getReplyForm(Request $request, EntityInterface $entity, $field_name, $pid = NULL) { Chris@0: $account = $this->currentUser(); Chris@0: $build = []; Chris@0: Chris@0: // The user is not just previewing a comment. Chris@0: if ($request->request->get('op') != $this->t('Preview')) { Chris@0: Chris@0: // $pid indicates that this is a reply to a comment. Chris@0: if ($pid) { Chris@0: // Load the parent comment. Chris@18: $comment = $this->entityTypeManager()->getStorage('comment')->load($pid); Chris@0: // Display the parent comment. Chris@18: $build['comment_parent'] = $this->entityTypeManager()->getViewBuilder('comment')->view($comment); Chris@0: } Chris@0: Chris@0: // The comment is in response to a entity. Chris@0: elseif ($entity->access('view', $account)) { Chris@0: // We make sure the field value isn't set so we don't end up with a Chris@0: // redirect loop. Chris@0: $entity = clone $entity; Chris@0: $entity->{$field_name}->status = CommentItemInterface::HIDDEN; Chris@0: // Render array of the entity full view mode. Chris@18: $build['commented_entity'] = $this->entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, 'full'); Chris@0: unset($build['commented_entity']['#cache']); Chris@0: } Chris@0: } Chris@0: else { Chris@0: $build['#title'] = $this->t('Preview comment'); Chris@0: } Chris@0: Chris@0: // Show the actual reply box. Chris@18: $comment = $this->entityTypeManager()->getStorage('comment')->create([ Chris@0: 'entity_id' => $entity->id(), Chris@0: 'pid' => $pid, Chris@0: 'entity_type' => $entity->getEntityTypeId(), Chris@0: 'field_name' => $field_name, Chris@0: ]); Chris@0: $build['comment_form'] = $this->entityFormBuilder()->getForm($comment); Chris@0: Chris@0: return $build; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Access check for the reply form. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity this comment belongs to. Chris@0: * @param string $field_name Chris@0: * The field_name to which the comment belongs. Chris@0: * @param int $pid Chris@0: * (optional) Some comments are replies to other comments. In those cases, Chris@0: * $pid is the parent comment's comment ID. Defaults to NULL. Chris@0: * Chris@0: * @return \Drupal\Core\Access\AccessResultInterface Chris@0: * An access result Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: */ Chris@0: public function replyFormAccess(EntityInterface $entity, $field_name, $pid = NULL) { Chris@0: // Check if entity and field exists. Chris@0: $fields = $this->commentManager->getFields($entity->getEntityTypeId()); Chris@0: if (empty($fields[$field_name])) { Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: Chris@0: $account = $this->currentUser(); Chris@0: Chris@0: // Check if the user has the proper permissions. Chris@0: $access = AccessResult::allowedIfHasPermission($account, 'post comments'); Chris@0: Chris@12: // If commenting is open on the entity. Chris@0: $status = $entity->{$field_name}->status; Chris@0: $access = $access->andIf(AccessResult::allowedIf($status == CommentItemInterface::OPEN) Chris@12: ->addCacheableDependency($entity)) Chris@12: // And if user has access to the host entity. Chris@12: ->andIf(AccessResult::allowedIf($entity->access('view'))); Chris@0: Chris@0: // $pid indicates that this is a reply to a comment. Chris@0: if ($pid) { Chris@0: // Check if the user has the proper permissions. Chris@0: $access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'access comments')); Chris@0: Chris@0: // Load the parent comment. Chris@18: $comment = $this->entityTypeManager()->getStorage('comment')->load($pid); Chris@0: // Check if the parent comment is published and belongs to the entity. Chris@0: $access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id())); Chris@0: if ($comment) { Chris@0: $access->addCacheableDependency($comment); Chris@0: } Chris@0: } Chris@0: return $access; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a set of nodes' last read timestamps. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The request of the page. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\JsonResponse Chris@0: * The JSON response. Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException Chris@0: */ Chris@0: public function renderNewCommentsNodeLinks(Request $request) { Chris@0: if ($this->currentUser()->isAnonymous()) { Chris@0: throw new AccessDeniedHttpException(); Chris@0: } Chris@0: Chris@0: $nids = $request->request->get('node_ids'); Chris@0: $field_name = $request->request->get('field_name'); Chris@0: if (!isset($nids)) { Chris@0: throw new NotFoundHttpException(); Chris@0: } Chris@0: // Only handle up to 100 nodes. Chris@0: $nids = array_slice($nids, 0, 100); Chris@0: Chris@0: $links = []; Chris@0: foreach ($nids as $nid) { Chris@18: $node = $this->entityTypeManager()->getStorage('node')->load($nid); Chris@0: $new = $this->commentManager->getCountNewComments($node); Chris@18: $page_number = $this->entityTypeManager()->getStorage('comment') Chris@0: ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name); Chris@0: $query = $page_number ? ['page' => $page_number] : NULL; Chris@0: $links[$nid] = [ Chris@0: 'new_comment_count' => (int) $new, Chris@0: 'first_new_comment_link' => $this->getUrlGenerator()->generateFromRoute('entity.node.canonical', ['node' => $node->id()], ['query' => $query, 'fragment' => 'new']), Chris@0: ]; Chris@0: } Chris@0: Chris@0: return new JsonResponse($links); Chris@0: } Chris@0: Chris@0: }