TokenDevelController.php in Token 8
File
src/Controller/TokenDevelController.php
View source
<?php
namespace Drupal\token\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\token\TokenEntityMapperInterface;
use Drupal\token\TreeBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TokenDevelController extends ControllerBase {
protected $treeBuilder;
protected $entityMapper;
public function __construct(TreeBuilderInterface $tree_builder, TokenEntityMapperInterface $entity_mapper) {
$this->treeBuilder = $tree_builder;
$this->entityMapper = $entity_mapper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('token.tree_builder'), $container
->get('token.entity_mapper'));
}
public function entityTokens(RouteMatchInterface $route_match) {
$output = [];
$parameter_name = $route_match
->getRouteObject()
->getOption('_token_entity_type_id');
$entity = $route_match
->getParameter($parameter_name);
if ($entity && $entity instanceof EntityInterface) {
$output = $this
->renderTokenTree($entity);
}
return $output;
}
protected function renderTokenTree(EntityInterface $entity) {
$this
->moduleHandler()
->loadInclude('token', 'pages.inc');
$entity_type = $entity
->getEntityTypeId();
$token_type = $this->entityMapper
->getTokenTypeForEntityType($entity_type);
$options = [
'flat' => TRUE,
'values' => TRUE,
'data' => [
$token_type => $entity,
],
];
$token_tree = [
$token_type => [
'tokens' => $this->treeBuilder
->buildTree($token_type, $options),
],
];
$build['tokens'] = [
'#type' => 'token_tree_table',
'#show_restricted' => FALSE,
'#show_nested' => FALSE,
'#skip_empty_values' => TRUE,
'#token_tree' => $token_tree,
'#columns' => [
'token',
'value',
],
'#empty' => $this
->t('No tokens available.'),
];
return $build;
}
}