3 namespace Drupal\token\Controller;
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;
13 * Devel integration for tokens.
15 class TokenDevelController extends ControllerBase {
18 * @var \Drupal\token\TreeBuilderInterface
20 protected $treeBuilder;
23 * @var \Drupal\token\TokenEntityMapperInterface
25 protected $entityMapper;
27 public function __construct(TreeBuilderInterface $tree_builder, TokenEntityMapperInterface $entity_mapper) {
28 $this->treeBuilder = $tree_builder;
29 $this->entityMapper = $entity_mapper;
35 public static function create(ContainerInterface $container) {
37 $container->get('token.tree_builder'),
38 $container->get('token.entity_mapper')
43 * Prints the loaded structure of the current entity.
45 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
46 * A RouteMatch object.
49 * Array of page elements to render.
51 public function entityTokens(RouteMatchInterface $route_match) {
54 $parameter_name = $route_match->getRouteObject()->getOption('_token_entity_type_id');
55 $entity = $route_match->getParameter($parameter_name);
57 if ($entity && $entity instanceof EntityInterface) {
58 $output = $this->renderTokenTree($entity);
65 * Render the token tree for the specified entity.
67 * @param \Drupal\Core\Entity\EntityInterface $entity
68 * The entity for which the token tree should be rendered.
71 * Render array of the token tree for the $entity.
73 * @see static::entityLoad
75 protected function renderTokenTree(EntityInterface $entity) {
76 $this->moduleHandler()->loadInclude('token', 'pages.inc');
77 $entity_type = $entity->getEntityTypeId();
79 $token_type = $this->entityMapper->getTokenTypeForEntityType($entity_type);
83 'data' => [$token_type => $entity],
88 'tokens' => $this->treeBuilder->buildTree($token_type, $options),
91 // foreach ($tree as $token => $token_info) {
92 // if (!isset($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
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.'),