-
-
Save sumanthkumarc/0fa36a3b78658c2a5892b0acd7ff2281 to your computer and use it in GitHub Desktop.
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