Commit c2f6edd6 authored by cilefen's avatar cilefen

Issue #2867493 by xjm, alexpott, connectedrobots, webchick: Error: Call to a...

Issue #2867493 by xjm, alexpott, connectedrobots, webchick: Error: Call to a member function getTotalCount() on boolean in statistics_get()
parent 1512ec2e
......@@ -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);
......
......@@ -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);
}
}
......@@ -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(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment