system.tokens.inc 6.01 KB
Newer Older
1 2 3 4 5 6
<?php

/**
 * @file
 * Builds placeholder replacement tokens system-wide data.
 *
7
 * This file handles tokens for the global 'site' and 'date' tokens.
8 9
 */

10 11
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Render\BubbleableMetadata;
12

13
/**
14
 * Implements hook_token_info().
15 16
 */
function system_token_info() {
17
  $types['site'] = [
18 19
    'name' => t("Site information"),
    'description' => t("Tokens for site-wide settings and other global information."),
20 21
  ];
  $types['date'] = [
22 23
    'name' => t("Dates"),
    'description' => t("Tokens related to times and dates."),
24
  ];
25 26

  // Site-wide global tokens.
27
  $site['name'] = [
28 29
    'name' => t("Name"),
    'description' => t("The name of the site."),
30 31
  ];
  $site['slogan'] = [
32 33
    'name' => t("Slogan"),
    'description' => t("The slogan of the site."),
34 35
  ];
  $site['mail'] = [
36 37
    'name' => t("Email"),
    'description' => t("The administrative email address for the site."),
38 39
  ];
  $site['url'] = [
40 41
    'name' => t("URL"),
    'description' => t("The URL of the site's front page."),
42 43
  ];
  $site['url-brief'] = [
44 45
    'name' => t("URL (brief)"),
    'description' => t("The URL of the site's front page without the protocol."),
46 47
  ];
  $site['login-url'] = [
48 49
    'name' => t("Login page"),
    'description' => t("The URL of the site's login page."),
50
  ];
51 52

  // Date related tokens.
53
  $date['short'] = [
54
    'name' => t("Short format"),
55 56 57
    'description' => t("A date in 'short' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'short')]),
  ];
  $date['medium'] = [
58
    'name' => t("Medium format"),
59 60 61
    'description' => t("A date in 'medium' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'medium')]),
  ];
  $date['long'] = [
62
    'name' => t("Long format"),
63 64 65
    'description' => t("A date in 'long' format. (%date)", ['%date' => format_date(REQUEST_TIME, 'long')]),
  ];
  $date['custom'] = [
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.'),
68 69
  ];
  $date['since'] = [
70
    'name' => t("Time-since"),
71 72 73
    'description' => t("A date in 'time-since' format. (%date)", ['%date' => \Drupal::service('date.formatter')->formatTimeDiffSince(REQUEST_TIME - 360)]),
  ];
  $date['raw'] = [
74
    'name' => t("Raw timestamp"),
75 76
    'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]),
  ];
77

78
  return [
79
    'types' => $types,
80
    'tokens' => [
81 82
      'site' => $site,
      'date' => $date,
83 84
    ],
  ];
85 86 87
}

/**
88
 * Implements hook_tokens().
89
 */
90
function system_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
91
  $token_service = \Drupal::token();
92

93
  $url_options = ['absolute' => TRUE];
94
  if (isset($options['langcode'])) {
95
    $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
96
    $langcode = $options['langcode'];
97 98
  }
  else {
99
    $langcode = NULL;
100
  }
101
  $replacements = [];
102 103 104 105 106

  if ($type == 'site') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
107 108 109
          $config = \Drupal::config('system.site');
          $bubbleable_metadata->addCacheableDependency($config);
          $site_name = $config->get('name');
110
          $replacements[$original] = $site_name;
111 112 113
          break;

        case 'slogan':
114 115 116
          $config = \Drupal::config('system.site');
          $bubbleable_metadata->addCacheableDependency($config);
          $slogan = $config->get('slogan');
117 118 119 120 121
          $build = [
            '#markup' => $slogan,
          ];
          // @todo Fix in https://www.drupal.org/node/2577827
          $replacements[$original] = \Drupal::service('renderer')->renderPlain($build);
122 123 124
          break;

        case 'mail':
125 126 127
          $config = \Drupal::config('system.site');
          $bubbleable_metadata->addCacheableDependency($config);
          $replacements[$original] = $config->get('mail');
128 129 130
          break;

        case 'url':
131
          /** @var \Drupal\Core\GeneratedUrl $result */
132
          $result = \Drupal::url('<front>', [], $url_options, TRUE);
133 134
          $bubbleable_metadata->addCacheableDependency($result);
          $replacements[$original] = $result->getGeneratedUrl();
135 136
          break;

137
        case 'url-brief':
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());
142 143
          break;

144
        case 'login-url':
145 146 147 148
          /** @var \Drupal\Core\GeneratedUrl $result */
          $result = \Drupal::url('user.page', [], $url_options, TRUE);
          $bubbleable_metadata->addCacheableDependency($result);
          $replacements[$original] = $result->getGeneratedUrl();
149 150 151 152 153 154 155 156
          break;
      }
    }
  }

  elseif ($type == 'date') {
    if (empty($data['date'])) {
      $date = REQUEST_TIME;
157 158 159
      // We depend on the current request time, so the tokens are not cacheable
      // at all.
      $bubbleable_metadata->setCacheMaxAge(0);
160 161 162 163 164 165 166
    }
    else {
      $date = $data['date'];
    }

    foreach ($tokens as $name => $original) {
      switch ($name) {
167
        case 'short':
168
        case 'medium':
169
        case 'long':
170
          $date_format = DateFormat::load($name);
171
          $bubbleable_metadata->addCacheableDependency($date_format);
172
          $replacements[$original] = format_date($date, $name, '', NULL, $langcode);
173 174 175
          break;

        case 'since':
176
          $replacements[$original] = \Drupal::service('date.formatter')->formatTimeDiffSince($date, ['langcode' => $langcode]);
177
          $bubbleable_metadata->setCacheMaxAge(0);
178
          break;
179 180

        case 'raw':
181
          $replacements[$original] = $date;
182
          break;
183 184 185
      }
    }

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);
189 190 191 192 193 194
      }
    }
  }

  return $replacements;
}