Version 1
[yaffs-website] / web / core / modules / system / tests / modules / token_test / src / Controller / TestController.php
1 <?php
2
3 namespace Drupal\token_test\Controller;
4
5
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;
11
12 /**
13  * Provides a test controller for token replacement.
14  */
15 class TestController extends ControllerBase {
16
17   /**
18    * The token replacement system.
19    *
20    * @var \Drupal\Core\Utility\Token
21    */
22   protected $token;
23
24   /**
25    * Constructs a new TestController instance.
26    *
27    * @param \Drupal\Core\Utility\Token $token
28    *   The token replacement system.
29    */
30   public function __construct(Token $token) {
31     $this->token = $token;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static($container->get('token'));
39   }
40
41   /**
42    * Provides a token replacement with a node as well as the current user.
43    *
44    * This controller passes an explicit bubbleable metadata object to
45    * $this->token->replace(), and applies the collected metadata to the render
46    * array being built.
47    *
48    * @param \Drupal\node\NodeInterface $node
49    *   The node.
50    *
51    * @return array
52    *   The render array.
53    */
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);
58
59     return $build;
60   }
61
62   /**
63    * Provides a token replacement with a node as well as the current user.
64    *
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.
68    *
69    * @param \Drupal\node\NodeInterface $node
70    *   The node.
71    *
72    * @return array
73    *   The render array.
74    */
75   public function tokenReplaceWithoutPassedBubbleableMetadata(NodeInterface $node) {
76     $build['#markup'] = $this->token->replace('Tokens: [node:nid] [current-user:uid]', ['node' => $node], []);
77
78     return $build;
79   }
80
81 }