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