You are here

protected function TreeBuilder::getTokenData in Token 8

Generate a token tree.

@internal

Parameters

string $token_type: The token type.

array $options: An associative array of additional options. See documentation for TreeBuilderInterface::buildTree() for more information.

Return value

array The token data for the specified $token_type.

1 call to TreeBuilder::getTokenData()
TreeBuilder::buildTree in src/TreeBuilder.php
Build a tree array of tokens used for themeing or information.

File

src/TreeBuilder.php, line 212

Class

TreeBuilder

Namespace

Drupal\token

Code

protected function getTokenData($token_type, array $options) {
  $options += [
    'parents' => [],
  ];
  $info = $this->tokenService
    ->getInfo();
  if ($options['depth'] <= 0 || !isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
    return [];
  }
  $tree = [];
  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 ($token !== 'entity' && 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'] = $this
        ->getTokenData($token_info['type'], $child_options);
    }
  }
  return $tree;
}