-
Notifications
You must be signed in to change notification settings - Fork 2
/
varbase_core.tokens.inc
71 lines (63 loc) · 2.05 KB
/
varbase_core.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* @file
* Contains varbase_core.tokens.inc.
*/
/**
* To have all varbase core general and global tokens.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function varbase_core_token_info() {
// Default theme token.
$info['types']['default-active-theme'] = [
'name' => t('Default theme'),
'description' => t('Tokens related to the Default theme.'),
];
$info['tokens']['default-active-theme']['path'] = [
'name' => t('Path'),
'description' => t('The path of the Default theme.'),
];
$info['tokens']['site']['origin-url'] = [
'name' => t("Origin URL"),
'description' => t("The Origin URL (scheme and HTTP host) of the site. No language prefix."),
];
return $info;
}
/**
* Implements hook_tokens().
*/
function varbase_core_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'site') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'origin-url':
// Until #1088112: Introduce a token to get site's base URL is
// committed,
// https://www.drupal.org/project/drupal/issues/1088112
// let's use a custom token. Let's call it: [site:origin-url].
// No language prefix in the url.
// https://www.drupal.org/project/varbase_core/issues/3106793
// -----------------------------------------------------------------
/** @var \Symfony\Component\HttpFoundation\Request $origin_url */
$origin_url = \Drupal::request()->getSchemeAndHttpHost() . \Drupal::request()->getBaseUrl();
$bubbleable_metadata->addCacheContexts(['url.site']);
$replacements[$original] = $origin_url;
break;
}
}
}
elseif ($type == 'default-active-theme') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'path':
$replacements[$original] = \Drupal::theme()->getActiveTheme()->getPath();
break;
}
}
}
return $replacements;
}