You are here

function statistics_node_links_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/statistics/statistics.module \statistics_node_links_alter()

Implements hook_node_links_alter().

File

core/modules/statistics/statistics.module, line 51
Logs and displays content statistics for a site.

Code

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');
    }
  }
}