8.5.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.0.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.1.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.2.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.3.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.4.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
8.6.x token.api.php hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
7.x system.api.php hook_tokens_alter(array &$replacements, array $context)

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 includes/token.inc
Generates replacement values for a list of tokens.

File

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

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);
    }
  }
}

Comments

rafinskipg’s picture

You can use hook_tokens_alter in a "hacky" way, instead of using Entity Tokens, in order to get a taxonomy term - i18n-Name token.

/**
* Changing the language of a Term token.
* Implements hook_tokens_alter().
*/
function mymodule_tokens_alter(&$replacements,  $context){
	if(isset($context['data']['entity'])){
		if($context['data']['entity']->type == 'page'){
			if(isset($replacements['[node:field_term]'])){
				$term = $context['data']['entity']->field_term['und']['0']['tid'];
				
				global $language;
				$term = taxonomy_term_load($term );
				$term = i18n_taxonomy_term_name($term,$language->language);
				$replacements['[node:field_term]']  = check_plain($term);
			}
		}
	}
}

www.monchacos.com/monchacos