Version 1
[yaffs-website] / web / modules / contrib / token / src / Controller / TokenTreeController.php
1 <?php
2
3 namespace Drupal\token\Controller;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\token\TreeBuilderInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Symfony\Component\HttpFoundation\Request;
10
11 /**
12  * Returns tree responses for tokens.
13  */
14 class TokenTreeController extends ControllerBase {
15
16   /**
17    * @var \Drupal\token\TreeBuilderInterface
18    */
19   protected $treeBuilder;
20
21   public function __construct(TreeBuilderInterface $tree_builder) {
22     $this->treeBuilder = $tree_builder;
23   }
24
25   /**
26    * {@inheritdoc}
27    */
28   public static function create(ContainerInterface $container) {
29     return new static(
30       $container->get('token.tree_builder')
31     );
32   }
33
34   /**
35    * Page callback to output a token tree as an empty page.
36    */
37   function outputTree(Request $request) {
38     $options = $request->query->has('options') ? Json::decode($request->query->get('options')) : [];
39
40     // The option token_types may only be an array OR 'all'. If it is not set,
41     // we assume that only global token types are requested.
42     $token_types = !empty($options['token_types']) ? $options['token_types'] : [];
43     if ($token_types == 'all') {
44       $build = $this->treeBuilder->buildAllRenderable($options);
45     }
46     else {
47       $build = $this->treeBuilder->buildRenderable($token_types, $options);
48     }
49
50     $build['#cache']['contexts'][] = 'url.query_args:options';
51     $build['#title'] = $this->t('Available tokens');
52
53     return $build;
54   }
55
56 }