function CommentStatistics::update
Same name in other branches
- 9 core/modules/comment/src/CommentStatistics.php \Drupal\comment\CommentStatistics::update()
- 8.9.x core/modules/comment/src/CommentStatistics.php \Drupal\comment\CommentStatistics::update()
- 10 core/modules/comment/src/CommentStatistics.php \Drupal\comment\CommentStatistics::update()
File
-
core/
modules/ comment/ src/ CommentStatistics.php, line 200
Class
Namespace
Drupal\commentCode
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', [
'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([
'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([
'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([
'cid' => 0,
'comment_count' => 0,
// Use the changed date of the entity if it's set, or default to
// request time.
'last_comment_timestamp' => $entity instanceof EntityChangedInterface ? $entity->getChangedTimeAcrossTranslations() : $this->time
->getRequestTime(),
'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->entityTypeManager
->getStorage($comment->getCommentedEntityTypeId())
->resetCache([
$comment->getCommentedEntityId(),
]);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.