-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdomain.tokens.inc
152 lines (140 loc) · 4.83 KB
/
domain.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @file
* Builds placeholder replacement tokens for domain-related data.
*/
/**
* Implements hook_token_info().
*/
function domain_token_info() {
// Domain token types.
$info['types']['domain'] = array(
'name' => t('Domains'),
'description' => t('Tokens related to domains.'),
'needs-data' => 'domain',
);
$info['types']['current-domain'] = array(
'name' => t('Current domain'),
'description' => t('Tokens related to the current domain.'),
'type' => 'domain',
);
$info['types']['default-domain'] = array(
'name' => t('Default domain'),
'description' => t('Tokens related to the default domain.'),
'type' => 'domain',
);
// Domain tokens.
$info['tokens']['domain']['id'] = array(
'name' => t('Domain id'),
'description' => t('The domain ID.'),
);
$info['tokens']['domain']['machine-name'] = array(
'name' => t('Domain machine name'),
'description' => t('The domain machine identifier.'),
);
$info['tokens']['domain']['path'] = array(
'name' => t('Domain path'),
'description' => t('The base url path for the domain.'),
);
$info['tokens']['domain']['name'] = array(
'name' => t('Domain name'),
'description' => t('The domain name.'),
);
$info['tokens']['domain']['url'] = array(
'name' => t('Domain URL'),
'description' => t('The domain\'s URL, lower-cased and with only alphanumeric characters.'),
);
$info['tokens']['domain']['hostname'] = array(
'name' => t('Domain hostname'),
'description' => t('The domain hostname.'),
);
$info['tokens']['domain']['subdomain'] = array(
'name' => t('Subdomain'),
'description' => t('The subdomain, lower-cased and with only alphanumeric characters. Only works with *.example.com formats'),
);
$info['tokens']['node']['domain'] = array(
'name' => t('Domain information'),
'description' => t('The domain associated with this content.'),
'type' => 'domain',
);
return $info;
}
/**
* Implements hook_tokens().
*/
function domain_tokens($type, $tokens, array $data = array(), array $options = array()) {
$sanitize = !empty($options['sanitize']);
$replacements = array();
// Base token handling.
if ($type == 'domain' && !empty($data['domain'])) {
$domain = $data['domain'];
// Loop through the tokens and replace the elements.
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $domain['domain_id'];
break;
case 'machine-name':
case 'machine_name': // Deprecated and renamed to 'machine-name'.
$replacements[$original] = $domain['machine_name'];
break;
case 'name':
$replacements[$original] = $sanitize ? check_plain($domain['sitename']) : $domain['sitename'];
break;
case 'path':
if (!isset($domain['path'])) {
$domain['path'] = domain_get_path($domain);
}
$replacements[$original] = $domain['path'];
break;
case 'url':
$url = domain_url_encode($domain['subdomain']);
$replacements[$original] = $sanitize ? check_plain($url) : $url;
break;
case 'subdomain':
$subdomain_elements = explode('.', $domain['subdomain']);
if (count($subdomain_elements) > 2) {
$subdomain = $subdomain_elements[0];
}
else {
$subdomain = 'www';
}
$subdomain = domain_url_encode($subdomain);
$replacements[$original] = $sanitize ? check_plain($subdomain) : $subdomain;
break;
case 'hostname':
$subdomain = $domain['subdomain'];
$replacements[$original] = $sanitize ? check_plain($subdomain) : $subdomain;
break;
}
}
}
// Node tokens.
if ($type == 'node' && !empty($data['node']->nid)) {
$node = $data['node'];
$domain = domain_get_node_match($node->nid);
if (empty($domain) || $domain == -1) {
return $replacements;
}
// Loop through the tokens to not waste cycles.
foreach ($tokens as $name => $original) {
if ($name == 'domain') {
$replacements[$original] = $sanitize ? check_plain($domain['sitename']) : $domain['sitename'];
}
}
if ($domain_tokens = token_find_with_prefix($tokens, 'domain')) {
$replacements += token_generate('domain', $domain_tokens, array('domain' => $domain), $options);
}
}
// Current domain tokens.
elseif ($type == 'current-domain') {
$current_domain = domain_get_domain();
$replacements += token_generate('domain', $tokens, array('domain' => $current_domain), $options);
}
// Default domain tokens.
elseif ($type == 'default-domain') {
$default_domain = domain_default(FALSE);
$replacements += token_generate('domain', $tokens, array('domain' => $default_domain), $options);
}
return $replacements;
}