Skip to content

Instantly share code, notes, and snippets.

@sumanthkumarc
Forked from reinis-kinkeris/my_module.token.inc
Created November 17, 2016 10:59
Show Gist options
  • Save sumanthkumarc/0fa36a3b78658c2a5892b0acd7ff2281 to your computer and use it in GitHub Desktop.
Save sumanthkumarc/0fa36a3b78658c2a5892b0acd7ff2281 to your computer and use it in GitHub Desktop.
<?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