Chris@0: t("Number of views"), Chris@0: 'description' => t("The number of visitors who have read the node."), Chris@0: ]; Chris@0: $node['day-count'] = [ Chris@0: 'name' => t("Views today"), Chris@0: 'description' => t("The number of visitors who have read the node today."), Chris@0: ]; Chris@0: $node['last-view'] = [ Chris@0: 'name' => t("Last view"), Chris@0: 'description' => t("The date on which a visitor last read the node."), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'tokens' => ['node' => $node], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_tokens(). Chris@0: */ Chris@0: function statistics_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { Chris@0: $token_service = \Drupal::token(); Chris@0: Chris@0: $replacements = []; Chris@0: Chris@0: if ($type == 'node' & !empty($data['node'])) { Chris@0: $node = $data['node']; Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: if ($name == 'total-count') { Chris@0: $statistics = statistics_get($node->id()); Chris@0: $replacements[$original] = $statistics['totalcount']; Chris@0: } Chris@0: elseif ($name == 'day-count') { Chris@0: $statistics = statistics_get($node->id()); Chris@0: $replacements[$original] = $statistics['daycount']; Chris@0: } Chris@0: elseif ($name == 'last-view') { Chris@0: $statistics = statistics_get($node->id()); Chris@18: $replacements[$original] = \Drupal::service('date.formatter')->format($statistics['timestamp']); Chris@0: } Chris@0: } Chris@0: Chris@0: if ($created_tokens = $token_service->findWithPrefix($tokens, 'last-view')) { Chris@0: $statistics = statistics_get($node->id()); Chris@0: $replacements += $token_service->generate('date', $created_tokens, ['date' => $statistics['timestamp']], $options, $bubbleable_metadata); Chris@0: } Chris@0: } Chris@0: Chris@0: return $replacements; Chris@0: }