hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
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'
\Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata: The bubbleable metadata. In case you alter an existing token based upon a data source that isn't in $context['data'], you must add that dependency to $bubbleable_metadata.
See also
Related topics
- Hooks
- Define functions that alter the behavior of Drupal core.
File
- core/lib/Drupal/Core/Utility/token.api.php, line 151
- Hooks related to the Token system.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function hook_tokens_alter( array & $replacements , array $context , \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata ) { $options = $context [ 'options' ]; if (isset( $options [ 'langcode' ])) { $url_options [ 'language' ] = \Drupal::languageManager()->getLanguage( $options [ 'langcode' ]); $langcode = $options [ 'langcode' ]; } else { $langcode = NULL; } 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 = $node ->field_title->view( 'default' ); $replacements [ $context [ 'tokens' ][ 'title' ]] = drupal_render( $title ); } } } |
Please login to continue.