function statistics_tokens
Same name in other branches
- 7.x modules/statistics/statistics.tokens.inc \statistics_tokens()
- 9 core/modules/statistics/statistics.tokens.inc \statistics_tokens()
- 10 core/modules/statistics/statistics.tokens.inc \statistics_tokens()
- 11.x core/modules/statistics/statistics.tokens.inc \statistics_tokens()
Implements hook_tokens().
File
-
core/
modules/ statistics/ statistics.tokens.inc, line 36
Code
function statistics_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
$replacements = [];
if ($type == 'node' & !empty($data['node'])) {
$node = $data['node'];
/** @var \Drupal\statistics\StatisticsStorageInterface $stats_storage */
$stats_storage = \Drupal::service('statistics.storage.node');
foreach ($tokens as $name => $original) {
if ($name == 'total-count') {
$replacements[$original] = $stats_storage->fetchView($node->id())
->getTotalCount();
}
elseif ($name == 'day-count') {
$replacements[$original] = $stats_storage->fetchView($node->id())
->getDayCount();
}
elseif ($name == 'last-view') {
$replacements[$original] = \Drupal::service('date.formatter')->format($stats_storage->fetchView($node->id())
->getTimestamp());
}
}
if ($created_tokens = $token_service->findWithPrefix($tokens, 'last-view')) {
$replacements += $token_service->generate('date', $created_tokens, [
'date' => $stats_storage->fetchView($node->id())
->getTimestamp(),
], $options, $bubbleable_metadata);
}
}
return $replacements;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.