Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/system/system.tokens.inc @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
5 * Builds placeholder replacement tokens system-wide data. | 5 * Builds placeholder replacement tokens system-wide data. |
6 * | 6 * |
7 * This file handles tokens for the global 'site' and 'date' tokens. | 7 * This file handles tokens for the global 'site' and 'date' tokens. |
8 */ | 8 */ |
9 | 9 |
10 use Drupal\Core\Url; | |
10 use Drupal\Core\Datetime\Entity\DateFormat; | 11 use Drupal\Core\Datetime\Entity\DateFormat; |
11 use Drupal\Core\Render\BubbleableMetadata; | 12 use Drupal\Core\Render\BubbleableMetadata; |
12 | 13 |
13 /** | 14 /** |
14 * Implements hook_token_info(). | 15 * Implements hook_token_info(). |
47 $site['login-url'] = [ | 48 $site['login-url'] = [ |
48 'name' => t("Login page"), | 49 'name' => t("Login page"), |
49 'description' => t("The URL of the site's login page."), | 50 'description' => t("The URL of the site's login page."), |
50 ]; | 51 ]; |
51 | 52 |
53 /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */ | |
54 $date_formatter = \Drupal::service('date.formatter'); | |
55 | |
52 // Date related tokens. | 56 // Date related tokens. |
53 $date['short'] = [ | 57 $date['short'] = [ |
54 'name' => t("Short format"), | 58 'name' => t("Short format"), |
55 'description' => t("A date in 'short' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'short')]), | 59 'description' => t("A date in 'short' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'short')]), |
56 ]; | 60 ]; |
57 $date['medium'] = [ | 61 $date['medium'] = [ |
58 'name' => t("Medium format"), | 62 'name' => t("Medium format"), |
59 'description' => t("A date in 'medium' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'medium')]), | 63 'description' => t("A date in 'medium' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'medium')]), |
60 ]; | 64 ]; |
61 $date['long'] = [ | 65 $date['long'] = [ |
62 'name' => t("Long format"), | 66 'name' => t("Long format"), |
63 'description' => t("A date in 'long' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'long')]), | 67 'description' => t("A date in 'long' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'long')]), |
64 ]; | 68 ]; |
65 $date['custom'] = [ | 69 $date['custom'] = [ |
66 'name' => t("Custom format"), | 70 '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.'), | 71 '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 ]; | 72 ]; |
69 $date['since'] = [ | 73 $date['since'] = [ |
70 'name' => t("Time-since"), | 74 'name' => t("Time-since"), |
71 'description' => t("A date in 'time-since' format. (%date)", ['%date' => \Drupal::service('date.formatter')->formatTimeDiffSince(REQUEST_TIME - 360)]), | 75 'description' => t("A date in 'time-since' format. (%date)", ['%date' => $date_formatter->formatTimeDiffSince(REQUEST_TIME - 360)]), |
72 ]; | 76 ]; |
73 $date['raw'] = [ | 77 $date['raw'] = [ |
74 'name' => t("Raw timestamp"), | 78 'name' => t("Raw timestamp"), |
75 'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]), | 79 'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]), |
76 ]; | 80 ]; |
127 $replacements[$original] = $config->get('mail'); | 131 $replacements[$original] = $config->get('mail'); |
128 break; | 132 break; |
129 | 133 |
130 case 'url': | 134 case 'url': |
131 /** @var \Drupal\Core\GeneratedUrl $result */ | 135 /** @var \Drupal\Core\GeneratedUrl $result */ |
132 $result = \Drupal::url('<front>', [], $url_options, TRUE); | 136 $result = Url::fromRoute('<front>', [], $url_options)->toString(TRUE); |
133 $bubbleable_metadata->addCacheableDependency($result); | 137 $bubbleable_metadata->addCacheableDependency($result); |
134 $replacements[$original] = $result->getGeneratedUrl(); | 138 $replacements[$original] = $result->getGeneratedUrl(); |
135 break; | 139 break; |
136 | 140 |
137 case 'url-brief': | 141 case 'url-brief': |
138 /** @var \Drupal\Core\GeneratedUrl $result */ | 142 /** @var \Drupal\Core\GeneratedUrl $result */ |
139 $result = \Drupal::url('<front>', [], $url_options, TRUE); | 143 $result = Url::fromRoute('<front>', [], $url_options)->toString(TRUE); |
140 $bubbleable_metadata->addCacheableDependency($result); | 144 $bubbleable_metadata->addCacheableDependency($result); |
141 $replacements[$original] = preg_replace(['!^https?://!', '!/$!'], '', $result->getGeneratedUrl()); | 145 $replacements[$original] = preg_replace(['!^https?://!', '!/$!'], '', $result->getGeneratedUrl()); |
142 break; | 146 break; |
143 | 147 |
144 case 'login-url': | 148 case 'login-url': |
145 /** @var \Drupal\Core\GeneratedUrl $result */ | 149 /** @var \Drupal\Core\GeneratedUrl $result */ |
146 $result = \Drupal::url('user.page', [], $url_options, TRUE); | 150 $result = Url::fromRoute('user.page', [], $url_options)->toString(TRUE); |
147 $bubbleable_metadata->addCacheableDependency($result); | 151 $bubbleable_metadata->addCacheableDependency($result); |
148 $replacements[$original] = $result->getGeneratedUrl(); | 152 $replacements[$original] = $result->getGeneratedUrl(); |
149 break; | 153 break; |
150 } | 154 } |
151 } | 155 } |
167 case 'short': | 171 case 'short': |
168 case 'medium': | 172 case 'medium': |
169 case 'long': | 173 case 'long': |
170 $date_format = DateFormat::load($name); | 174 $date_format = DateFormat::load($name); |
171 $bubbleable_metadata->addCacheableDependency($date_format); | 175 $bubbleable_metadata->addCacheableDependency($date_format); |
172 $replacements[$original] = format_date($date, $name, '', NULL, $langcode); | 176 $replacements[$original] = \Drupal::service('date.formatter')->format($date, $name, '', NULL, $langcode); |
173 break; | 177 break; |
174 | 178 |
175 case 'since': | 179 case 'since': |
176 $replacements[$original] = \Drupal::service('date.formatter')->formatTimeDiffSince($date, ['langcode' => $langcode]); | 180 $replacements[$original] = \Drupal::service('date.formatter')->formatTimeDiffSince($date, ['langcode' => $langcode]); |
177 $bubbleable_metadata->setCacheMaxAge(0); | 181 $bubbleable_metadata->setCacheMaxAge(0); |
183 } | 187 } |
184 } | 188 } |
185 | 189 |
186 if ($created_tokens = $token_service->findWithPrefix($tokens, 'custom')) { | 190 if ($created_tokens = $token_service->findWithPrefix($tokens, 'custom')) { |
187 foreach ($created_tokens as $name => $original) { | 191 foreach ($created_tokens as $name => $original) { |
188 $replacements[$original] = format_date($date, 'custom', $name, NULL, $langcode); | 192 $replacements[$original] = \Drupal::service('date.formatter')->format($date, 'custom', $name, NULL, $langcode); |
189 } | 193 } |
190 } | 194 } |
191 } | 195 } |
192 | 196 |
193 return $replacements; | 197 return $replacements; |