just photo

How to add a custom token

Julia 06/01/2010

It is a snippet - how to create custom tokens for specific replacements that can improve other modules relying on Token.

Custom implementation

Firstly, how can you create own tokens in a module yourself.
This is a small snippet about creating own token.

For example I use content_profile module I wish to have the title of profile node in my tokens.

Before that, don't forget to enable the Token module

<?php
/**
* Implementation of hook_token_values().
*/
function [mymodulename]_token_values($type, $object = NULL, $options = array()) {
 
$values = array();
  switch (
$type) {
    case
'node':
      if (isset(
$object)) {
       
$account = user_load(array('uid' => $object->uid));
      }
      else {
        global
$user;
       
$account = user_load(array('uid' => $user->uid));
      }
     
     
     
$node_profile = content_profile_load('profile', $account->uid);
     
$values['profile'] = ($node_profile->title) ? $node_profile->title : '';     
     
     
      break;
  }
  return
$values;
}

/**
* Implementation of hook_token_list().
*/
function [mymodulename]_token_list($type = 'all') {
  if (
$type == 'node' || $type == 'all') {
   
$tokens['node']['profile']       = t("Profile title of authors content profile");
   
    return
$tokens;
  }
}
?>

Module Custom Tokens

Also you can try to use the Custom Tokens module

From the desciption of this module:
"Advanced module, PHP code evaluation and execution
This module is designed for developers with, at least, a little PHP knowledge. Custom tokens will evaluate and execute PHP snippets in order to provide the replacements for the tokens you define."

This post is very good. i learn much after i read. thanks a lot.http://www.golfzonediscount.com

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options