View source
<?php
namespace Drupal\multiversion;
use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\comment\CommentStatistics as CoreCommentStatistics;
class CommentStatistics extends CoreCommentStatistics {
public function update(CommentInterface $comment) {
$storage_class = $comment
->getEntityType()
->getStorageClass();
if (is_subclass_of($storage_class, ContentEntityStorageInterface::class) !== FALSE) {
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) {
$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('c._deleted', FALSE)
->condition('default_langcode', 1)
->orderBy('c.created', 'DESC')
->range(0, 1)
->execute()
->fetchObject();
$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 {
$entity = $comment
->getCommentedEntity();
if ($entity instanceof EntityOwnerInterface) {
$last_comment_uid = $entity
->getOwnerId();
}
if (!isset($last_comment_uid)) {
$last_comment_uid = $this->currentUser
->id();
}
$this->database
->update('comment_entity_statistics')
->fields([
'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();
}
if (!$comment->_rev->is_stub) {
$this->entityManager
->getStorage($comment
->getCommentedEntityTypeId())
->resetCache([
$comment
->getCommentedEntityId(),
]);
}
}
else {
parent::update($comment);
}
}
}