-
Create a Custom Module: If you don't already have a custom module, create one. You can create a custom module in Drupal by following these steps:
a. Create a new folder for your module inside the
modules/custom
directory in your Drupal installation. Give it a suitable name, such as "custom_tokens."b. Inside your module folder, create a
custom_tokens.info.yml
file and acustom_tokens.module
file. -
Define Your Token: In the
custom_tokens.module
file, you'll define your custom token using Drupal'shook_token_info()
andhook_tokens()
hooks.
/**
* Implements hook_token_info().
*/
function custom_tokens_token_info() {
$info = array();
$info['tokens']['custom']['my_custom_token'] = array(
'name' => t('My Custom Token'),
'description' => t('This is a custom token for demo.'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function custom_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'custom') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'my_custom_token':
$replacements[$original] = 'Custom Token Content';
break;
}
}
}
return $replacements;
}
Add new comment