You are here

function statistics_tokens in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/statistics/statistics.tokens.inc \statistics_tokens()

Implements hook_tokens().

File

core/modules/statistics/statistics.tokens.inc, line 36
Builds placeholder replacement tokens for node visitor statistics.

Code

function statistics_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();
  $replacements = array();
  if ($type == 'node' & !empty($data['node'])) {
    $node = $data['node'];
    foreach ($tokens as $name => $original) {
      if ($name == 'total-count') {
        $statistics = statistics_get($node
          ->id());
        $replacements[$original] = $statistics['totalcount'];
      }
      elseif ($name == 'day-count') {
        $statistics = statistics_get($node
          ->id());
        $replacements[$original] = $statistics['daycount'];
      }
      elseif ($name == 'last-view') {
        $statistics = statistics_get($node
          ->id());
        $replacements[$original] = format_date($statistics['timestamp']);
      }
    }
    if ($created_tokens = $token_service
      ->findWithPrefix($tokens, 'last-view')) {
      $statistics = statistics_get($node
        ->id());
      $replacements += $token_service
        ->generate('date', $created_tokens, array(
        'date' => $statistics['timestamp'],
      ), $options, $bubbleable_metadata);
    }
  }
  return $replacements;
}