Last active
January 23, 2017 16:58
-
-
Save reinis-kinkeris/93e71588ce6e7ae72f0c5f808ae21fb9 to your computer and use it in GitHub Desktop.
Drupal 8 - Example to define global hard coded tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hard coded token defined on line 47.