diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index ccb51e4..ad6d6d5 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -27,7 +27,10 @@ public function recordView($id); * @param array $ids * An array of IDs of entities to fetch the views for. * - * @return array \Drupal\statistics\StatisticsViewsResult + * @return \Drupal\statistics\StatisticsViewsResult[] + * An array of value objects representing the number of times each entity + * has been viewed. The array is keyed by entity ID. If an ID does not + * exist, it will not be present in the array. */ public function fetchViews($ids); @@ -37,7 +40,9 @@ public function fetchViews($ids); * @param int $id * The ID of the entity to fetch the views for. * - * @return \Drupal\statistics\StatisticsViewsResult + * @return \Drupal\statistics\StatisticsViewsResult|false + * If the entity exists, a value object representing the number of times if + * has been viewed. If it does not exist, FALSE is returned. */ public function fetchView($id); diff --git a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php index 152c95a..6f2f667 100644 --- a/core/modules/statistics/src/Tests/StatisticsLoggingTest.php +++ b/core/modules/statistics/src/Tests/StatisticsLoggingTest.php @@ -3,6 +3,7 @@ namespace Drupal\statistics\Tests; use Drupal\simpletest\WebTestBase; +use Drupal\node\Entity\Node; /** * Tests request logging for cached and uncached pages. @@ -125,6 +126,17 @@ public function testLogging() { $this->client->post($base_root . $stats_path, ['form_params' => $post]); $node_counter = statistics_get($this->node->id()); $this->assertIdentical($node_counter['totalcount'], '1'); + + // Try fetching statistics for an invalid node ID and verify it returns + // FALSE. + $node_id = 10000000000000000; + $node = Node::load($node_id); + $this->assertNull($node); + + // This is a test specifically for the deprecated statistics_get() function + // and so should remain unconverted until that function is removed. + $result = statistics_get($node_id); + $this->assertIdentical($result, FALSE); } } diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index a7f1e10..4d7f42a 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -10,6 +10,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\node\NodeInterface; +use Drupal\statistics\StatisticsViewsResult; /** * Implements hook_help(). @@ -125,6 +126,12 @@ function statistics_get($id) { if ($id > 0) { /** @var \Drupal\statistics\StatisticsViewsResult $statistics */ $statistics = \Drupal::service('statistics.storage.node')->fetchView($id); + + // For backwards compatibility, return FALSE if an invalid node ID was + // passed in. + if (!($statistics instanceof StatisticsViewsResult)) { + return FALSE; + } return [ 'totalcount' => $statistics->getTotalCount(), 'daycount' => $statistics->getDayCount(),