You are here

function token_rules_input_evaluator_apply in Token 6

Apply the input evaluator.

Parameters

$text: The string for which tokens should be replaced.

$used_vars: The used variables as returned from preparation.

$state: The current evaluation state of rules.

File

./token.rules.inc, line 56
Rules integration for the token module.

Code

function token_rules_input_evaluator_apply($text, $used_vars, &$state) {
  static $token_cache = array();
  if ($used_vars) {
    $vars = rules_get_variables(drupal_map_assoc(array_diff($used_vars, array(
      ':global',
    ))), $state);
    if ($vars === FALSE) {

      //there not all needed variables available!
      return FALSE;
    }
    $vars[':global'] = ':global';
    foreach ($used_vars as $name) {
      $type = $name == ':global' ? 'global' : _token_rules_map_type($state['variables'][$name]->info['type']);
      if ($type) {
        $token_id = _token_get_id($type, $vars[$name]);
        if (isset($token_cache[$token_id]) && $token_cache[$token_id] != $name) {

          // this is the same variable in another state
          // so we need to flush the token cache to get the fresh values
          token_get_values('reset');
        }
        $text = token_replace($text, $type, $vars[$name], TOKEN_PREFIX . $name . ':', TOKEN_SUFFIX);

        // remember that this variable has been used and got cached
        $token_cache[$token_id] = $name;
      }
    }
  }
  return $text;
}