Skip to content

Instantly share code, notes, and snippets.

@reinis-danenbergsons
Last active January 23, 2017 16:58
Show Gist options
  • Select an option

  • Save reinis-danenbergsons/93e71588ce6e7ae72f0c5f808ae21fb9 to your computer and use it in GitHub Desktop.

Select an option

Save reinis-danenbergsons/93e71588ce6e7ae72f0c5f808ae21fb9 to your computer and use it in GitHub Desktop.
Drupal 8 - Example to define global hard coded tokens
<?php
use Drupal\Core\Render\BubbleableMetadata;
/**
* @implements hook_token_info().
*/
function my_module_token_info() {
$type = [
'name' => t('Custom tokens'),
'description' => t('My custom tokens.'),
];
$customTokens['my_string'] = [
'name' => t('My string'),
'description' => t('My string to be used.'),
];
return [
'types' => [
'customTokens' => $type
],
'tokens' => [
'customTokens' => $customTokens
],
];
}
/**
* @implements hook_tokens().
*
* @param string $type
* @param array $tokens
* @param array $data
* @param array $options
* @param BubbleableMetadata $bubbleable_metadata
*
* @return array $replacements
*/
function my_module_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'customTokens') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'my_string':
$replacements[$original] = 'my custom string';
break;
}
}
}
return $replacements;
}
@reinis-danenbergsons

Copy link
Copy Markdown
Author

Hard coded token defined on line 47.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment