/**
 * Helper function for preg_replace_callback to generate the html for each token filter
 */
function token_filter_replacetoken($matches) {  
  global $user;
  $type  = $matches[1];
  $token = $matches[2];
  switch ($type) {
    case 'user' :
      $object = $user;
      break;
    case 'global':
    default:
      $object = NULL;
      break;
  } 
  
  // add [ ] to the token so str_replace correctly replaces token values
  // if not, a token like 'custom_text' won't be properly replaced if another token like 'custom' exists  
  $token = '['.$token.']';
    
  $output = token_replace($token, $type, $object, '[', ']');  
  return $output;
}

The function passes any value for $type, even if it's not global, or user.

The correct code should be

/**
 * Helper function for preg_replace_callback to generate the html for each token filter
 */
function token_filter_replacetoken($matches) {  
  global $user;
  
  $type  = $matches[1];
  $token = $matches[2];
  
  if ($type != 'user' && $type != 'global') {
    return '';
  }
  
  switch ($type) {
    case 'user':
      $object = $user;
      break;
    
    case 'global':
    default:
      $object = NULL;
      break;
  } 
  
  // add [ ] to the token so str_replace correctly replaces token values
  // if not, a token like 'custom_text' won't be properly replaced if another token like 'custom' exists  
  $token = '['.$token.']';
    
  $output = token_replace($token, $type, $object, '[', ']');  
  return $output;
}

Also, $token = '['.$token.']' forces the user to use [token user [uid]], when it would be preferable to use [token user [uid]].

Comments

apaderno’s picture

The last sentence should be read as: also, $token = '['.$token.']' forces the user to use [token user [uid]], when it would be preferable to use [token user uid].

apaderno’s picture

    case 'global':
    default:
      $object = NULL;
      break;

can ben simplified in

    case 'global':
      $object = NULL;
      break;

because the function would not use values for $type that are not global, or user.

pvhee’s picture

Hm, I don't agree with the corrections:

    case 'global':
    default:
      $object = NULL;
      break;

makes sure you can use other contexts (eg custom contexts provided by a custom module) in the token filter, next to the global and user context (and that you don't want mixed in the global context).

In the latest revision, I've added the '[' and ']' around the text to replace only in the internal replace mechanism, such that eg 'custom' and 'custom_text' are not conflicting. You can still use [token user uid] in the token filter, [token user [uid]] won't work!

apaderno’s picture

Custom contexts provided by third-party module probably require an object that the module is not able to pass to the function. That is the reason I suggested to only handle user, and global replacement.

justingeeslin’s picture

I'm creating a module which creates its own tokens in a context other than 'user' and 'global'. The suggested changes would force me to create build in a input filter for my tokens into my module.

I don't agree with the corrections either.

darvanen’s picture

Status: Active » Closed (outdated)

Drupal 6 version is no longer supported. Please reopen this if you find it is relevant to Drupal 7 or 8.