You are here

function _token_build_tree in Token 7

Generate a token tree.

1 call to _token_build_tree()
token_build_tree in ./token.module
Build a tree array of tokens used for themeing or information.

File

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

Code

function _token_build_tree($token_type, array $options) {
  $options += array(
    'parents' => array(),
  );
  $info = token_get_info();
  if ($options['depth'] <= 0 || !isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
    return array();
  }
  $tree = array();
  foreach ($info['tokens'][$token_type] as $token => $token_info) {

    // Build the raw token string.
    $token_parents = $options['parents'];
    if (empty($token_parents)) {

      // If the parents array is currently empty, assume the token type is its
      // parent.
      $token_parents[] = $token_type;
    }
    elseif (in_array($token, array_slice($token_parents, 1), TRUE)) {

      // Prevent duplicate recursive tokens. For example, this will prevent
      // the tree from generating the following tokens or deeper:
      // [comment:parent:parent]
      // [comment:parent:root:parent]
      continue;
    }
    $token_parents[] = $token;
    if (!empty($token_info['dynamic'])) {
      $token_parents[] = '?';
    }
    $raw_token = '[' . implode(':', $token_parents) . ']';
    $tree[$raw_token] = $token_info;
    $tree[$raw_token]['raw token'] = $raw_token;

    // Add the token's real name (leave out the base token type).
    $tree[$raw_token]['token'] = implode(':', array_slice($token_parents, 1));

    // Add the token's parent as its raw token value.
    if (!empty($options['parents'])) {
      $tree[$raw_token]['parent'] = '[' . implode(':', $options['parents']) . ']';
    }

    // Fetch the child tokens.
    if (!empty($token_info['type'])) {
      $child_options = $options;
      $child_options['depth']--;
      $child_options['parents'] = $token_parents;
      $tree[$raw_token]['children'] = _token_build_tree($token_info['type'], $child_options);
    }
  }
  return $tree;
}