예제 #1
0
 /**
  * Test comment field name.
  */
 public function testCommentFieldName()
 {
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     $view = Views::getView('test_comment_field_name');
     $this->executeView($view);
     $expected_result = [['cid' => $this->comment->id(), 'field_name' => $this->comment->getFieldName()], ['cid' => $this->customComment->id(), 'field_name' => $this->customComment->getFieldName()]];
     $column_map = ['cid' => 'cid', 'comment_field_data_field_name' => 'field_name'];
     $this->assertIdenticalResultset($view, $expected_result, $column_map);
     // Test that no data can be rendered.
     $this->assertIdentical(FALSE, isset($view->field['field_name']));
     // Grant permission to properly check view access on render.
     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
     $this->container->get('account_switcher')->switchTo(new AnonymousUserSession());
     $view = Views::getView('test_comment_field_name');
     $this->executeView($view);
     // Test that data rendered.
     $output = $renderer->executeInRenderContext(new RenderContext(), function () use($view) {
         return $view->field['field_name']->advancedRender($view->result[0]);
     });
     $this->assertIdentical($this->comment->getFieldName(), $output);
     $output = $renderer->executeInRenderContext(new RenderContext(), function () use($view) {
         return $view->field['field_name']->advancedRender($view->result[1]);
     });
     $this->assertIdentical($this->customComment->getFieldName(), $output);
 }
예제 #2
0
 /**
  * {@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()]);
     }
 }
 /**
  * Build the default links (reply, edit, delete …) for a comment.
  *
  * @param \Drupal\comment\CommentInterface $entity
  *   The comment object.
  * @param \Drupal\Core\Entity\EntityInterface $commented_entity
  *   The entity to which the comment is attached.
  *
  * @return array
  *   An array that can be processed by drupal_pre_render_links().
  */
 protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity)
 {
     $links = array();
     $status = $commented_entity->get($entity->getFieldName())->status;
     if ($status == CommentItemInterface::OPEN) {
         if ($entity->access('delete')) {
             $links['comment-delete'] = array('title' => t('Delete'), 'url' => $entity->urlInfo('delete-form'));
         }
         if ($entity->access('update')) {
             $links['comment-edit'] = array('title' => t('Edit'), 'url' => $entity->urlInfo('edit-form'));
         }
         if ($entity->access('create')) {
             $links['comment-reply'] = array('title' => t('Reply'), 'url' => Url::fromRoute('comment.reply', ['entity_type' => $entity->getCommentedEntityTypeId(), 'entity' => $entity->getCommentedEntityId(), 'field_name' => $entity->getFieldName(), 'pid' => $entity->id()]));
         }
         if (!$entity->isPublished() && $entity->access('approve')) {
             $links['comment-approve'] = array('title' => t('Approve'), 'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]));
         }
         if (empty($links) && $this->currentUser->isAnonymous()) {
             $links['comment-forbidden']['title'] = $this->commentManager->forbiddenMessage($commented_entity, $entity->getFieldName());
         }
     }
     // Add translations link for translation-enabled comment bundles.
     if ($this->moduleHandler->moduleExists('content_translation') && $this->access($entity)->isAllowed()) {
         $links['comment-translations'] = array('title' => t('Translate'), 'url' => $entity->urlInfo('drupal:content-translation-overview'));
     }
     return array('#theme' => 'links__comment__comment', '#links' => $links, '#attributes' => array('class' => array('links', 'inline')));
 }
예제 #4
0
 /**
  * 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();
 }
예제 #5
0
 /**
  * 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');
 }
예제 #7
0
 /**
  * {@inheritdoc}
  */
 public function getMaxThreadPerThread(CommentInterface $comment)
 {
     $query = $this->database->select('comment_field_data', 'c')->condition('entity_id', $comment->getCommentedEntityId())->condition('field_name', $comment->getFieldName())->condition('entity_type', $comment->getCommentedEntityTypeId())->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE')->condition('default_langcode', 1);
     $query->addExpression('MAX(thread)', 'thread');
     return $query->execute()->fetchField();
 }
예제 #8
0
 /**
  * Build the default links (reply, edit, delete …) for a comment.
  *
  * @param \Drupal\comment\CommentInterface $entity
  *   The comment object.
  * @param \Drupal\Core\Entity\EntityInterface $commented_entity
  *   The entity to which the comment is attached.
  *
  * @return array
  *   An array that can be processed by drupal_pre_render_links().
  */
 protected static function buildLinks(CommentInterface $entity, EntityInterface $commented_entity)
 {
     $links = array();
     $status = $commented_entity->get($entity->getFieldName())->status;
     $container = \Drupal::getContainer();
     if ($status == CommentItemInterface::OPEN) {
         if ($entity->access('delete')) {
             $links['comment-delete'] = array('title' => t('Delete'), 'href' => "comment/{$entity->id()}/delete", 'html' => TRUE);
         }
         if ($entity->access('update')) {
             $links['comment-edit'] = array('title' => t('Edit'), 'href' => "comment/{$entity->id()}/edit", 'html' => TRUE);
         }
         if ($entity->access('create')) {
             $links['comment-reply'] = array('title' => t('Reply'), 'href' => "comment/reply/{$entity->getCommentedEntityTypeId()}/{$entity->getCommentedEntityId()}/{$entity->getFieldName()}/{$entity->id()}", 'html' => TRUE);
         }
         if (!$entity->isPublished() && $entity->access('approve')) {
             $links['comment-approve'] = array('title' => t('Approve'), 'route_name' => 'comment.approve', 'route_parameters' => array('comment' => $entity->id()), 'html' => TRUE);
         }
         if (empty($links) && \Drupal::currentUser()->isAnonymous()) {
             $links['comment-forbidden']['title'] = \Drupal::service('comment.manager')->forbiddenMessage($commented_entity, $entity->getFieldName());
             $links['comment-forbidden']['html'] = TRUE;
         }
     }
     // Add translations link for translation-enabled comment bundles.
     if (\Drupal::moduleHandler()->moduleExists('content_translation') && content_translation_translate_access($entity)) {
         $links['comment-translations'] = array('title' => t('Translate'), 'href' => 'comment/' . $entity->id() . '/translations', 'html' => TRUE);
     }
     return array('#theme' => 'links__comment__comment', '#links' => $links, '#attributes' => array('class' => array('links', 'inline')));
 }