I have created a custom token which reads values from the Apache Request Headers. It seems to work fine for my testing, but then when another user subsequently accesses the token, it uses my values. It seems that the token value is being cached. How do I disable caching on custom token values? NOTE: because of the use situation, I need to disable caching on the token value (or whole page), not just add a context.
I have searched for this and not found an answer. There are no cache related settings in hook_token_info nor hook_tokens. I have seen references to using bubbleable metadata in caching (https://www.drupal.org/node/2528662), but I am not clear on what I need to put there to disable caching.
/**
* Implements hook_token_info().
*/
function fred_token_info() {
$info = [
'types' => [
'fred_type' => [
'name' => t('fred type'),
],
],
'tokens' => [
'fred_type' => [
'APACHE_HEADERS' => [
'name' => 'apache_request_headers()',
'description' => t('Request headers as sent through Apache...not strictly a superglobal, but useful. Works in the Apache, FastCGI, CLI, and FPM webservers.'),
'dynamic' => TRUE,
],
],
],
];
return $info;
}
/**
* Implements hook_tokens().
*/
function fred_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'fred_type') {
foreach ($tokens as $name => $original) {
$tokenParts = explode(':',$name);
if (count($tokenParts) >= 2) {
switch ($tokenParts[0]) {
case 'APACHE_HEADERS':
$fredType =& $_SERVER;
break;
}
$FredKey = $tokenParts[1];
}
else {
return;
}
if (!isset($tokenParts[2]) || $tokenParts[2]!=='raw') {
$replacements[$original] = filter_var($superglobalType[$superglobalKey], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES|FILTER_FLAG_STRIP_LOW);
}
}
}
return $replacements;
}