2009-08-19 20:19:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Builds placeholder replacement tokens for node visitor statistics.
|
|
|
|
*/
|
|
|
|
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
use Drupal\Core\Render\BubbleableMetadata;
|
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_token_info().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
|
|
|
function statistics_token_info() {
|
2017-03-04 01:20:24 +00:00
|
|
|
$node['total-count'] = [
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("Number of views"),
|
|
|
|
'description' => t("The number of visitors who have read the node."),
|
2017-03-04 01:20:24 +00:00
|
|
|
];
|
|
|
|
$node['day-count'] = [
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("Views today"),
|
|
|
|
'description' => t("The number of visitors who have read the node today."),
|
2017-03-04 01:20:24 +00:00
|
|
|
];
|
|
|
|
$node['last-view'] = [
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("Last view"),
|
|
|
|
'description' => t("The date on which a visitor last read the node."),
|
|
|
|
'type' => 'date',
|
2017-03-04 01:20:24 +00:00
|
|
|
];
|
2009-08-19 20:19:37 +00:00
|
|
|
|
2017-03-04 01:20:24 +00:00
|
|
|
return [
|
|
|
|
'tokens' => ['node' => $node],
|
|
|
|
];
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_tokens().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
function statistics_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
|
2013-09-16 03:58:06 +00:00
|
|
|
$token_service = \Drupal::token();
|
2013-04-18 07:24:35 +00:00
|
|
|
|
2017-03-04 01:20:24 +00:00
|
|
|
$replacements = [];
|
2009-08-19 20:19:37 +00:00
|
|
|
|
|
|
|
if ($type == 'node' & !empty($data['node'])) {
|
|
|
|
$node = $data['node'];
|
2021-05-12 20:34:21 +00:00
|
|
|
|
2019-07-23 19:46:19 +00:00
|
|
|
/** @var \Drupal\statistics\StatisticsStorageInterface $stats_storage */
|
|
|
|
$stats_storage = \Drupal::service('statistics.storage.node');
|
2021-05-12 20:34:21 +00:00
|
|
|
$node_view = NULL;
|
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
foreach ($tokens as $name => $original) {
|
2010-04-20 09:48:06 +00:00
|
|
|
if ($name == 'total-count') {
|
2021-05-12 20:34:21 +00:00
|
|
|
$node_view = $node_view ?? $stats_storage->fetchView($node->id());
|
|
|
|
$replacements[$original] = $node_view ? $node_view->getTotalCount() : 0;
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
2010-04-20 09:48:06 +00:00
|
|
|
elseif ($name == 'day-count') {
|
2021-05-12 20:34:21 +00:00
|
|
|
$node_view = $node_view ?? $stats_storage->fetchView($node->id());
|
|
|
|
$replacements[$original] = $node_view ? $node_view->getDayCount() : 0;
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
elseif ($name == 'last-view') {
|
2021-05-12 20:34:21 +00:00
|
|
|
$node_view = $node_view ?? $stats_storage->fetchView($node->id());
|
|
|
|
$replacements[$original] = $node_view ? \Drupal::service('date.formatter')->format($node_view->getTimestamp()) : t('never');
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($created_tokens = $token_service->findWithPrefix($tokens, 'last-view')) {
|
2021-05-12 20:34:21 +00:00
|
|
|
$node_view = $node_view ?? $stats_storage->fetchView($node->id());
|
|
|
|
$replacements += $token_service->generate('date', $created_tokens, ['date' => $node_view ? $node_view->getTimestamp() : 0], $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|