/** * {@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'))); }
/** * {@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(); }
/** * 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'))); }