3 namespace Drupal\token_test\Controller;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Render\BubbleableMetadata;
8 use Drupal\Core\Utility\Token;
9 use Drupal\node\NodeInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a test controller for token replacement.
15 class TestController extends ControllerBase {
18 * The token replacement system.
20 * @var \Drupal\Core\Utility\Token
25 * Constructs a new TestController instance.
27 * @param \Drupal\Core\Utility\Token $token
28 * The token replacement system.
30 public function __construct(Token $token) {
31 $this->token = $token;
37 public static function create(ContainerInterface $container) {
38 return new static($container->get('token'));
42 * Provides a token replacement with a node as well as the current user.
44 * This controller passes an explicit bubbleable metadata object to
45 * $this->token->replace(), and applies the collected metadata to the render
48 * @param \Drupal\node\NodeInterface $node
54 public function tokenReplace(NodeInterface $node) {
55 $bubbleable_metadata = new BubbleableMetadata();
56 $build['#markup'] = $this->token->replace('Tokens: [node:nid] [current-user:uid]', ['node' => $node], [], $bubbleable_metadata);
57 $bubbleable_metadata->applyTo($build);
63 * Provides a token replacement with a node as well as the current user.
65 * This controller is for testing the token service's fallback behavior of
66 * applying collected metadata to the currently active render context when an
67 * explicit bubbleable metadata object isn't passed in.
69 * @param \Drupal\node\NodeInterface $node
75 public function tokenReplaceWithoutPassedBubbleableMetadata(NodeInterface $node) {
76 $build['#markup'] = $this->token->replace('Tokens: [node:nid] [current-user:uid]', ['node' => $node], []);