You are here

public function TreeBuilder::buildRenderable in Token 8

Build a render array with token tree built as per specified options.

Parameters

array $token_types: An array containing token types that should be shown in the tree.

array $options: (optional) An associative array to control which tokens are shown and how. The properties available are:

  • 'global_types' (defaults to TRUE): Show all global token types along with the specified types.
  • 'click_insert' (defaults to TRUE): Include classes and caption to show allow inserting tokens in fields by clicking on them.
  • 'show_restricted' (defaults to FALSE): Show restricted tokens in the tree.
  • 'show_nested' (defaults to FALSE): If this token is nested and should therefor not show on the token browser as a top level token.
  • 'recursion_limit' (defaults to 3): Only show tokens up to the specified depth.

Return value

array Render array for the token tree.

Overrides TreeBuilderInterface::buildRenderable

1 call to TreeBuilder::buildRenderable()
TreeBuilder::buildAllRenderable in src/TreeBuilder.php
Build a render array with token tree containing all possible tokens.

File

src/TreeBuilder.php, line 52

Class

TreeBuilder

Namespace

Drupal\token

Code

public function buildRenderable(array $token_types, array $options = []) {

  // Set default options.
  $options += [
    'global_types' => TRUE,
    'click_insert' => TRUE,
    'show_restricted' => FALSE,
    'show_nested' => FALSE,
    'recursion_limit' => 3,
  ];
  $info = $this->tokenService
    ->getInfo();
  if ($options['global_types']) {
    $token_types = array_merge($token_types, $this->tokenService
      ->getGlobalTokenTypes());
  }
  $element = [];

  // @todo Find a way to use the render cache for this.
  $tree_options = [
    'flat' => TRUE,
    'restricted' => $options['show_restricted'],
    'nested' => $options['show_nested'],
    'depth' => $options['recursion_limit'],
  ];
  $token_tree = [];
  foreach ($info['types'] as $type => $type_info) {
    if (!in_array($type, $token_types)) {
      continue;
    }
    $token_tree[$type] = $type_info;
    $token_tree[$type]['tokens'] = $this
      ->buildTree($type, $tree_options);
  }
  $element += [
    '#type' => 'token_tree_table',
    '#token_tree' => $token_tree,
    '#show_restricted' => $options['show_restricted'],
    '#show_nested' => $options['show_nested'],
    '#click_insert' => $options['click_insert'],
    '#columns' => [
      'name',
      'token',
      'description',
    ],
    '#empty' => $this
      ->t('No tokens available'),
  ];
  return $element;
}