public function MongodbCommentStatistics::create in MongoDB 8
Insert an empty record for the given entity.
Parameters
\Drupal\Core\Entity\FieldableEntityInterface $entity: The created entity for which a statistics record is to be initialized.
array $fields: Array of comment field definitions for the given entity.
Overrides CommentStatisticsInterface::create
File
- mongodb_comment/
src/ MongodbCommentStatistics.php, line 102 - Contains \Drupal\comment\CommentStatistics.
Class
Namespace
Drupal\mongodb_commentCode
public function create(FieldableEntityInterface $entity, $fields) {
return;
$query = $this->database
->insert('comment_entity_statistics')
->fields(array(
'entity_id',
'entity_type',
'field_name',
'cid',
'last_comment_timestamp',
'last_comment_name',
'last_comment_uid',
'comment_count',
));
foreach ($fields as $field_name => $detail) {
// Skip fields that entity does not have.
if (!$entity
->hasField($field_name)) {
continue;
}
// Get the user ID from the entity if it's set, or default to the
// currently logged in user.
$last_comment_uid = 0;
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();
}
// Default to REQUEST_TIME when entity does not have a changed property.
$last_comment_timestamp = REQUEST_TIME;
if ($entity instanceof EntityChangedInterface) {
$last_comment_timestamp = $entity
->getChangedTime();
}
$query
->values(array(
'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();
}