By rajatgusain on
Creation of custom tokens / placeholder for Drupal 7. I searched many forums to get any help regarding the creation of custom tokens but I failed in getting a proper solution that's why I decided to post this topic to help drupal users. For creating custom token we will use three token api function.
hook_token_info
hook_tokens($type, $tokens, array $data = array(), array $options = array())
token_replace($text, array $data = array(), array $options = array())
/**
* Implements hook_token_info(). This hook will register tow token lname and fname.
*/
function myhook_token_info() {
$info['tokens']['custom']['fname'] = array(
'name' => t('First name'),
'description' => t('First name re placer for fname '),
);
$info['tokens']['custom']['lname'] = array(
'name' => t('Last name'),
'description' => t('Last name re placer for lname '),
);
return $info;
}
/**
* Implements hook_tokens(). This hook will operate the token and replace it with it's value.
*/
function myhook_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
if ($type == 'custom') {
foreach ($tokens as $name => $original) {
if (array_key_exists($name, $data)) {
$replacements[$original] = $data[$name];
}
}
}
return $replacements;
}
/**
* Here we will use the token_replace() function to get the actual content after replacement.
*/
function myhook_myfunction() {
$custom = array();
$custom['fname'] = 'Rajat';
$custom['lname'] = 'Gusain';
$data = $custom;
$temp = "Full name : [custom:fname] [custom:lname]";
$temp = token_replace($temp, $data);
// After token replacement $temp variable will contain " Full name : Rajat Gusain"
}
Any comment, compliment, problem is welcome.
Comments
What I came up with
What I came up with, fully documented.
PS: Had to replace http:// with httx:// because otherwise the text formatting would fuck the code up.
Thanks!
I've been looking for a reference like this for hours! Thanks!
This is great.Thanks so much
This is great.
Thanks so much for posting that! Saved me a tonne of time, and the inline documentation is fantastic.
Ændrew Rininsland
News Developer and polynerd
http://www.aendrew.com
Outdated example
This example doesn't seem to work with Drupal 7. I ended up using the Examples for Developers module and the token_example sub module to get a good template to start from.
Just in case anyone wanted a
Just in case anyone wanted a version without tabs and instead used spaces. Here:
Very helpful, thanks.
Very helpful, thanks.
Thanks
Thanks for the info, it's very useful.
I also recommend this link that has a more practical approach:
http://blog.artemshymko.com/how-create-custom-token-drupal-7
Senior Drupal Developer — Backend, Frontend & DevOps/CI/CD
I'm getting a WSOD for that
I'm getting a WSOD for that link
a white page is very
a white page is a very practical approach ;-)
Really a white page :(
Really a white page :(
Resurrected
Here's an archived version of that link:
http://web.archive.org/web/20121120085959/http://blog.artemshymko.com/ho...
Using Existing Tokens
Is there a way to create a custom token using tokens that already exist? I'd like to be able to make these pipes only appear if there is data in the next token.
[node:title] | [example:field] | [token:another]Maybe I'm missing something, but I seriously can not figure it out.
I'm guessing that there
I'm guessing that there probably isn't a way to do this. Tokens are simply done with a search and replace, there is no logic used in their processing.
Contact me to contract me for D7 -> D10/11 migrations.
Thank you
Very helpful, thanks.
Thanks
Thanks for taking the trouble to write that. Made my life a lot easier! :-)
Jon
Jon