annotate core/modules/system/system.tokens.inc @ 19:fa3358dc1485 tip

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