system.tokens.inc 6.3 KB

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