You are here

function TokenTreeController::outputTree in Token 8

Page callback to output a token tree as an empty page.

1 string reference to 'TokenTreeController::outputTree'
token.routing.yml in ./token.routing.yml
token.routing.yml

File

src/Controller/TokenTreeController.php, line 37

Class

TokenTreeController
Returns tree responses for tokens.

Namespace

Drupal\token\Controller

Code

function outputTree(Request $request) {
  $options = $request->query
    ->has('options') ? Json::decode($request->query
    ->get('options')) : [];

  // The option token_types may only be an array OR 'all'. If it is not set,
  // we assume that only global token types are requested.
  $token_types = !empty($options['token_types']) ? $options['token_types'] : [];
  if ($token_types == 'all') {
    $build = $this->treeBuilder
      ->buildAllRenderable($options);
  }
  else {
    $build = $this->treeBuilder
      ->buildRenderable($token_types, $options);
  }
  $build['#cache']['contexts'][] = 'url.query_args:options';
  $build['#title'] = $this
    ->t('Available tokens');

  // If this is an AJAX/modal request, add a wrapping div to the contents so
  // that Drupal.behaviors.tokenTree and Drupal.behaviors.tokenAttach can
  // stil find the elements they need to.
  // @see https://www.drupal.org/project/token/issues/2994671
  // @see https://www.drupal.org/node/2940704
  // @see http://danielnouri.org/notes/2011/03/14/a-jquery-find-that-also-finds-the-root-element/
  if ($request
    ->isXmlHttpRequest()) {
    $build['#prefix'] = '<div>';
    $build['#suffix'] = '</div>';
  }
  return $build;
}