function system_tokens in Drupal 9
Same name and namespace in other branches
- 8 core/modules/system/system.tokens.inc \system_tokens()
- 7 modules/system/system.tokens.inc \system_tokens()
Implements hook_tokens().
File
- core/
modules/ system/ system.tokens.inc, line 94 - Builds placeholder replacement tokens system-wide data.
Code
function system_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
$url_options = [
'absolute' => TRUE,
];
if (isset($options['langcode'])) {
$url_options['language'] = \Drupal::languageManager()
->getLanguage($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = NULL;
}
$replacements = [];
if ($type == 'site') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'name':
$config = \Drupal::config('system.site');
$bubbleable_metadata
->addCacheableDependency($config);
$site_name = $config
->get('name');
$replacements[$original] = $site_name;
break;
case 'slogan':
$config = \Drupal::config('system.site');
$bubbleable_metadata
->addCacheableDependency($config);
$slogan = $config
->get('slogan');
$build = [
'#markup' => $slogan,
];
// @todo Fix in https://www.drupal.org/node/2577827
$replacements[$original] = \Drupal::service('renderer')
->renderPlain($build);
break;
case 'mail':
$config = \Drupal::config('system.site');
$bubbleable_metadata
->addCacheableDependency($config);
$replacements[$original] = $config
->get('mail');
break;
case 'url':
/** @var \Drupal\Core\GeneratedUrl $result */
$result = Url::fromRoute('<front>', [], $url_options)
->toString(TRUE);
$bubbleable_metadata
->addCacheableDependency($result);
$replacements[$original] = $result
->getGeneratedUrl();
break;
case 'url-brief':
/** @var \Drupal\Core\GeneratedUrl $result */
$result = Url::fromRoute('<front>', [], $url_options)
->toString(TRUE);
$bubbleable_metadata
->addCacheableDependency($result);
$replacements[$original] = preg_replace([
'!^https?://!',
'!/$!',
], '', $result
->getGeneratedUrl());
break;
case 'login-url':
/** @var \Drupal\Core\GeneratedUrl $result */
$result = Url::fromRoute('user.page', [], $url_options)
->toString(TRUE);
$bubbleable_metadata
->addCacheableDependency($result);
$replacements[$original] = $result
->getGeneratedUrl();
break;
}
}
}
elseif ($type == 'date') {
if (empty($data['date'])) {
$date = REQUEST_TIME;
// We depend on the current request time, so the tokens are not cacheable
// at all.
$bubbleable_metadata
->setCacheMaxAge(0);
}
else {
$date = $data['date'];
}
foreach ($tokens as $name => $original) {
switch ($name) {
case 'short':
case 'medium':
case 'long':
$date_format = DateFormat::load($name);
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($date, $name, '', NULL, $langcode);
break;
case 'since':
$replacements[$original] = \Drupal::service('date.formatter')
->formatTimeDiffSince($date, [
'langcode' => $langcode,
]);
$bubbleable_metadata
->setCacheMaxAge(0);
break;
case 'raw':
$replacements[$original] = $date;
break;
}
}
if ($created_tokens = $token_service
->findWithPrefix($tokens, 'custom')) {
foreach ($created_tokens as $name => $original) {
$replacements[$original] = \Drupal::service('date.formatter')
->format($date, 'custom', $name, NULL, $langcode);
}
}
}
return $replacements;
}