system.tokens.inc 6.0 KB

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