You are here

function token_build_tree in Token 7

Build a tree array of tokens used for themeing or information.

Parameters

$token_type: The token type.

$flat_tree: A boolean if TRUE will only make a flat array of tokens, otherwise child tokens will be inside the 'children' parameter of a token.

$show_restricted: A boolean if TRUE will show restricted tokens. Otherwise they will be hidden. Default is FALSE.

$recursion_limit: An integer with the maximum number of token levels to recurse.

$parents: An optional array with the current parents of the tokens.

3 calls to token_build_tree()
theme_token_tree in ./token.pages.inc
Provide a 'tree' display of nested tokens.
token_autocomplete_token in ./token.pages.inc
token_devel_token_object in ./token.pages.inc
Menu callback; prints the available tokens and values for an object.
1 string reference to 'token_build_tree'
token_clear_cache in ./token.module
Clear token caches and static variables.

File

./token.module, line 887
Enhances the token API in core: adds a browseable UI, missing tokens, etc.

Code

function token_build_tree($token_type, array $options = array()) {
  global $language;

  // Static cache of already built token trees.
  $trees =& drupal_static(__FUNCTION__, array());
  $options += array(
    'restricted' => FALSE,
    'depth' => 4,
    'data' => array(),
    'values' => FALSE,
    'flat' => FALSE,
  );

  // Do not allow past the maximum token information depth.
  $options['depth'] = min($options['depth'], TOKEN_MAX_DEPTH);

  // If $token_type is an entity, make sure we are using the actual token type.
  if ($entity_token_type = token_get_entity_mapping('entity', $token_type)) {
    $token_type = $entity_token_type;
  }
  $tree_cid = "tree:{$token_type}:{$language->language}:{$options['depth']}";

  // If we do not have this base tree in the static cache, check {cache_token}
  // otherwise generate and store it in the cache.
  if (!isset($trees[$tree_cid])) {
    if ($cache = cache_get($tree_cid, 'cache_token')) {
      $trees[$tree_cid] = $cache->data;
    }
    else {
      $options['parents'] = array();
      $trees[$tree_cid] = _token_build_tree($token_type, $options);
      cache_set($tree_cid, $trees[$tree_cid], 'cache_token');
    }
  }
  $tree = $trees[$tree_cid];

  // If the user has requested a flat tree, convert it.
  if (!empty($options['flat'])) {
    $tree = token_flatten_tree($tree);
  }

  // Fill in token values.
  if (!empty($options['values'])) {
    $token_values = array();
    foreach ($tree as $token => $token_info) {
      if (!empty($token_info['dynamic']) || !empty($token_info['restricted'])) {
        continue;
      }
      elseif (!isset($token_info['value'])) {
        $token_values[$token_info['token']] = $token;
      }
    }
    if (!empty($token_values)) {
      $token_values = token_generate($token_type, $token_values, $options['data']);
      foreach ($token_values as $token => $replacement) {
        $tree[$token]['value'] = $replacement;
      }
    }
  }
  return $tree;
}