You are here

function token_get_values in Token 6

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

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

Parameters

$type: (optional) 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: (optional) An object to use for building substitution values (e.g. a node comment, or user object).

$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.

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

Return value

An object with two properties:

  • tokens: All the possible tokens names generated.
  • values: The corresponding values for the tokens.

Note that before performing actual token replacement that the token names should be run through token_prepare_tokens().

5 calls to token_get_values()
TokenTestHelper::assertTokens in ./token.test
TokenTestHelper::setUp in ./token.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
token_devel_token_object in ./token.pages.inc
Menu callback; prints the available tokens and values for an object.
token_replace_multiple in ./token.module
Replace all tokens in a given string with appropriate values.
token_rules_input_evaluator_apply in ./token.rules.inc
Apply the input evaluator.

File

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

Code

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

  // 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;
  }
  else {
    $running = TRUE;
  }

  // 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 || !empty($options['reset'])) {
    $tokens = array();
  }

  // Allow simple resets of the static values.
  if ($type === 'reset') {
    $tokens = array();
    $running = FALSE;
    return;
  }

  // Neutralize options that do not affect token replacement.
  $serialized_options = $options;
  unset($serialized_options['clear']);

  // Store the token cache by object ID and serialized options.
  $cid = _token_get_id($type, $object) . ':' . md5(serialize($serialized_options));
  if ($type != 'global' && !isset($tokens[$type][$cid])) {
    token_include();
    $tokens[$type][$cid] = module_invoke_all('token_values', $type, $object, $options);
  }

  // Special-case global tokens, as we always want to be able to process
  // those substitutions.
  if (!isset($tokens['global'][$cid])) {
    token_include();
    $tokens['global'][$cid] = module_invoke_all('token_values', 'global', NULL, $options);
  }
  $all = $tokens['global'][$cid];
  if ($type != 'global') {

    // Avoid using array_merge() if only global tokens were requested.
    $all = array_merge($all, $tokens[$type][$cid]);
  }

  // Allow other modules to alter the replacements.
  $context = array(
    'type' => $type,
    'object' => $object,
    'options' => $options,
  );
  drupal_alter('token_values', $all, $context);
  $result = new stdClass();
  $result->tokens = array_keys($all);
  $result->values = array_values($all);
  $running = FALSE;
  return $result;
}