diff --git a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php index 8e124ad..31c7518 100755 --- a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @@ -4,12 +4,13 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Cache\Cache; +use Drupal\node\Entity\Node; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Entity\EntityManagerInterface; use Drupal\statistics\StatisticsStorageInterface; /** @@ -23,11 +24,18 @@ class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPluginInterface { /** - * The entity manager service. + * The entity type manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityManager; + protected $entityTypeManager; + + /** + * The entity repository service. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; /** * The storage for statistics. @@ -45,14 +53,17 @@ class StatisticsPopularBlock extends BlockBase implements ContainerFactoryPlugin * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository service * @param \Drupal\statistics\StatisticsStorageInterface $statistics_storage * The storage for statistics. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, StatisticsStorageInterface $statistics_storage) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, StatisticsStorageInterface $statistics_storage) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->entityManager = $entity_manager; + $this->entityTypeManager = $entity_type_manager; + $this->entityRepository = $entity_repository; $this->statisticsStorage = $statistics_storage; } @@ -64,8 +75,9 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager'), - $container->get('statistics.statistics_storage') + $container->get('entity_type.manager'), + $container->get('entity.repository'), + $container->get('statistics.storage') ); } @@ -161,7 +173,7 @@ public function build() { /** * Generates the ordered array of node links for build(). * - * @param array $nids + * @param int[] $nids * An ordered array of node ids. * @param string $title * The title for the list. @@ -169,12 +181,12 @@ public function build() { * @return array * A render array for the list. */ - protected function nodeTitleList($nids, $title) { - $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids); + protected function nodeTitleList(array $nids, $title) { + $nodes = Node::loadMultiple($nids); $items = []; foreach ($nids as $nid) { - $node = $this->entityManager->getTranslationFromContext($nodes[$nid]); + $node = $this->entityRepository->getTranslationFromContext($nodes[$nid]); $items[] = [ '#type' => 'link', '#title' => $node->getTitle(), @@ -191,7 +203,7 @@ protected function nodeTitleList($nids, $title) { '#items' => $items, '#title' => $title, '#cache' => [ - 'tags' => $this->entityManager->getDefinition('node')->getListCacheTags(), + 'tags' => $this->entityTypeManager->getDefinition('node')->getListCacheTags(), ], ]; } diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index feddc66..9f992ab 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -36,8 +36,8 @@ public function recordHit($nid); * 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. + * - 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. */ public function fetchViews($nid); diff --git a/core/modules/statistics/src/Tests/StatisticsReportsTest.php b/core/modules/statistics/src/Tests/StatisticsReportsTest.php index 839d447..fcb2a40 100644 --- a/core/modules/statistics/src/Tests/StatisticsReportsTest.php +++ b/core/modules/statistics/src/Tests/StatisticsReportsTest.php @@ -49,18 +49,10 @@ function testPopularContentBlock() { $this->assertText('All time', 'Found the all time popular content.'); $this->assertText('Last viewed', 'Found the last viewed popular content.'); - $this->assertCacheTags( - Cache::mergeTags( - Cache::mergeTags( - Cache::mergeTags( - $node->getCacheTags(), - $block->getCacheTags() - ), - $this->blockingUser->getCacheTags() - ), - ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view'] - ) - ); + $tags = Cache::mergeTags($node->getCacheTags(), $block->getCacheTags()); + $tags = Cache::mergeTags($tags, $this->blockingUser->getCacheTags()); + $tags = Cache::mergeTags($tags, ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']); + $this->assertCacheTags($tags); $this->assertCacheContexts($node->getCacheContexts()); // Check if the node link is displayed. diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index 4a91bc1..bbe6f3e 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -52,7 +52,7 @@ 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.statistics_storage')->fetchViews($entity->id()); + $statistics = \Drupal::service('statistics.storage')->fetchViews($entity->id()); if ($statistics) { $links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics['totalcount'], '1 view', '@count views'); $node_links['statistics'] = [ @@ -70,7 +70,7 @@ function statistics_node_links_alter(array &$links, NodeInterface $entity, array * Implements hook_cron(). */ function statistics_cron() { - $storage = \Drupal::service('statistics.statistics_storage'); + $storage = \Drupal::service('statistics.storage'); if ($storage->needsReset()) { $storage->resetDayCount(); } @@ -82,11 +82,11 @@ function statistics_cron() { * Retrieves a node's "view statistics". * * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. - * Use \Drupal::service('statistics.statistics_storage')->fetchViews($nid). + * Use \Drupal::service('statistics.storage')->fetchViews($nid). */ function statistics_get($nid) { if ($nid > 0) { - return \Drupal::service('statistics.statistics_storage')->fetchViews($nid); + return \Drupal::service('statistics.storage')->fetchViews($nid); } } @@ -96,7 +96,7 @@ function statistics_get($nid) { function statistics_node_predelete(EntityInterface $node) { // Clean up statistics table when node is deleted. $nid = $node->id(); - return \Drupal::service('statistics.statistics_storage')->clean($nid); + return \Drupal::service('statistics.storage')->clean($nid); } /** diff --git a/core/modules/statistics/statistics.php b/core/modules/statistics/statistics.php index 4b3452b..edb1cc4 100644 --- a/core/modules/statistics/statistics.php +++ b/core/modules/statistics/statistics.php @@ -25,6 +25,6 @@ $nid = filter_input(INPUT_POST, 'nid', FILTER_VALIDATE_INT); if ($nid) { $container->get('request_stack')->push(Request::createFromGlobals()); - $container->get('statistics.statistics_storage')->recordHit($nid); + $container->get('statistics.storage')->recordHit($nid); } } diff --git a/core/modules/statistics/statistics.services.yml b/core/modules/statistics/statistics.services.yml index 3fdd40b..b034780 100644 --- a/core/modules/statistics/statistics.services.yml +++ b/core/modules/statistics/statistics.services.yml @@ -1,5 +1,5 @@ services: - statistics.statistics_storage: + statistics.storage: class: Drupal\statistics\StatisticsDatabaseStorage arguments: ['@database', '@state', '@request_stack'] tags: