You are here

function token_get_values in Token 5

Same name and namespace in other branches
  1. 6 token.module \token_get_values()

Return a list of valid substitution tokens and their values for the specified type.

Parameters

type: A flag indicating the class of substitution tokens to use. If an object is passed in the second param, 'type' should contain the object's type. For example, 'node', 'comment', or 'user'. If no type is specified, only 'global' site-wide substitution tokens are built.

object: Optionally, the object to use for building substitution values. A node, comment, user, etc.

flush: 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. The default is FALSE.

Return value

A keyed array containing the substitution tokens and the substitution values for the passed-in type and object.

2 calls to token_get_values()
token_replace in ./token.module
Return the value of $original, with all instances of placeholder tokens replaced by their proper values. To replace multiple types at once see token_replace_multiple().
token_replace_multiple in ./token.module
Return the value of $original, with all instances of placeholder tokens replaced by their proper values. Contrary to token_replace(), this function supports replacing multiple types.

File

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

Code

function token_get_values($type = 'global', $object = NULL, $flush = FALSE, $options = array()) {
  static $tokens;
  static $running;

  // Flush the static 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.
  if ($flush || !isset($tokens)) {
    $tokens = array();
  }

  // Since objects in PHP5 are always passed by reference, we ensure we're
  // working on a copy of the object.
  if (is_object($object)) {
    $object = drupal_clone($object);
  }

  // Simple recursion check. This is to avoid content_view()'s potential
  // for endless looping when a filter uses tokens, which load the content
  // view, which calls the filter, which uses tokens, which...
  if ($running) {

    // We'll allow things to get two levels deep, but bail out after that
    // without performing any substitutions.
    $result = new stdClass();
    $result->tokens = array();
    $result->values = array();
    return $result;
  }
  $running = TRUE;
  token_include();
  $id = _token_get_id($type, $object);
  if ($id && isset($tokens[$type][$id])) {
    $tmp_tokens = $tokens[$type][$id];
  }
  else {
    $tmp_tokens = module_invoke_all('token_values', $type, $object, $options);
    $tokens[$type][$id] = $tmp_tokens;
  }

  // Special-case global tokens, as we always want to be able to process
  // those substitutions.
  if (!isset($tokens['global']['default'])) {
    $tokens['global']['default'] = module_invoke_all('token_values', 'global');
  }
  $all = array_merge($tokens['global']['default'], $tokens[$type][$id]);
  $result = new stdClass();
  $result->tokens = array_keys($all);
  $result->values = array_values($all);
  $running = FALSE;
  return $result;
}