Ejemplo n.º 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);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1)
 {
     // Count how many comments (c1) are before $comment (c2) in display order.
     // This is the 0-based display ordinal.
     $query = $this->database->select('comment_field_data', 'c1');
     $query->innerJoin('comment_field_data', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
     $query->addExpression('COUNT(*)', 'count');
     $query->condition('c2.cid', $comment->id());
     if (!$this->currentUser->hasPermission('administer comments')) {
         $query->condition('c1.status', CommentInterface::PUBLISHED);
     }
     if ($comment_mode == CommentManagerInterface::COMMENT_MODE_FLAT) {
         // For rendering flat comments, cid is used for ordering comments due to
         // unpredictable behavior with timestamp, so we make the same assumption
         // here.
         $query->condition('c1.cid', $comment->id(), '<');
     } else {
         // For threaded comments, the c.thread column is used for ordering. We can
         // use the sorting code for comparison, but must remove the trailing
         // slash.
         $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) - 1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) - 1))');
     }
     $query->condition('c1.default_langcode', 1);
     $query->condition('c2.default_langcode', 1);
     $ordinal = $query->execute()->fetchField();
     return $divisor > 1 ? floor($ordinal / $divisor) : $ordinal;
 }
 /**
  * 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')));
 }
 /**
  * Performs the specified operation on the specified comment.
  *
  * @param \Drupal\comment\CommentInterface $comment
  *   Comment to perform operation on.
  * @param string $operation
  *   Operation to perform.
  * @param bool $approval
  *   Operation is found on approval page.
  */
 function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE)
 {
     $edit = array();
     $edit['operation'] = $operation;
     $edit['comments[' . $comment->id() . ']'] = TRUE;
     $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
     if ($operation == 'delete') {
         $this->drupalPostForm(NULL, array(), t('Delete comments'));
         $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', array('@operation' => $operation)));
     } else {
         $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', array('@operation' => $operation)));
     }
 }
 /**
  * Checks current page for specified comment.
  *
  * @param \Drupal\comment\CommentInterface $comment
  *   The comment object.
  * @param bool $reply
  *   Boolean indicating whether the comment is a reply to another comment.
  *
  * @return boolean
  *   Boolean indicating whether the comment was found.
  */
 function commentExists(CommentInterface $comment = NULL, $reply = FALSE)
 {
     if ($comment) {
         $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
         $regex .= '<a id="comment-' . $comment->id() . '"(.*?)';
         $regex .= $comment->getSubject() . '(.*?)';
         $regex .= $comment->comment_body->value . '(.*?)';
         $regex .= '/s';
         return (bool) preg_match($regex, $this->getRawContent());
     } else {
         return FALSE;
     }
 }
Ejemplo n.º 6
0
/**
 * Alter the links of a comment.
 *
 * @param array &$links
 *   A renderable array representing the comment links.
 * @param \Drupal\comment\CommentInterface $entity
 *   The comment being rendered.
 * @param array &$context
 *   Various aspects of the context in which the comment links are going to be
 *   displayed, with the following keys:
 *   - 'view_mode': the view mode in which the comment is being viewed
 *   - 'langcode': the language in which the comment is being viewed
 *   - 'commented_entity': the entity to which the comment is attached
 *
 * @see \Drupal\comment\CommentViewBuilder::renderLinks()
 * @see \Drupal\comment\CommentViewBuilder::buildLinks()
 */
function hook_comment_links_alter(array &$links, CommentInterface $entity, array &$context)
{
    $links['mymodule'] = array('#theme' => 'links__comment__mymodule', '#attributes' => array('class' => array('links', 'inline')), '#links' => array('comment-report' => array('title' => t('Report'), 'url' => Url::fromRoute('comment_test.report', ['comment' => $entity->id()], ['query' => ['token' => \Drupal::getContainer()->get('csrf_token')->get("comment/{$entity->id()}/report")]]), 'html' => TRUE)));
}
Ejemplo n.º 7
0
 /**
  * Returns comment delete form.
  *
  * @param \Drupal\comment\CommentInterface $comment
  *   The comment entity.
  *
  * @return \Drupal\Core\Ajax\AjaxResponse
  *   The Ajax response.
  */
 public function delete(CommentInterface $comment)
 {
     $response = new AjaxResponse();
     // Hide contents.
     $response->addCommand(new InvokeCommand('.comment-wrapper-' . $comment->id() . ' >*', 'hide'));
     // Replace comment with form.
     $form = $this->entityFormBuilder()->getForm($comment, 'delete');
     $response->addCommand(new ReplaceCommand('.comment-wrapper-' . $comment->id(), $form));
     return $response;
 }
Ejemplo n.º 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')));
 }