public function CommentManager::getCountNewComments in Drupal 10
Same name and namespace in other branches
- 8 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::getCountNewComments()
- 9 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::getCountNewComments()
File
- core/
modules/ comment/ src/ CommentManager.php, line 198
Class
- CommentManager
- Comment manager contains common functions to manage comment fields.
Namespace
Drupal\commentCode
public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) {
// @todo Replace module handler with optional history service injection
// after https://www.drupal.org/node/2081585.
if ($this->currentUser
->isAuthenticated() && $this->moduleHandler
->moduleExists('history')) {
// Retrieve the timestamp at which the current user last viewed this entity.
if (!$timestamp) {
if ($entity
->getEntityTypeId() == 'node') {
$timestamp = history_read($entity
->id());
}
else {
$function = $entity
->getEntityTypeId() . '_last_viewed';
if (function_exists($function)) {
$timestamp = $function($entity
->id());
}
else {
// Default to 30 days ago.
// @todo Remove once https://www.drupal.org/node/1029708 lands.
$timestamp = COMMENT_NEW_LIMIT;
}
}
}
$timestamp = $timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT;
// Use the timestamp to retrieve the number of new comments.
$query = $this->entityTypeManager
->getStorage('comment')
->getQuery()
->accessCheck(TRUE)
->condition('entity_type', $entity
->getEntityTypeId())
->condition('entity_id', $entity
->id())
->condition('created', $timestamp, '>')
->condition('status', CommentInterface::PUBLISHED);
if ($field_name) {
// Limit to a particular field.
$query
->condition('field_name', $field_name);
}
return $query
->count()
->execute();
}
return FALSE;
}