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() {
|
2010-04-20 09:48:06 +00:00
|
|
|
$node['total-count'] = array(
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("Number of views"),
|
|
|
|
'description' => t("The number of visitors who have read the node."),
|
|
|
|
);
|
2010-04-20 09:48:06 +00:00
|
|
|
$node['day-count'] = array(
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("Views today"),
|
|
|
|
'description' => t("The number of visitors who have read the node today."),
|
|
|
|
);
|
|
|
|
$node['last-view'] = array(
|
|
|
|
'name' => t("Last view"),
|
|
|
|
'description' => t("The date on which a visitor last read the node."),
|
|
|
|
'type' => 'date',
|
|
|
|
);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'tokens' => array('node' => $node),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
2009-10-18 07:56:20 +00:00
|
|
|
$replacements = array();
|
2009-08-19 20:19:37 +00:00
|
|
|
|
|
|
|
if ($type == 'node' & !empty($data['node'])) {
|
|
|
|
$node = $data['node'];
|
|
|
|
|
|
|
|
foreach ($tokens as $name => $original) {
|
2010-04-20 09:48:06 +00:00
|
|
|
if ($name == 'total-count') {
|
2013-07-20 12:21:43 +00:00
|
|
|
$statistics = statistics_get($node->id());
|
2010-04-20 09:48:06 +00:00
|
|
|
$replacements[$original] = $statistics['totalcount'];
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
2010-04-20 09:48:06 +00:00
|
|
|
elseif ($name == 'day-count') {
|
2013-07-20 12:21:43 +00:00
|
|
|
$statistics = statistics_get($node->id());
|
2010-04-20 09:48:06 +00:00
|
|
|
$replacements[$original] = $statistics['daycount'];
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
elseif ($name == 'last-view') {
|
2013-07-20 12:21:43 +00:00
|
|
|
$statistics = statistics_get($node->id());
|
2009-08-19 20:19:37 +00:00
|
|
|
$replacements[$original] = format_date($statistics['timestamp']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($created_tokens = $token_service->findWithPrefix($tokens, 'last-view')) {
|
2013-07-20 12:21:43 +00:00
|
|
|
$statistics = statistics_get($node->id());
|
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
|
|
|
$replacements += $token_service->generate('date', $created_tokens, array('date' => $statistics['timestamp']), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|