1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Builds placeholder replacement tokens for taxonomy terms and vocabularies.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_token_info().
|
10
|
*/
|
11
|
function i18n_taxonomy_token_info() {
|
12
|
|
13
|
// Taxonomy term related variables.
|
14
|
$term['i18n-name'] = array(
|
15
|
'name' => t("Name (localized)"),
|
16
|
'description' => t("The name of the taxonomy term."),
|
17
|
);
|
18
|
$term['i18n-description'] = array(
|
19
|
'name' => t("Description (localized)"),
|
20
|
'description' => t("The optional description of the taxonomy term."),
|
21
|
);
|
22
|
|
23
|
// Taxonomy vocabulary related variables.
|
24
|
$vocabulary['i18n-name'] = array(
|
25
|
'name' => t("Name (localized)"),
|
26
|
'description' => t("The name of the taxonomy vocabulary."),
|
27
|
);
|
28
|
$vocabulary['i18n-description'] = array(
|
29
|
'name' => t("Description (localized)"),
|
30
|
'description' => t("The optional description of the taxonomy vocabulary."),
|
31
|
);
|
32
|
|
33
|
// Chained tokens for taxonomies
|
34
|
$term['i18n-vocabulary'] = array(
|
35
|
'name' => t("Vocabulary (localized)"),
|
36
|
'description' => t("The vocabulary the taxonomy term belongs to."),
|
37
|
'type' => 'vocabulary',
|
38
|
);
|
39
|
$term['i18n-parent'] = array(
|
40
|
'name' => t("Parent term (localized)"),
|
41
|
'description' => t("The parent term of the taxonomy term, if one exists."),
|
42
|
'type' => 'term',
|
43
|
);
|
44
|
|
45
|
return array(
|
46
|
'tokens' => array(
|
47
|
'term' => $term,
|
48
|
'vocabulary' => $vocabulary,
|
49
|
),
|
50
|
);
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* Implements hook_tokens().
|
55
|
*/
|
56
|
function i18n_taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
57
|
$replacements = array();
|
58
|
$sanitize = !empty($options['sanitize']);
|
59
|
$langcode = isset($options['language']) ? $options['language']->language : i18n_langcode();
|
60
|
|
61
|
if ($type == 'term' && !empty($data['term'])) {
|
62
|
$term = $data['term'];
|
63
|
|
64
|
foreach ($tokens as $name => $original) {
|
65
|
switch ($name) {
|
66
|
|
67
|
case 'i18n-name':
|
68
|
$name = i18n_taxonomy_term_name($term, $langcode);
|
69
|
$replacements[$original] = $sanitize ? check_plain($name) : $name;
|
70
|
break;
|
71
|
|
72
|
case 'i18n-description':
|
73
|
$replacements[$original] = i18n_string_text(array('taxonomy', 'term', $term->tid, 'description'), $term->description, array('langcode' => $langcode, 'format' => $term->format, 'sanitize' => $sanitize, 'cache' => TRUE));
|
74
|
break;
|
75
|
|
76
|
case 'i18n-vocabulary':
|
77
|
$vocabulary = taxonomy_vocabulary_load($term->vid);
|
78
|
$replacements[$original] = check_plain(i18n_taxonomy_vocabulary_name($vocabulary, $langcode));
|
79
|
break;
|
80
|
|
81
|
case 'i18n-parent':
|
82
|
if ($parents = taxonomy_get_parents($term->tid)) {
|
83
|
$parent = array_pop($parents);
|
84
|
$replacements[$original] = check_plain(i18n_taxonomy_term_name($parent, $langcode));
|
85
|
}
|
86
|
break;
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
|
91
|
elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
|
92
|
$vocabulary = $data['vocabulary'];
|
93
|
|
94
|
foreach ($tokens as $name => $original) {
|
95
|
switch ($name) {
|
96
|
|
97
|
case 'i18n-name':
|
98
|
$name = i18n_taxonomy_vocabulary_name($vocabulary, $langcode);
|
99
|
$replacements[$original] = $sanitize ? check_plain($name) : $name;
|
100
|
break;
|
101
|
|
102
|
case 'i18n-description':
|
103
|
$description = i18n_object_langcode($vocabulary) ? $vocabulary->description : i18n_string(array('taxonomy', 'vocabulary', $vocabulary->vid, 'description'), $vocabulary->description, array('langcode' => $langcode));
|
104
|
$replacements[$original] = $sanitize ? filter_xss($description) : $description;
|
105
|
break;
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
|
110
|
return $replacements;
|
111
|
}
|