3 namespace Drupal\token_test\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Render\BubbleableMetadata;
7 use Drupal\Core\Utility\Token;
8 use Drupal\node\NodeInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Provides a test controller for token replacement.
14 class TestController extends ControllerBase {
17 * The token replacement system.
19 * @var \Drupal\Core\Utility\Token
24 * Constructs a new TestController instance.
26 * @param \Drupal\Core\Utility\Token $token
27 * The token replacement system.
29 public function __construct(Token $token) {
30 $this->token = $token;
36 public static function create(ContainerInterface $container) {
37 return new static($container->get('token'));
41 * Provides a token replacement with a node as well as the current user.
43 * This controller passes an explicit bubbleable metadata object to
44 * $this->token->replace(), and applies the collected metadata to the render
47 * @param \Drupal\node\NodeInterface $node
53 public function tokenReplace(NodeInterface $node) {
54 $bubbleable_metadata = new BubbleableMetadata();
55 $build['#markup'] = $this->token->replace('Tokens: [node:nid] [current-user:uid]', ['node' => $node], [], $bubbleable_metadata);
56 $bubbleable_metadata->applyTo($build);
62 * Provides a token replacement with a node as well as the current user.
64 * This controller is for testing the token service's fallback behavior of
65 * applying collected metadata to the currently active render context when an
66 * explicit bubbleable metadata object isn't passed in.
68 * @param \Drupal\node\NodeInterface $node
74 public function tokenReplaceWithoutPassedBubbleableMetadata(NodeInterface $node) {
75 $build['#markup'] = $this->token->replace('Tokens: [node:nid] [current-user:uid]', ['node' => $node], []);