statistics.tokens.inc

Builds placeholder replacement tokens for node visitor statistics.

File

drupal-7.x/modules/statistics/statistics.tokens.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for node visitor statistics.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function statistics_token_info() {
  10. $node['total-count'] = array(
  11. 'name' => t("Number of views"),
  12. 'description' => t("The number of visitors who have read the node."),
  13. );
  14. $node['day-count'] = array(
  15. 'name' => t("Views today"),
  16. 'description' => t("The number of visitors who have read the node today."),
  17. );
  18. $node['last-view'] = array(
  19. 'name' => t("Last view"),
  20. 'description' => t("The date on which a visitor last read the node."),
  21. 'type' => 'date',
  22. );
  23. return array(
  24. 'tokens' => array('node' => $node),
  25. );
  26. }
  27. /**
  28. * Implements hook_tokens().
  29. */
  30. function statistics_tokens($type, $tokens, array $data = array(), array $options = array()) {
  31. $url_options = array('absolute' => TRUE);
  32. $replacements = array();
  33. if ($type == 'node' & !empty($data['node'])) {
  34. $node = $data['node'];
  35. foreach ($tokens as $name => $original) {
  36. if ($name == 'total-count') {
  37. $statistics = statistics_get($node->nid);
  38. $replacements[$original] = $statistics['totalcount'];
  39. }
  40. elseif ($name == 'day-count') {
  41. $statistics = statistics_get($node->nid);
  42. $replacements[$original] = $statistics['daycount'];
  43. }
  44. elseif ($name == 'last-view') {
  45. $statistics = statistics_get($node->nid);
  46. $replacements[$original] = format_date($statistics['timestamp']);
  47. }
  48. }
  49. if ($created_tokens = token_find_with_prefix($tokens, 'last-view')) {
  50. $statistics = statistics_get($node->nid);
  51. $replacements += token_generate('date', $created_tokens, array('date' => $statistics['timestamp']), $options);
  52. }
  53. }
  54. return $replacements;
  55. }