diff --git a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php index 31c7518..34e8c00 100755 --- a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Drupal\node\Entity\Node; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -45,6 +46,11 @@ class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPlugin protected $statisticsStorage; /** + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** * Constructs an StatisticsPopularBlock object. * * @param array $configuration @@ -60,11 +66,12 @@ class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPlugin * @param \Drupal\statistics\StatisticsStorageInterface $statistics_storage * The storage for statistics. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, StatisticsStorageInterface $statistics_storage) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, StatisticsStorageInterface $statistics_storage, RendererInterface $renderer) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; $this->entityRepository = $entity_repository; $this->statisticsStorage = $statistics_storage; + $this->renderer = $renderer; } /** @@ -77,7 +84,8 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('entity_type.manager'), $container->get('entity.repository'), - $container->get('statistics.storage') + $container->get('statistics.storage'), + $container->get('renderer') ); } @@ -187,15 +195,13 @@ protected function nodeTitleList(array $nids, $title) { $items = []; foreach ($nids as $nid) { $node = $this->entityRepository->getTranslationFromContext($nodes[$nid]); - $items[] = [ + $item = [ '#type' => 'link', '#title' => $node->getTitle(), '#url' => $node->urlInfo('canonical'), - '#cache' => [ - 'contexts' => $node->getCacheContexts(), - 'tags' => $node->getCacheTags(), - ], ]; + $this->renderer->addCacheableDependency($item, $node); + $items[] = $item; } return [ diff --git a/core/modules/statistics/src/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php index e479dcc..f7c317b 100644 --- a/core/modules/statistics/src/StatisticsDatabaseStorage.php +++ b/core/modules/statistics/src/StatisticsDatabaseStorage.php @@ -71,13 +71,13 @@ public function recordHit($nid) { * {@inheritdoc} */ public function fetchViews($nid) { - // Retrieve an array, which includes totalcount, daycount, and timestamp. - return $this->connection + $views = $this->connection ->select('node_counter', 'nc') ->fields('nc', ['totalcount', 'daycount', 'timestamp']) ->condition('nid', $nid, '=') ->execute() ->fetchAssoc(); + return new StatisticsViews($views['totalcount'], $views['daycount'], $views['timestamp']); } /** diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index 9f992ab..eeb1e7e 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -32,13 +32,7 @@ public function recordHit($nid); * @param int $nid * The ID of the node to fetch the views for. * - * @return array - * An associative array containing: - * - totalcount: Integer for the total number of times the node has been - * viewed. - * - daycount: Integer for the total number of times the node has been - * viewed "today". For the daycount to be reset, cron must be enabled. - * - timestamp: Integer for the timestamp of when the node was last viewed. + * @return \Drupal\statistics\StatisticsViewsInterface */ public function fetchViews($nid); diff --git a/core/modules/statistics/src/StatisticsViews.php b/core/modules/statistics/src/StatisticsViews.php index e69de29..8f64d3d 100644 --- a/core/modules/statistics/src/StatisticsViews.php +++ b/core/modules/statistics/src/StatisticsViews.php @@ -0,0 +1,48 @@ +totalCount = $total_count; + $this->dayCount = $day_count; + $this->timestamp = $timestamp; + } + + /** + * {@inheritdoc} + */ + public function getTotalCount() { + return $this->totalCount; + } + + /** + * {@inheritdoc} + */ + public function getDayCount() { + return $this->dayCount; + } + + /** + * {@inheritdoc} + */ + public function getTimestamp() { + return $this->timestamp; + } +} \ No newline at end of file diff --git a/core/modules/statistics/src/StatisticsViewsInterface.php b/core/modules/statistics/src/StatisticsViewsInterface.php index e69de29..2db24d3 100644 --- a/core/modules/statistics/src/StatisticsViewsInterface.php +++ b/core/modules/statistics/src/StatisticsViewsInterface.php @@ -0,0 +1,28 @@ +hasPermission('view post access counter')) { $statistics = \Drupal::service('statistics.storage')->fetchViews($entity->id()); - if ($statistics) { - $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics['totalcount'], '1 view', '@count views'); + if ($statistics instanceof \Drupal\statistics\StatisticsViewsInterface) { + $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); $links['statistics'] = [ '#theme' => 'links__node__statistics', '#links' => $statistics_links, @@ -74,19 +74,24 @@ function statistics_cron() { if ($storage->needsReset()) { $storage->resetDayCount(); } - - $max_total_count = $storage->maxTotalCount(); + $storage->maxTotalCount(); } /** * Retrieves a node's "view statistics". * - * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. + * @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0. * Use \Drupal::service('statistics.storage')->fetchViews($nid). */ function statistics_get($nid) { if ($nid > 0) { - return \Drupal::service('statistics.storage')->fetchViews($nid); + /** @var \Drupal\statistics\StatisticsViewsInterface $statistics */ + $statistics = \Drupal::service('statistics.storage')->fetchViews($nid); + return [ + 'totalcount' => $statistics->getTotalCount(), + 'daycount' => $statistics->getDayCount(), + 'timestamp' => $statistics->getTimestamp(), + ]; } }