diff --git a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php index ace896b..0fc0813 100644 --- a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @@ -208,6 +208,9 @@ protected function nodeTitleList(array $nids, $title) { '#theme' => 'item_list__node', '#items' => $items, '#title' => $title, + '#cache' => [ + 'tags' => $this->entityTypeManager->getDefinition('node')->getListCacheTags(), + ], ]; } diff --git a/core/modules/statistics/src/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php index 4173179..ae07c76 100644 --- a/core/modules/statistics/src/StatisticsDatabaseStorage.php +++ b/core/modules/statistics/src/StatisticsDatabaseStorage.php @@ -70,14 +70,26 @@ public function recordView($id) { /** * {@inheritdoc} */ - public function fetchViews($id) { - $views = $this->connection + public function fetchViews($ids) { + $views = $this->connection ->select('node_counter', 'nc') ->fields('nc', ['totalcount', 'daycount', 'timestamp']) - ->condition('nid', $id, '=') + ->condition('nid', $ids, 'IN') ->execute() - ->fetchAssoc(); - return new StatisticsViewsResult($views['totalcount'], $views['daycount'], $views['timestamp']); + ->fetchAll(); + foreach ($views as $id => $view) { + $views[$id] = new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); + } + return $views; + } + + /** + * {@inheritdoc} + */ + public function fetchView($id) { + $views = $this->fetchViews(array($id)); + var_dump(reset($views)); + return reset($views); } /** diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index 39dee70..ccb51e4 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -22,14 +22,24 @@ public function recordView($id); /** - * Returns the number of times a entity has been viewed. + * Returns the number of times entities have been viewed. + * + * @param array $ids + * An array of IDs of entities to fetch the views for. + * + * @return array \Drupal\statistics\StatisticsViewsResult + */ + public function fetchViews($ids); + + /** + * Returns the number of times a single entity has been viewed. * * @param int $id * The ID of the entity to fetch the views for. * * @return \Drupal\statistics\StatisticsViewsResult */ - public function fetchViews($id); + public function fetchView($id); /** * Returns the number of times a entity has been viewed. diff --git a/core/modules/statistics/src/Tests/StatisticsReportsTest.php b/core/modules/statistics/src/Tests/StatisticsReportsTest.php index adf5bbf..0fe2e28 100644 --- a/core/modules/statistics/src/Tests/StatisticsReportsTest.php +++ b/core/modules/statistics/src/Tests/StatisticsReportsTest.php @@ -51,7 +51,7 @@ function testPopularContentBlock() { $tags = Cache::mergeTags($node->getCacheTags(), $block->getCacheTags()); $tags = Cache::mergeTags($tags, $this->blockingUser->getCacheTags()); - $tags = Cache::mergeTags($tags, ['block_view', 'config:block_list', 'rendered', 'user_view']); + $tags = Cache::mergeTags($tags, ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']); $this->assertCacheTags($tags); $contexts = Cache::mergeContexts($node->getCacheContexts(), $block->getCacheContexts()); $contexts = Cache::mergeContexts($contexts, ['url.query_args:_wrapper_format']); diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index 2e968b4..2a5a7d2 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -52,15 +52,16 @@ function statistics_node_links_alter(array &$links, NodeInterface $entity, array if ($context['view_mode'] != 'rss') { $links['#cache']['contexts'][] = 'user.permissions'; if (\Drupal::currentUser()->hasPermission('view post access counter')) { - $statistics = \Drupal::service('statistics.storage')->fetchViews($entity->id()); - $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); - $links['statistics'] = array( - '#theme' => 'links__node__statistics', - '#links' => $statistics_links, - '#attributes' => array('class' => array('links', 'inline')), - '#cache' => array('max-age' => \Drupal::config('statistics.settings')->get('display_max_age')), - ); - + $statistics = \Drupal::service('statistics.storage')->fetchView($entity->id()); + if ($statistics) { + $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); + $links['statistics'] = array( + '#theme' => 'links__node__statistics', + '#links' => $statistics_links, + '#attributes' => array('class' => array('links', 'inline')), + ); + } + $links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age'); } } } @@ -118,12 +119,12 @@ function statistics_title_list($dbfield, $dbrows) { * Retrieves a node's "view statistics". * * @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0. - * Use \Drupal::service('statistics.storage')->fetchViews($id). + * Use \Drupal::service('statistics.storage')->fetchView($id). */ function statistics_get($id) { if ($id > 0) { /** @var \Drupal\statistics\StatisticsViewsResult $statistics */ - $statistics = \Drupal::service('statistics.storage')->fetchViews($id); + $statistics = \Drupal::service('statistics.storage')->fetchView($id); return [ 'totalcount' => $statistics->getTotalCount(), 'daycount' => $statistics->getDayCount(),