function 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

hook_tokens()

Related topics

1 invocation of hook_tokens_alter()
Token::generate in drupal/core/lib/Drupal/Core/Utility/Token.php
Generates replacement values for a list of tokens.

File

drupal/core/modules/system/system.api.php, line 3264
Hooks provided by Drupal core and the System module.

Code

function hook_tokens_alter(array &$replacements, array $context) {
  $options = $context['options'];
  if (isset($options['langcode'])) {
    $url_options['language'] = language_load($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = 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, 'field_title', 'default', $langcode);
      $replacements[$context['tokens']['title']] = drupal_render($title);
    }
  }
}