5 * Logs and displays content statistics for a site.
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
12 use Drupal\node\NodeInterface;
13 use Drupal\statistics\StatisticsViewsResult;
16 * Implements hook_help().
18 function statistics_help($route_name, RouteMatchInterface $route_match) {
19 switch ($route_name) {
20 case 'help.page.statistics':
22 $output .= '<h3>' . t('About') . '</h3>';
23 $output .= '<p>' . t('The Statistics module shows you how often content is viewed. This is useful in determining which pages of your site are most popular. For more information, see the <a href=":statistics_do">online documentation for the Statistics module</a>.', [':statistics_do' => 'https://www.drupal.org/documentation/modules/statistics/']) . '</p>';
24 $output .= '<h3>' . t('Uses') . '</h3>';
26 $output .= '<dt>' . t('Displaying popular content') . '</dt>';
27 $output .= '<dd>' . t('The module includes a <em>Popular content</em> block that displays the most viewed pages today and for all time, and the last content viewed. To use the block, enable <em>Count content views</em> on the <a href=":statistics-settings">Statistics page</a>, and then you can enable and configure the block on the <a href=":blocks">Block layout page</a>.', [':statistics-settings' => \Drupal::url('statistics.settings'), ':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</dd>';
28 $output .= '<dt>' . t('Page view counter') . '</dt>';
29 $output .= '<dd>' . t('The Statistics module includes a counter for each page that increases whenever the page is viewed. To use the counter, enable <em>Count content views</em> on the <a href=":statistics-settings">Statistics page</a>, and set the necessary <a href=":permissions">permissions</a> (<em>View content hits</em>) so that the counter is visible to the users.', [':statistics-settings' => \Drupal::url('statistics.settings'), ':permissions' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-statistics'])]) . '</dd>';
33 case 'statistics.settings':
34 return '<p>' . t('Settings for the statistical information that Drupal will keep about the site.') . '</p>';
39 * Implements hook_ENTITY_TYPE_view() for node entities.
41 function statistics_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
42 if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
43 $build['#attached']['library'][] = 'statistics/drupal.statistics';
44 $settings = ['data' => ['nid' => $node->id()], 'url' => Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')->toString()];
45 $build['#attached']['drupalSettings']['statistics'] = $settings;
50 * Implements hook_node_links_alter().
52 function statistics_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
53 if ($context['view_mode'] != 'rss') {
54 $links['#cache']['contexts'][] = 'user.permissions';
55 if (\Drupal::currentUser()->hasPermission('view post access counter')) {
56 $statistics = \Drupal::service('statistics.storage.node')->fetchView($entity->id());
58 $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views');
59 $links['statistics'] = [
60 '#theme' => 'links__node__statistics',
61 '#links' => $statistics_links,
62 '#attributes' => ['class' => ['links', 'inline']],
65 $links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age');
71 * Implements hook_cron().
73 function statistics_cron() {
74 $storage = \Drupal::service('statistics.storage.node');
75 $storage->resetDayCount();
76 $max_total_count = $storage->maxTotalCount();
77 \Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, $max_total_count));
81 * Returns the most viewed content of all time, today, or the last-viewed node.
83 * @param string $dbfield
84 * The database field to use, one of:
85 * - 'totalcount': Integer that shows the top viewed content of all time.
86 * - 'daycount': Integer that shows the top viewed content for today.
87 * - 'timestamp': Integer that shows only the last viewed node.
89 * The number of rows to be returned.
91 * @return SelectQuery|false
92 * A query result containing the node ID, title, user ID that owns the node,
93 * and the username for the selected node(s), or FALSE if the query could not
94 * be executed correctly.
96 function statistics_title_list($dbfield, $dbrows) {
97 if (in_array($dbfield, ['totalcount', 'daycount', 'timestamp'])) {
98 $query = db_select('node_field_data', 'n');
99 $query->addTag('node_access');
100 $query->join('node_counter', 's', 'n.nid = s.nid');
101 $query->join('users_field_data', 'u', 'n.uid = u.uid');
104 ->fields('n', ['nid', 'title'])
105 ->fields('u', ['uid', 'name'])
106 ->condition($dbfield, 0, '<>')
107 ->condition('n.status', 1)
108 // @todo This should be actually filtering on the desired node status
109 // field language and just fall back to the default language.
110 ->condition('n.default_langcode', 1)
111 ->condition('u.default_langcode', 1)
112 ->orderBy($dbfield, 'DESC')
120 * Retrieves a node's "view statistics".
122 * @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0.
123 * Use \Drupal::service('statistics.storage.node')->fetchView($id).
125 function statistics_get($id) {
127 /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
128 $statistics = \Drupal::service('statistics.storage.node')->fetchView($id);
130 // For backwards compatibility, return FALSE if an invalid node ID was
132 if (!($statistics instanceof StatisticsViewsResult)) {
136 'totalcount' => $statistics->getTotalCount(),
137 'daycount' => $statistics->getDayCount(),
138 'timestamp' => $statistics->getTimestamp(),
144 * Implements hook_ENTITY_TYPE_predelete() for node entities.
146 function statistics_node_predelete(EntityInterface $node) {
147 // Clean up statistics table when node is deleted.
149 return \Drupal::service('statistics.storage.node')->deleteViews($id);
153 * Implements hook_ranking().
155 function statistics_ranking() {
156 if (\Drupal::config('statistics.settings')->get('count_content_views')) {
159 'title' => t('Number of views'),
162 'table' => 'node_counter',
163 'alias' => 'node_counter',
164 'on' => 'node_counter.nid = i.sid',
166 // Inverse law that maps the highest view count on the site to 1 and 0
167 // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
168 // in order to ensure that the :statistics_scale argument is treated as
169 // a numeric type, because the PostgreSQL PDO driver sometimes puts
170 // values in as strings instead of numbers in complex expressions like
172 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (ROUND(:statistics_scale, 4)))',
173 'arguments' => [':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0],
180 * Implements hook_preprocess_HOOK() for block templates.
182 function statistics_preprocess_block(&$variables) {
183 if ($variables['configuration']['provider'] == 'statistics') {
184 $variables['attributes']['role'] = 'navigation';
189 * Implements hook_block_alter().
191 * Removes the "popular" block from display if the module is not configured
192 * to count content views.
194 function statistics_block_alter(&$definitions) {
195 $statistics_count_content_views = \Drupal::config('statistics.settings')->get('count_content_views');
196 if (empty($statistics_count_content_views)) {
197 unset($definitions['statistics_popular_block']);