I have the tokens [user:mail] and [date:long] in a text field of a node. The tokens are not being replaced with any text but rather just appear as the token.

This is Drupal 7 and Tokens comes in core but I installed Tokens module and that did not resolve the issue. I also tried placing the tokens in a block but no joy there either.

Any ideas?

Comments

esnyder’s picture

After researching all week this is what I have learned. Tokens is an API only that gives a module writer or themer the ability to use tokens in their code. Tokens are not set up out of the box (at least in D7) to be used inside blocks or nodes automatically. This means that if you place a token in your field value (a body of an article for instance) it will not get replaced with a standard version of Drupal 7. You actually have to implement the token_replace() function to make Drupal replace tokens with their strings.

What I did was create a custom region.tpl.php file and changed one line. The original file looked like this:

<!-- <?php print $region_name; ?> region -->
<?php if ($region_style == 'full-width'): ?>
<div id="<?php print $region_name; ?>-wrapper" class="<?php print $region_name; ?>-wrapper full-width clearfix">
<?php endif; ?>
  <div id="<?php print $region_name; ?>" class="<?php print $classes; ?>"<?php print $fluid_width; ?>>
    <div id="<?php print $region_name; ?>-inner" class="<?php print $region_name; ?>-inner inner">
      <?php print $content; ?>
    </div><!-- /<?php print $region_name; ?>-inner -->
  </div><!-- /<?php print $region_name; ?> -->
<?php if ($region_style == 'full-width'): ?>
</div><!-- /<?php print $region_name; ?>-wrapper -->
<?php endif; ?>

I changed this line to include the token_replace:

<?php print token_replace($content); ?>

When the region areas get rendered the token_replace function takes the content and replaces all the tokens with their values.

In it's simplest form token_replace() takes a variable, replaces all the tokens in the variable and returns the new value. It can do more, look at the documentation on it here.

I have also found that you can add your own custom tokens. There are a number of standard groups of tokens that comes with the token module (now included with Drupal core as of D7). To see these install Token and then click on the help link. You will see a list of currently available tokens. You can also add your own group using some code that looks like this. Warning! This code is not production ready and may not adhere to the Drupal coding standards!

To 'Register' your custom token group in the list of tokens:

function my_module_token_info(){
  
  // 'Register' the custom token group 'Testing'
  $data['types']['testing'] = array(
    'name' => t('Testing'),
    'description' => t('Custom Testing tokens.'),
  );
  
  // Register the token '[testing:name]'
  $data['tokens']['testing']['name'] = array(
    'name' => t('Name'),
    'description' => t('The person\'s name.'),
  );
  return $data;
}

You may read about the hook token_info here.

After you have 'registered' the token group and token it still does not have a value. You need to give it a value using the next function:

function my_module_tokens($type, $object = NULL, $options = array()) {
    $tokens['[testing:name]']      = 'Fred the Red';
    return $tokens;
}

You may read about the hook_tokens function here.

Now if you use these two functions in a module along with the token_replace function above in the region.tpl.php in a theme then the token [testing:name] will get replaced with "Fred the Red" anytime that token appears in a region's content.

ashutoshm2’s picture

In registration mail for admin approval user tokens are not replacing.Is this a drupal core problem?

I am simply registering as the user to my site and getting the mail with the tokens that are not replaced with the values.

Jim Ruby’s picture

I am experiencing the same thing, maybe I'll have to try that code at the top of this thred.