Example #1
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;
 }
Example #2
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);
 }
Example #3
0
 protected function updateLast($comment, $oldRevisionId, $newRevisionId, $diffFieldName, $entity, CommentInterface $lastComment)
 {
     // If there is a new revision of the host entity, update it on the comment
     if (!empty($newRevisionId)) {
         // If the last comment did not store changes, store the current revisions pair
         if (empty($lastComment->{$diffFieldName}->right_rid)) {
             $lastComment->{$diffFieldName}->setValue([['left_rid' => $oldRevisionId, 'right_rid' => $newRevisionId]]);
         } else {
             $changes = $lastComment->{$diffFieldName}->getValue();
             $changes[0]['right_rid'] = $newRevisionId;
             $lastComment->{$diffFieldName}->setValue($changes);
         }
     }
     // Overwrite the old subject only if it was a change record.
     if ($lastComment->subject->value === 'Change Record') {
         $lastComment->setSubject($comment['subject']);
     }
     // Set all fields for which data has been passed
     // and there is no saved value
     foreach ($comment as $name => $value) {
         if (!isset($lastComment->{$name}->value)) {
             if (is_array($value)) {
                 $lastComment->{$name}->setValue($value);
             } else {
                 $lastComment->{$name}->value = $value;
             }
         }
     }
     $lastComment->setChangedTime(REQUEST_TIME);
     $lastComment->save();
 }
Example #4
0
 /**
  * Tests output for comment properties on nodes in full page view mode.
  *
  * @param \EasyRdf_Graph $graph
  *   The EasyRDF graph object.
  */
 protected function assertRdfaNodeCommentProperties($graph)
 {
     // Relationship between node and comment.
     $expected_value = array('type' => 'uri', 'value' => $this->articleCommentUri);
     $this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/comment', $expected_value), 'Relationship between node and comment found (schema:comment).');
     // Comment type.
     $this->assertEqual($graph->type($this->articleCommentUri), 'schema:Comment', 'Comment type was found (schema:Comment).');
     // Comment title.
     $expected_value = array('type' => 'literal', 'value' => $this->articleComment->get('subject')->value, 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/name', $expected_value), 'Article comment title was found (schema:name).');
     // Comment created date.
     $expected_value = array('type' => 'literal', 'value' => format_date($this->articleComment->get('created')->value, 'custom', 'c', 'UTC'), 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/dateCreated', $expected_value), 'Article comment created date was found (schema:dateCreated).');
     // Comment body.
     $text = $this->articleComment->get('comment_body')->value;
     $expected_value = array('type' => 'literal', 'value' => "{$text}\n", 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/text', $expected_value), 'Article comment body was found (schema:text).');
     // Comment uid.
     $expected_value = array('type' => 'uri', 'value' => $this->commenterUri);
     $this->assertTrue($graph->hasProperty($this->articleCommentUri, 'http://schema.org/author', $expected_value), 'Article comment author was found (schema:author).');
     // Comment author type.
     $this->assertEqual($graph->type($this->commenterUri), 'schema:Person', 'Comment author type was found (schema:Person).');
     // Comment author name.
     $expected_value = array('type' => 'literal', 'value' => $this->webUser->getUsername());
     $this->assertTrue($graph->hasProperty($this->commenterUri, 'http://schema.org/name', $expected_value), 'Comment author name was found (schema:name).');
 }
 /**
  * {@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('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('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->getChangedTime() : 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.
     $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->resetCache(array($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')));
 }
 /**
  * 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();
 }
 /**
  * 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;
     }
 }
Example #10
0
 /**
  * Verifies that a length violation exists for the given field.
  *
  * @param \Drupal\comment\CommentInterface $comment
  *   The comment object to validate.
  * @param string $field_name
  *   The field that violates the maximum length.
  * @param int $length
  *   Number of characters that was exceeded.
  */
 protected function assertLengthViolation(CommentInterface $comment, $field_name, $length)
 {
     $violations = $comment->validate();
     $this->assertEqual(count($violations), 1, "Violation found when {$field_name} is too long.");
     $this->assertEqual($violations[0]->getPropertyPath(), "{$field_name}.0.value");
     $field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
     $this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length)));
 }
Example #11
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');
 }
 /**
  * Helper function for testCommentRdfaMarkup().
  *
  * Tests the current page for basic comment RDFa markup.
  *
  * @param $comment
  *   Comment object.
  * @param $account
  *   An array containing information about an anonymous user.
  */
 function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = array())
 {
     $comment_uri = $comment->url('canonical', array('absolute' => TRUE));
     // Comment type.
     $expected_value = array('type' => 'uri', 'value' => 'http://rdfs.org/sioc/types#Comment');
     $this->assertTrue($graph->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioct:Comment).');
     // Comment type.
     $expected_value = array('type' => 'uri', 'value' => 'http://rdfs.org/sioc/ns#Post');
     $this->assertTrue($graph->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioc:Post).');
     // Comment title.
     $expected_value = array('type' => 'literal', 'value' => $comment->getSubject(), 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');
     // Comment date.
     $expected_value = array('type' => 'literal', 'value' => date('c', $comment->getCreatedTime()), 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime');
     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');
     // Comment date.
     $expected_value = array('type' => 'literal', 'value' => date('c', $comment->getCreatedTime()), 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime');
     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');
     // Comment body.
     $expected_value = array('type' => 'literal', 'value' => $comment->comment_body->value . "\n", 'lang' => 'en');
     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/rss/1.0/modules/content/encoded', $expected_value), 'Comment body found in RDF output (content:encoded).');
     // The comment author can be a registered user or an anonymous user.
     if ($comment->getOwnerId() > 0) {
         $author_uri = url('user/' . $comment->getOwnerId(), array('absolute' => TRUE));
         // Comment relation to author.
         $expected_value = array('type' => 'uri', 'value' => $author_uri);
         $this->assertTrue($graph->hasProperty($comment_uri, 'http://rdfs.org/sioc/ns#has_creator', $expected_value), 'Comment relation to author found in RDF output (sioc:has_creator).');
     } else {
         // The author is expected to be a blank node.
         $author_uri = $graph->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
         if ($author_uri instanceof \EasyRdf_Resource) {
             $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.');
         } else {
             $this->fail('Comment relation to author found in RDF output (sioc:has_creator).');
         }
     }
     // Author name.
     $name = empty($account["name"]) ? $this->web_user->getUsername() : $account["name"] . " (not verified)";
     $expected_value = array('type' => 'literal', 'value' => $name);
     $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).');
     // Comment author homepage (only for anonymous authors).
     if ($comment->getOwnerId() == 0) {
         $expected_value = array('type' => 'uri', 'value' => 'http://example.org/');
         $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
     }
 }
/**
 * 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)));
}
 /**
  * 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;
 }
 /**
  * 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')));
 }