5 * Builds placeholder replacement tokens system-wide data.
7 * This file handles tokens for the global 'site' and 'date' tokens.
10 use Drupal\Core\Datetime\Entity\DateFormat;
11 use Drupal\Core\Render\BubbleableMetadata;
14 * Implements hook_token_info().
16 function system_token_info() {
18 'name' => t("Site information"),
19 'description' => t("Tokens for site-wide settings and other global information."),
23 'description' => t("Tokens related to times and dates."),
26 // Site-wide global tokens.
29 'description' => t("The name of the site."),
32 'name' => t("Slogan"),
33 'description' => t("The slogan of the site."),
37 'description' => t("The administrative email address for the site."),
41 'description' => t("The URL of the site's front page."),
43 $site['url-brief'] = [
44 'name' => t("URL (brief)"),
45 'description' => t("The URL of the site's front page without the protocol."),
47 $site['login-url'] = [
48 'name' => t("Login page"),
49 'description' => t("The URL of the site's login page."),
52 // Date related tokens.
54 'name' => t("Short format"),
55 'description' => t("A date in 'short' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'short')]),
58 'name' => t("Medium format"),
59 'description' => t("A date in 'medium' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'medium')]),
62 'name' => t("Long format"),
63 'description' => t("A date in 'long' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'long')]),
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.'),
70 'name' => t("Time-since"),
71 'description' => t("A date in 'time-since' format. (%date)", ['%date' => \Drupal::service('date.formatter')->formatTimeDiffSince(REQUEST_TIME - 360)]),
74 'name' => t("Raw timestamp"),
75 'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]),
88 * Implements hook_tokens().
90 function system_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
91 $token_service = \Drupal::token();
93 $url_options = ['absolute' => TRUE];
94 if (isset($options['langcode'])) {
95 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
96 $langcode = $options['langcode'];
103 if ($type == 'site') {
104 foreach ($tokens as $name => $original) {
107 $config = \Drupal::config('system.site');
108 $bubbleable_metadata->addCacheableDependency($config);
109 $site_name = $config->get('name');
110 $replacements[$original] = $site_name;
114 $config = \Drupal::config('system.site');
115 $bubbleable_metadata->addCacheableDependency($config);
116 $slogan = $config->get('slogan');
118 '#markup' => $slogan,
120 // @todo Fix in https://www.drupal.org/node/2577827
121 $replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
125 $config = \Drupal::config('system.site');
126 $bubbleable_metadata->addCacheableDependency($config);
127 $replacements[$original] = $config->get('mail');
131 /** @var \Drupal\Core\GeneratedUrl $result */
132 $result = \Drupal::url('<front>', [], $url_options, TRUE);
133 $bubbleable_metadata->addCacheableDependency($result);
134 $replacements[$original] = $result->getGeneratedUrl();
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());
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();
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
159 $bubbleable_metadata->setCacheMaxAge(0);
162 $date = $data['date'];
165 foreach ($tokens as $name => $original) {
170 $date_format = DateFormat::load($name);
171 $bubbleable_metadata->addCacheableDependency($date_format);
172 $replacements[$original] = format_date($date, $name, '', NULL, $langcode);
176 $replacements[$original] = \Drupal::service('date.formatter')->formatTimeDiffSince($date, ['langcode' => $langcode]);
177 $bubbleable_metadata->setCacheMaxAge(0);
181 $replacements[$original] = $date;
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);
193 return $replacements;