Version 1
[yaffs-website] / web / core / modules / system / system.tokens.inc
1 <?php
2
3 /**
4  * @file
5  * Builds placeholder replacement tokens system-wide data.
6  *
7  * This file handles tokens for the global 'site' and 'date' tokens.
8  */
9
10 use Drupal\Core\Datetime\Entity\DateFormat;
11 use Drupal\Core\Render\BubbleableMetadata;
12
13 /**
14  * Implements hook_token_info().
15  */
16 function system_token_info() {
17   $types['site'] = [
18     'name' => t("Site information"),
19     'description' => t("Tokens for site-wide settings and other global information."),
20   ];
21   $types['date'] = [
22     'name' => t("Dates"),
23     'description' => t("Tokens related to times and dates."),
24   ];
25
26   // Site-wide global tokens.
27   $site['name'] = [
28     'name' => t("Name"),
29     'description' => t("The name of the site."),
30   ];
31   $site['slogan'] = [
32     'name' => t("Slogan"),
33     'description' => t("The slogan of the site."),
34   ];
35   $site['mail'] = [
36     'name' => t("Email"),
37     'description' => t("The administrative email address for the site."),
38   ];
39   $site['url'] = [
40     'name' => t("URL"),
41     'description' => t("The URL of the site's front page."),
42   ];
43   $site['url-brief'] = [
44     'name' => t("URL (brief)"),
45     'description' => t("The URL of the site's front page without the protocol."),
46   ];
47   $site['login-url'] = [
48     'name' => t("Login page"),
49     'description' => t("The URL of the site's login page."),
50   ];
51
52   // Date related tokens.
53   $date['short'] = [
54     'name' => t("Short format"),
55     'description' => t("A date in 'short' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'short')]),
56   ];
57   $date['medium'] = [
58     'name' => t("Medium format"),
59     'description' => t("A date in 'medium' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'medium')]),
60   ];
61   $date['long'] = [
62     'name' => t("Long format"),
63     'description' => t("A date in 'long' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'long')]),
64   ];
65   $date['custom'] = [
66     'name' => t("Custom format"),
67     'description' => t('A date in a custom format. See <a href="http://php.net/manual/function.date.php">the PHP documentation</a> for details.'),
68   ];
69   $date['since'] = [
70     'name' => t("Time-since"),
71     'description' => t("A date in 'time-since' format. (%date)", ['%date' => \Drupal::service('date.formatter')->formatTimeDiffSince(REQUEST_TIME - 360)]),
72   ];
73   $date['raw'] = [
74     'name' => t("Raw timestamp"),
75     'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]),
76   ];
77
78   return [
79     'types' => $types,
80     'tokens' => [
81       'site' => $site,
82       'date' => $date,
83     ],
84   ];
85 }
86
87 /**
88  * Implements hook_tokens().
89  */
90 function system_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
91   $token_service = \Drupal::token();
92
93   $url_options = ['absolute' => TRUE];
94   if (isset($options['langcode'])) {
95     $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
96     $langcode = $options['langcode'];
97   }
98   else {
99     $langcode = NULL;
100   }
101   $replacements = [];
102
103   if ($type == 'site') {
104     foreach ($tokens as $name => $original) {
105       switch ($name) {
106         case 'name':
107           $config = \Drupal::config('system.site');
108           $bubbleable_metadata->addCacheableDependency($config);
109           $site_name = $config->get('name');
110           $replacements[$original] = $site_name;
111           break;
112
113         case 'slogan':
114           $config = \Drupal::config('system.site');
115           $bubbleable_metadata->addCacheableDependency($config);
116           $slogan = $config->get('slogan');
117           $build = [
118             '#markup' => $slogan,
119           ];
120           // @todo Fix in https://www.drupal.org/node/2577827
121           $replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
122           break;
123
124         case 'mail':
125           $config = \Drupal::config('system.site');
126           $bubbleable_metadata->addCacheableDependency($config);
127           $replacements[$original] = $config->get('mail');
128           break;
129
130         case 'url':
131           /** @var \Drupal\Core\GeneratedUrl $result */
132           $result = \Drupal::url('<front>', [], $url_options, TRUE);
133           $bubbleable_metadata->addCacheableDependency($result);
134           $replacements[$original] = $result->getGeneratedUrl();
135           break;
136
137         case 'url-brief':
138           /** @var \Drupal\Core\GeneratedUrl $result */
139           $result = \Drupal::url('<front>', [], $url_options, TRUE);
140           $bubbleable_metadata->addCacheableDependency($result);
141           $replacements[$original] = preg_replace(['!^https?://!', '!/$!'], '', $result->getGeneratedUrl());
142           break;
143
144         case 'login-url':
145           /** @var \Drupal\Core\GeneratedUrl $result */
146           $result = \Drupal::url('user.page', [], $url_options, TRUE);
147           $bubbleable_metadata->addCacheableDependency($result);
148           $replacements[$original] = $result->getGeneratedUrl();
149           break;
150       }
151     }
152   }
153
154   elseif ($type == 'date') {
155     if (empty($data['date'])) {
156       $date = REQUEST_TIME;
157       // We depend on the current request time, so the tokens are not cacheable
158       // at all.
159       $bubbleable_metadata->setCacheMaxAge(0);
160     }
161     else {
162       $date = $data['date'];
163     }
164
165     foreach ($tokens as $name => $original) {
166       switch ($name) {
167         case 'short':
168         case 'medium':
169         case 'long':
170           $date_format = DateFormat::load($name);
171           $bubbleable_metadata->addCacheableDependency($date_format);
172           $replacements[$original] = format_date($date, $name, '', NULL, $langcode);
173           break;
174
175         case 'since':
176           $replacements[$original] = \Drupal::service('date.formatter')->formatTimeDiffSince($date, ['langcode' => $langcode]);
177           $bubbleable_metadata->setCacheMaxAge(0);
178           break;
179
180         case 'raw':
181           $replacements[$original] = $date;
182           break;
183       }
184     }
185
186     if ($created_tokens = $token_service->findWithPrefix($tokens, 'custom')) {
187       foreach ($created_tokens as $name => $original) {
188         $replacements[$original] = format_date($date, 'custom', $name, NULL, $langcode);
189       }
190     }
191   }
192
193   return $replacements;
194 }