function statistics_node_links_alter

Same name in other branches
  1. 9 core/modules/statistics/statistics.module \statistics_node_links_alter()
  2. 10 core/modules/statistics/statistics.module \statistics_node_links_alter()
  3. 11.x core/modules/statistics/statistics.module \statistics_node_links_alter()

Implements hook_node_links_alter().

File

core/modules/statistics/statistics.module, line 52

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.