You are here

function token_replace_multiple in Token 6

Same name and namespace in other branches
  1. 5 token.module \token_replace_multiple()

Replace all tokens in a given string with appropriate values.

Contrary to token_replace() this function supports replacing multiple types.

Parameters

$text: A string potentially containing replaceable tokens.

$types: (optional) An array of substitution classes and optional objects. The key is a flag indicating the class of substitution tokens to use. If an object is passed as value, the key should contain the object's type. For example, 'node', 'comment', or 'user'. The object will be used for building substitution values. If no type is specified, only 'global' site-wide substitution tokens are built.

$leading: (optional) Character(s) to prepend to the token key before searching for matches. Defaults to TOKEN_PREFIX.

$trailing: (optional) Character(s) to append to the token key before searching for matches. Defaults to TOKEN_SUFFIX.

$options: (optional) A keyed array of settings and flags to control the token generation and replacement process. Supported options are:

  • clear: A boolean flag indicating that tokens should be removed from the final text if no replacement value can be generated.

$flush: (optional) A flag indicating whether or not to flush the token cache. Useful for processes that need to slog through huge numbers of tokens in a single execution cycle. Flushing it will keep them from burning through memory. Defaults to FALSE.

Return value

Text with tokens replaced.

5 calls to token_replace_multiple()
token_actions_goto_action in ./token_actions.module
Action callback to redirect the user to a tokenized URL.
token_actions_message_action in ./token_actions.module
Action callback to send a message to the current user's screen.
token_actions_send_email_action in ./token_actions.module
Action callback to send a tokenized e-mail.
token_replace in ./token.module
Replace all tokens in a given string with appropriate values.
token_test_exit in tests/token_test.module
Implements hook_exit().

File

./token.module, line 252
The Token API module.

Code

function token_replace_multiple($text, $types = array(
  'global' => NULL,
), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {

  // Ensure that the $text parameter is a string and not an array which is an
  // invalid input.
  if (is_array($text)) {
    foreach ($text as $key => $value) {
      $text[$key] = token_replace_multiple($value, $types, $leading, $trailing, $options, $flush);
    }
    return $text;
  }

  // If there are no tokens to replace, just return the text.
  $text_tokens = token_scan($text, $leading, $trailing);
  if (empty($text_tokens)) {
    return $text;
  }
  $full = new stdClass();
  $full->tokens = $full->values = array();

  // Allow global token replacement by default.
  if (empty($types) || !is_array($types)) {
    $types = array(
      'global' => NULL,
    );
  }
  foreach ($types as $type => $object) {
    $temp = token_get_values($type, $object, $flush, $options);
    $full->tokens = array_merge($full->tokens, $temp->tokens);
    $full->values = array_merge($full->values, $temp->values);
  }

  // Support clearing out tokens that would not be replaced.
  if (!empty($options['clear'])) {
    foreach ($text_tokens as $token) {
      if (!in_array($token, $full->tokens)) {
        $full->tokens[] = $token;
        $full->values[] = '';
      }
    }
  }
  $tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
  return str_replace($tokens, $full->values, $text);
}