diff -u b/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php --- b/core/modules/comment/src/CommentManager.php +++ b/core/modules/comment/src/CommentManager.php @@ -3,6 +3,7 @@ namespace Drupal\comment; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; @@ -58,11 +59,18 @@ protected $currentUser; /** - * History mark read time. + * History modules settings config. * - * @var int + * @var \Drupal\Core\Config\ImmutableConfig */ - protected $historyMarkRead; + protected $historyConfig; + + /** + * Time manager. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; /** * Construct the CommentManager object. @@ -77,19 +85,22 @@ * The module handler service. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. + * @param \Drupal\Component\Datetime\TimeInterface $time + * Time manager. */ - public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { + public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, TimeInterface $time) { $this->entityManager = $entity_manager; $this->userConfig = $config_factory->get('user.settings'); $this->stringTranslation = $string_translation; $this->moduleHandler = $module_handler; $this->currentUser = $current_user; + $this->historyConfig = $config_factory->get('history.settings'); + $this->time = $time; } /** $this->moduleHandler = $module_handler; $this->currentUser = $current_user; - $this->historyMarkRead = $config_factory->get('history.settings')->get('history_mark_read'); } /** @@ -201,7 +212,7 @@ } } } - $history_read_limit = REQUEST_TIME - $this->historyMarkRead; + $history_read_limit = $this->time->getRequestTime() - $this->historyConfig->get('history_mark_read'); $timestamp = ($timestamp > $history_read_limit ? $timestamp : $history_read_limit); // Use the timestamp to retrieve the number of new comments. diff -u b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php --- b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php +++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php @@ -2,6 +2,7 @@ namespace Drupal\comment\Plugin\views\field; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Database\Connection; use Drupal\comment\CommentInterface; @@ -37,11 +38,18 @@ protected $database; /** - * History read limit time. + * History config. * - * @var int + * @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig */ - protected $historyReadLimit; + protected $historyConfig; + + /** + * Time manager. + * + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $time; /** * Constructs a \Drupal\comment\Plugin\views\field\NodeNewComments object. @@ -56,19 +64,22 @@ * Database Service Object. * @param \Drupal\Core\Config\ConfigFactory $configFactory * Config factory service. + * @param \Drupal\Component\Datetime\TimeInterface $time + * Time manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, ConfigFactory $configFactory) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, ConfigFactory $configFactory, TimeInterface $time) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->database = $database; - $this->historyReadLimit = REQUEST_TIME - $configFactory->get('history.settings')->get('history_mark_read'); + $this->historyConfig = $configFactory->get('history.settings'); + $this->time = $time; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'), $container->get('config.factory')); + return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'), $container->get('config.factory'), $container->get('datetime.time')); } /** @@ -138,14 +149,15 @@ } if ($nids) { + $history_read_limit = $this->time->getRequestTime() - $this->historyConfig->get('history_mark_read'); $result = $this->database->query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment_field_data} c ON n.nid = c.entity_id AND c.entity_type = 'node' AND c.default_langcode = 1 LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN ( :nids[] ) AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp1), :timestamp2) AND c.status = :status GROUP BY n.nid", [ ':status' => CommentInterface::PUBLISHED, ':h_uid' => $user->id(), ':nids[]' => $nids, - ':timestamp1' => $this->historyReadLimit, - ':timestamp2' => $this->historyReadLimit, + ':timestamp1' => $history_read_limit, + ':timestamp2' => $history_read_limit, ]); foreach ($result as $node) { foreach ($ids[$node->nid] as $id) {