Lines: 97.67%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Hook; | |
| 6 | ||
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 8 | use Drupal\Core\Datetime\DateFormatterInterface; | |
| 9 | use Drupal\Core\Hook\Attribute\Hook; | |
| 10 | use Drupal\Core\Render\BubbleableMetadata; | |
| 11 | use Drupal\Core\StringTranslation\StringTranslationTrait; | |
| 12 | use Drupal\Core\StringTranslation\TranslationInterface; | |
| 13 | use Drupal\Core\Utility\Token; | |
| 14 | use Drupal\visitors\VisitorsCounterInterface; | |
| 15 | ||
| 16 | /** | |
| 17 | * Implements visitor counter token hooks. | |
| 18 | */ | |
| 19 | class TokenHooks { | |
| 20 | use StringTranslationTrait; | |
| 21 | ||
| 22 | /** | |
| 23 | * Constructs a TokenHooks object. | |
| 24 | * | |
| 25 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | |
| 26 | * The config factory. | |
| 27 | * @param \Drupal\Core\Utility\Token $token | |
| 28 | * The token service. | |
| 29 | * @param \Drupal\visitors\VisitorsCounterInterface $counter | |
| 30 | * The visitors counter. | |
| 31 | * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter | |
| 32 | * The date formatter. | |
| 33 | * @param \Drupal\Core\StringTranslation\TranslationInterface $translation | |
| 34 | * The string translation service. | |
| 35 | */ | |
| 36 | public function __construct( | |
| 37 | protected ConfigFactoryInterface $configFactory, | |
| 38 | protected Token $token, | |
| 39 | protected VisitorsCounterInterface $counter, | |
| 40 | protected DateFormatterInterface $dateFormatter, | |
| 41 | TranslationInterface $translation, | |
| 42 | ) { | |
| 43 | 3 | $this->setStringTranslation($translation); |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Implements hook_token_info(). | |
| 48 | */ | |
| 49 | #[Hook('token_info')] | |
| 50 | public function tokenInfo() { | |
| 51 | 1 | $entity['total-count'] = [ |
| 52 | 1 | 'name' => $this->t("Number of views"), |
| 53 | 1 | 'description' => $this->t("The number of visitors who have read the node."), |
| 54 | 1 | ]; |
| 55 | 1 | $entity['day-count'] = [ |
| 56 | 1 | 'name' => $this->t("Views today"), |
| 57 | 1 | 'description' => $this->t("The number of visitors who have read the node today."), |
| 58 | 1 | ]; |
| 59 | 1 | $entity['last-view'] = [ |
| 60 | 1 | 'name' => $this->t("Last view"), |
| 61 | 1 | 'description' => $this->t("The date on which a visitor last read the node."), |
| 62 | 1 | 'type' => 'date', |
| 63 | 1 | ]; |
| 64 | 1 | $token = [ |
| 65 | 1 | 'tokens' => [], |
| 66 | 1 | ]; |
| 67 | 1 | $entity_types = $this->configFactory->get('visitors.settings')->get('counter.entity_types') ?? []; |
| 68 | 1 | foreach ($entity_types as $entity_type) { |
| 69 | 1 | $token['tokens'][$entity_type] = $entity; |
| 70 | } | |
| 71 | 1 | return $token; |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * Implements hook_tokens(). | |
| 76 | */ | |
| 77 | #[Hook('tokens')] | |
| 78 | public function tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { | |
| 79 | 2 | $token_service = $this->token; |
| 80 | 2 | $entity_types = $this->configFactory->get('visitors.settings')->get('counter.entity_types') ?? []; |
| 81 | 2 | $replacements = []; |
| 82 | 2 | if (!in_array($type, $entity_types) || empty($data[$type])) { |
| 83 | 1 | return $replacements; |
| 84 | } | |
| 85 | 1 | $entity = $data[$type]; |
| 86 | 1 | $entity_id = (int) ($entity->id() ?? 0); |
| 87 | 1 | $entity_view = $this->counter->fetchView($type, $entity_id); |
| 88 | 1 | foreach ($tokens as $name => $original) { |
| 89 | 1 | if ($name == 'total-count') { |
| 90 | 1 | $replacements[$original] = $entity_view ? $entity_view->getTotalCount() : 0; |
| 91 | } | |
| 92 | 1 | elseif ($name == 'day-count') { |
| 93 | 1 | $replacements[$original] = $entity_view ? $entity_view->getDayCount() : 0; |
| 94 | } | |
| 95 | 1 | elseif ($name == 'last-view') { |
| 96 | 1 | $replacements[$original] = $entity_view |
| 97 | 1 | ? $this->dateFormatter->format($entity_view->getTimestamp()) |
| 98 | 0 | : $this->t('never'); |
| 99 | } | |
| 100 | } | |
| 101 | 1 | if ($created_tokens = $token_service->findWithPrefix($tokens, 'last-view')) { |
| 102 | 1 | $replacements += $token_service->generate('date', $created_tokens, [ |
| 103 | 1 | 'date' => $entity_view ? $entity_view->getTimestamp() : 0, |
| 104 | 1 | ], $options, $bubbleable_metadata); |
| 105 | } | |
| 106 | 1 | return $replacements; |
| 107 | } | |
| 108 | ||
| 109 | } |