Chris@0: t("Site information"), Chris@0: 'description' => t("Tokens for site-wide settings and other global information."), Chris@0: ]; Chris@0: $types['date'] = [ Chris@0: 'name' => t("Dates"), Chris@0: 'description' => t("Tokens related to times and dates."), Chris@0: ]; Chris@0: Chris@0: // Site-wide global tokens. Chris@0: $site['name'] = [ Chris@0: 'name' => t("Name"), Chris@0: 'description' => t("The name of the site."), Chris@0: ]; Chris@0: $site['slogan'] = [ Chris@0: 'name' => t("Slogan"), Chris@0: 'description' => t("The slogan of the site."), Chris@0: ]; Chris@0: $site['mail'] = [ Chris@0: 'name' => t("Email"), Chris@0: 'description' => t("The administrative email address for the site."), Chris@0: ]; Chris@0: $site['url'] = [ Chris@0: 'name' => t("URL"), Chris@0: 'description' => t("The URL of the site's front page."), Chris@0: ]; Chris@0: $site['url-brief'] = [ Chris@0: 'name' => t("URL (brief)"), Chris@0: 'description' => t("The URL of the site's front page without the protocol."), Chris@0: ]; Chris@0: $site['login-url'] = [ Chris@0: 'name' => t("Login page"), Chris@0: 'description' => t("The URL of the site's login page."), Chris@0: ]; Chris@0: Chris@18: /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */ Chris@18: $date_formatter = \Drupal::service('date.formatter'); Chris@18: Chris@0: // Date related tokens. Chris@0: $date['short'] = [ Chris@0: 'name' => t("Short format"), Chris@18: 'description' => t("A date in 'short' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'short')]), Chris@0: ]; Chris@0: $date['medium'] = [ Chris@0: 'name' => t("Medium format"), Chris@18: 'description' => t("A date in 'medium' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'medium')]), Chris@0: ]; Chris@0: $date['long'] = [ Chris@0: 'name' => t("Long format"), Chris@18: 'description' => t("A date in 'long' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'long')]), Chris@0: ]; Chris@0: $date['custom'] = [ Chris@0: 'name' => t("Custom format"), Chris@0: 'description' => t('A date in a custom format. See the PHP documentation for details.'), Chris@0: ]; Chris@0: $date['since'] = [ Chris@0: 'name' => t("Time-since"), Chris@18: 'description' => t("A date in 'time-since' format. (%date)", ['%date' => $date_formatter->formatTimeDiffSince(REQUEST_TIME - 360)]), Chris@0: ]; Chris@0: $date['raw'] = [ Chris@0: 'name' => t("Raw timestamp"), Chris@0: 'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]), Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'types' => $types, Chris@0: 'tokens' => [ Chris@0: 'site' => $site, Chris@0: 'date' => $date, Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_tokens(). Chris@0: */ Chris@0: function system_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { Chris@0: $token_service = \Drupal::token(); Chris@0: Chris@0: $url_options = ['absolute' => TRUE]; Chris@0: if (isset($options['langcode'])) { Chris@0: $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); Chris@0: $langcode = $options['langcode']; Chris@0: } Chris@0: else { Chris@0: $langcode = NULL; Chris@0: } Chris@0: $replacements = []; Chris@0: Chris@0: if ($type == 'site') { Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: case 'name': Chris@0: $config = \Drupal::config('system.site'); Chris@0: $bubbleable_metadata->addCacheableDependency($config); Chris@0: $site_name = $config->get('name'); Chris@0: $replacements[$original] = $site_name; Chris@0: break; Chris@0: Chris@0: case 'slogan': Chris@0: $config = \Drupal::config('system.site'); Chris@0: $bubbleable_metadata->addCacheableDependency($config); Chris@0: $slogan = $config->get('slogan'); Chris@0: $build = [ Chris@0: '#markup' => $slogan, Chris@0: ]; Chris@0: // @todo Fix in https://www.drupal.org/node/2577827 Chris@0: $replacements[$original] = \Drupal::service('renderer')->renderPlain($build); Chris@0: break; Chris@0: Chris@0: case 'mail': Chris@0: $config = \Drupal::config('system.site'); Chris@0: $bubbleable_metadata->addCacheableDependency($config); Chris@0: $replacements[$original] = $config->get('mail'); Chris@0: break; Chris@0: Chris@0: case 'url': Chris@0: /** @var \Drupal\Core\GeneratedUrl $result */ Chris@18: $result = Url::fromRoute('', [], $url_options)->toString(TRUE); Chris@0: $bubbleable_metadata->addCacheableDependency($result); Chris@0: $replacements[$original] = $result->getGeneratedUrl(); Chris@0: break; Chris@0: Chris@0: case 'url-brief': Chris@0: /** @var \Drupal\Core\GeneratedUrl $result */ Chris@18: $result = Url::fromRoute('', [], $url_options)->toString(TRUE); Chris@0: $bubbleable_metadata->addCacheableDependency($result); Chris@0: $replacements[$original] = preg_replace(['!^https?://!', '!/$!'], '', $result->getGeneratedUrl()); Chris@0: break; Chris@0: Chris@0: case 'login-url': Chris@0: /** @var \Drupal\Core\GeneratedUrl $result */ Chris@18: $result = Url::fromRoute('user.page', [], $url_options)->toString(TRUE); Chris@0: $bubbleable_metadata->addCacheableDependency($result); Chris@0: $replacements[$original] = $result->getGeneratedUrl(); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: elseif ($type == 'date') { Chris@0: if (empty($data['date'])) { Chris@0: $date = REQUEST_TIME; Chris@0: // We depend on the current request time, so the tokens are not cacheable Chris@0: // at all. Chris@0: $bubbleable_metadata->setCacheMaxAge(0); Chris@0: } Chris@0: else { Chris@0: $date = $data['date']; Chris@0: } Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: case 'short': Chris@0: case 'medium': Chris@0: case 'long': Chris@0: $date_format = DateFormat::load($name); Chris@0: $bubbleable_metadata->addCacheableDependency($date_format); Chris@18: $replacements[$original] = \Drupal::service('date.formatter')->format($date, $name, '', NULL, $langcode); Chris@0: break; Chris@0: Chris@0: case 'since': Chris@0: $replacements[$original] = \Drupal::service('date.formatter')->formatTimeDiffSince($date, ['langcode' => $langcode]); Chris@0: $bubbleable_metadata->setCacheMaxAge(0); Chris@0: break; Chris@0: Chris@0: case 'raw': Chris@0: $replacements[$original] = $date; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($created_tokens = $token_service->findWithPrefix($tokens, 'custom')) { Chris@0: foreach ($created_tokens as $name => $original) { Chris@18: $replacements[$original] = \Drupal::service('date.formatter')->format($date, 'custom', $name, NULL, $langcode); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $replacements; Chris@0: }