Version 1
[yaffs-website] / web / modules / contrib / token / src / Controller / TokenDevelController.php
1 <?php
2
3 namespace Drupal\token\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\token\TokenEntityMapperInterface;
9 use Drupal\token\TreeBuilderInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Devel integration for tokens.
14  */
15 class TokenDevelController extends ControllerBase {
16
17   /**
18    * @var \Drupal\token\TreeBuilderInterface
19    */
20   protected $treeBuilder;
21
22   /**
23    * @var \Drupal\token\TokenEntityMapperInterface
24    */
25   protected $entityMapper;
26
27   public function __construct(TreeBuilderInterface $tree_builder, TokenEntityMapperInterface $entity_mapper) {
28     $this->treeBuilder = $tree_builder;
29     $this->entityMapper = $entity_mapper;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('token.tree_builder'),
38       $container->get('token.entity_mapper')
39     );
40   }
41
42   /**
43    * Prints the loaded structure of the current entity.
44    *
45    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
46    *    A RouteMatch object.
47    *
48    * @return array
49    *    Array of page elements to render.
50    */
51   public function entityTokens(RouteMatchInterface $route_match) {
52     $output = [];
53
54     $parameter_name = $route_match->getRouteObject()->getOption('_token_entity_type_id');
55     $entity = $route_match->getParameter($parameter_name);
56
57     if ($entity && $entity instanceof EntityInterface) {
58       $output = $this->renderTokenTree($entity);
59     }
60
61     return $output;
62   }
63
64   /**
65    * Render the token tree for the specified entity.
66    *
67    * @param \Drupal\Core\Entity\EntityInterface $entity
68    *   The entity for which the token tree should be rendered.
69    *
70    * @return array
71    *   Render array of the token tree for the $entity.
72    *
73    * @see static::entityLoad
74    */
75   protected function renderTokenTree(EntityInterface $entity) {
76     $this->moduleHandler()->loadInclude('token', 'pages.inc');
77     $entity_type = $entity->getEntityTypeId();
78
79     $token_type = $this->entityMapper->getTokenTypeForEntityType($entity_type);
80     $options = [
81       'flat' => TRUE,
82       'values' => TRUE,
83       'data' => [$token_type => $entity],
84     ];
85
86     $token_tree = [
87       $token_type => [
88         'tokens' => $this->treeBuilder->buildTree($token_type, $options),
89       ],
90     ];
91 //    foreach ($tree as $token => $token_info) {
92 //      if (!isset($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
93 //        continue;
94 //      }
95 //    }
96
97     $build['tokens'] = [
98       '#type' => 'token_tree_table',
99       '#show_restricted' => FALSE,
100       '#show_nested' => FALSE,
101       '#skip_empty_values' => TRUE,
102       '#token_tree' => $token_tree,
103       '#columns' => ['token', 'value'],
104       '#empty' => $this->t('No tokens available.'),
105     ];
106
107     return $build;
108   }
109 }