' . t('About') . ''; $output .= '
' . 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 online documentation for the Statistics module.', [':statistics_do' => 'https://www.drupal.org/documentation/modules/statistics/']) . '
'; $output .= '' . t('Settings for the statistical information that Drupal will keep about the site.') . '
'; } } /** * Implements hook_ENTITY_TYPE_view() for node entities. */ function statistics_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) { if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) { $build['#attached']['library'][] = 'statistics/drupal.statistics'; $settings = ['data' => ['nid' => $node->id()], 'url' => Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')->toString()]; $build['#attached']['drupalSettings']['statistics'] = $settings; } } /** * Implements hook_node_links_alter(). */ function statistics_node_links_alter(array &$links, NodeInterface $entity, array &$context) { if ($context['view_mode'] != 'rss') { $links['#cache']['contexts'][] = 'user.permissions'; if (\Drupal::currentUser()->hasPermission('view post access counter')) { $statistics = \Drupal::service('statistics.storage.node')->fetchView($entity->id()); if ($statistics) { $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); $links['statistics'] = [ '#theme' => 'links__node__statistics', '#links' => $statistics_links, '#attributes' => ['class' => ['links', 'inline']], ]; } $links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age'); } } } /** * Implements hook_cron(). */ function statistics_cron() { $storage = \Drupal::service('statistics.storage.node'); $storage->resetDayCount(); $max_total_count = $storage->maxTotalCount(); \Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, $max_total_count)); } /** * Returns the most viewed content of all time, today, or the last-viewed node. * * @param string $dbfield * The database field to use, one of: * - 'totalcount': Integer that shows the top viewed content of all time. * - 'daycount': Integer that shows the top viewed content for today. * - 'timestamp': Integer that shows only the last viewed node. * @param int $dbrows * The number of rows to be returned. * * @return SelectQuery|false * A query result containing the node ID, title, user ID that owns the node, * and the username for the selected node(s), or FALSE if the query could not * be executed correctly. */ function statistics_title_list($dbfield, $dbrows) { if (in_array($dbfield, ['totalcount', 'daycount', 'timestamp'])) { $query = db_select('node_field_data', 'n'); $query->addTag('node_access'); $query->join('node_counter', 's', 'n.nid = s.nid'); $query->join('users_field_data', 'u', 'n.uid = u.uid'); return $query ->fields('n', ['nid', 'title']) ->fields('u', ['uid', 'name']) ->condition($dbfield, 0, '<>') ->condition('n.status', 1) // @todo This should be actually filtering on the desired node status // field language and just fall back to the default language. ->condition('n.default_langcode', 1) ->condition('u.default_langcode', 1) ->orderBy($dbfield, 'DESC') ->range(0, $dbrows) ->execute(); } return FALSE; } /** * Retrieves a node's "view statistics". * * @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0. * Use \Drupal::service('statistics.storage.node')->fetchView($id). */ 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(), 'timestamp' => $statistics->getTimestamp(), ]; } } /** * Implements hook_ENTITY_TYPE_predelete() for node entities. */ function statistics_node_predelete(EntityInterface $node) { // Clean up statistics table when node is deleted. $id = $node->id(); return \Drupal::service('statistics.storage.node')->deleteViews($id); } /** * Implements hook_ranking(). */ function statistics_ranking() { if (\Drupal::config('statistics.settings')->get('count_content_views')) { return [ 'views' => [ 'title' => t('Number of views'), 'join' => [ 'type' => 'LEFT', 'table' => 'node_counter', 'alias' => 'node_counter', 'on' => 'node_counter.nid = i.sid', ], // Inverse law that maps the highest view count on the site to 1 and 0 // to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite // in order to ensure that the :statistics_scale argument is treated as // a numeric type, because the PostgreSQL PDO driver sometimes puts // values in as strings instead of numbers in complex expressions like // this. 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (ROUND(:statistics_scale, 4)))', 'arguments' => [':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0], ], ]; } } /** * Implements hook_preprocess_HOOK() for block templates. */ function statistics_preprocess_block(&$variables) { if ($variables['configuration']['provider'] == 'statistics') { $variables['attributes']['role'] = 'navigation'; } } /** * Implements hook_block_alter(). * * Removes the "popular" block from display if the module is not configured * to count content views. */ function statistics_block_alter(&$definitions) { $statistics_count_content_views = \Drupal::config('statistics.settings')->get('count_content_views'); if (empty($statistics_count_content_views)) { unset($definitions['statistics_popular_block']); } }