public function CommentStatistics::create in Drupal 10
Same name and namespace in other branches
- 8 core/modules/comment/src/CommentStatistics.php \Drupal\comment\CommentStatistics::create()
- 9 core/modules/comment/src/CommentStatistics.php \Drupal\comment\CommentStatistics::create()
File
- core/modules/comment/src/CommentStatistics.php, line 104
Class
- CommentStatistics
Namespace
Drupal\comment
Code
public function create(FieldableEntityInterface $entity, $fields) {
$query = $this->database
->insert('comment_entity_statistics')
->fields([
'entity_id',
'entity_type',
'field_name',
'cid',
'last_comment_timestamp',
'last_comment_name',
'last_comment_uid',
'comment_count',
]);
foreach ($fields as $field_name => $detail) {
if (!$entity
->hasField($field_name)) {
continue;
}
$last_comment_uid = 0;
if ($entity instanceof EntityOwnerInterface) {
$last_comment_uid = $entity
->getOwnerId();
}
if (!isset($last_comment_uid)) {
$last_comment_uid = $this->currentUser
->id();
}
$last_comment_timestamp = REQUEST_TIME;
if ($entity instanceof EntityChangedInterface) {
$last_comment_timestamp = $entity
->getChangedTimeAcrossTranslations();
}
$query
->values([
'entity_id' => $entity
->id(),
'entity_type' => $entity
->getEntityTypeId(),
'field_name' => $field_name,
'cid' => 0,
'last_comment_timestamp' => $last_comment_timestamp,
'last_comment_name' => NULL,
'last_comment_uid' => $last_comment_uid,
'comment_count' => 0,
]);
}
$query
->execute();
}