function hook_tokens_alter
Same name in other branches
- 9 core/lib/Drupal/Core/Utility/token.api.php \hook_tokens_alter()
- 8.9.x core/lib/Drupal/Core/Utility/token.api.php \hook_tokens_alter()
- 10 core/lib/Drupal/Core/Utility/token.api.php \hook_tokens_alter()
- 11.x core/lib/Drupal/Core/Utility/token.api.php \hook_tokens_alter()
Alter replacement values for placeholder tokens.
Parameters
$replacements: An associative array of replacements returned by hook_tokens().
$context: The context in which hook_tokens() was called. An associative array with the following keys, which have the same meaning as the corresponding parameters of hook_tokens():
- 'type'
- 'tokens'
- 'data'
- 'options'
See also
Related topics
1 invocation of hook_tokens_alter()
- token_generate in includes/
token.inc - Generates replacement values for a list of tokens.
File
-
modules/
system/ system.api.php, line 4497
Code
function hook_tokens_alter(array &$replacements, array $context) {
$options = $context['options'];
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
if ($context['type'] == 'node' && !empty($context['data']['node'])) {
$node = $context['data']['node'];
// Alter the [node:title] token, and replace it with the rendered content
// of a field (field_title).
if (isset($context['tokens']['title'])) {
$title = field_view_field('node', $node, 'field_title', 'default', $language_code);
$replacements[$context['tokens']['title']] = drupal_render($title);
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.