| 1 |
<?php
|
| 2 |
// $Id: taxonomy.tokens.inc,v 1.2 2009/09/18 00:04:23 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Builds placeholder replacement tokens for taxonomy terms and vocabularies.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_token_info().
|
| 11 |
*/
|
| 12 |
function taxonomy_token_info() {
|
| 13 |
$types['term'] = array(
|
| 14 |
'name' => t("Taxonomy terms"),
|
| 15 |
'description' => t("Tokens related to taxonomy terms."),
|
| 16 |
'needs-data' => 'term',
|
| 17 |
);
|
| 18 |
$types['vocabulary'] = array(
|
| 19 |
'name' => t("Vocabularies"),
|
| 20 |
'description' => t("Tokens related to taxonomy vocabularies."),
|
| 21 |
'needs-data' => 'vocabulary',
|
| 22 |
);
|
| 23 |
|
| 24 |
// Taxonomy term related variables.
|
| 25 |
$term['tid'] = array(
|
| 26 |
'name' => t("Term ID"),
|
| 27 |
'description' => t("The unique ID of the taxonomy term."),
|
| 28 |
);
|
| 29 |
$term['vid'] = array(
|
| 30 |
'name' => t("Vocabulary ID"),
|
| 31 |
'description' => t("The unique ID of the vocabulary the term belongs to."),
|
| 32 |
);
|
| 33 |
$term['name'] = array(
|
| 34 |
'name' => t("Name"),
|
| 35 |
'description' => t("The name of the taxonomy term."),
|
| 36 |
);
|
| 37 |
$term['description'] = array(
|
| 38 |
'name' => t("Description"),
|
| 39 |
'description' => t("The optional description of the taxonomy term."),
|
| 40 |
);
|
| 41 |
$term['node-count'] = array(
|
| 42 |
'name' => t("Node count"),
|
| 43 |
'description' => t("The number of nodes tagged with the taxonomy term."),
|
| 44 |
);
|
| 45 |
$term['url'] = array(
|
| 46 |
'name' => t("URL"),
|
| 47 |
'description' => t("The URL of the taxonomy term."),
|
| 48 |
);
|
| 49 |
|
| 50 |
// Taxonomy vocabulary related variables.
|
| 51 |
$vocabulary['vid'] = array(
|
| 52 |
'name' => t("Vocabulary ID"),
|
| 53 |
'description' => t("The unique ID of the taxonomy vocabulary."),
|
| 54 |
);
|
| 55 |
$vocabulary['name'] = array(
|
| 56 |
'name' => t("Name"),
|
| 57 |
'description' => t("The name of the taxonomy vocabulary."),
|
| 58 |
);
|
| 59 |
$vocabulary['description'] = array(
|
| 60 |
'name' => t("Description"),
|
| 61 |
'description' => t("The optional description of the taxonomy vocabulary."),
|
| 62 |
);
|
| 63 |
$vocabulary['node-count'] = array(
|
| 64 |
'name' => t("Node count"),
|
| 65 |
'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
|
| 66 |
);
|
| 67 |
$vocabulary['term-count'] = array(
|
| 68 |
'name' => t("Node count"),
|
| 69 |
'description' => t("The number of terms belonging to the taxonomy vocabulary."),
|
| 70 |
);
|
| 71 |
|
| 72 |
// Chained tokens for taxonomies
|
| 73 |
$term['vocabulary'] = array(
|
| 74 |
'name' => t("Vocabulary"),
|
| 75 |
'description' => t("The vocabulary the taxonomy term belongs to."),
|
| 76 |
'type' => 'vocabulary',
|
| 77 |
);
|
| 78 |
$term['parent'] = array(
|
| 79 |
'name' => t("Parent term"),
|
| 80 |
'description' => t("The parent term of the taxonomy term, if one exists."),
|
| 81 |
'type' => 'term',
|
| 82 |
);
|
| 83 |
|
| 84 |
return array(
|
| 85 |
'types' => $types,
|
| 86 |
'tokens' => array(
|
| 87 |
'term' => $term,
|
| 88 |
'vocabulary' => $vocabulary,
|
| 89 |
),
|
| 90 |
);
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Implement hook_tokens().
|
| 95 |
*/
|
| 96 |
function taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
| 97 |
$replacements = array();
|
| 98 |
$sanitize = !empty($options['sanitize']);
|
| 99 |
|
| 100 |
if ($type == 'term' && !empty($data['term'])) {
|
| 101 |
$term = $data['term'];
|
| 102 |
|
| 103 |
foreach ($tokens as $name => $original) {
|
| 104 |
switch ($name) {
|
| 105 |
case 'tid':
|
| 106 |
$replacements[$original] = $term->tid;
|
| 107 |
break;
|
| 108 |
|
| 109 |
case 'vid':
|
| 110 |
$replacements[$original] = $term->vid;
|
| 111 |
break;
|
| 112 |
|
| 113 |
case 'name':
|
| 114 |
$replacements[$original] = $sanitize ? check_plain($term->name) : $term->name;
|
| 115 |
break;
|
| 116 |
|
| 117 |
case 'description':
|
| 118 |
$replacements[$original] = $sanitize ? filter_xss($term->description) : $term->description;
|
| 119 |
break;
|
| 120 |
|
| 121 |
case 'url':
|
| 122 |
$replacements[$original] = url('taxonomy/term/' . $term, array('absolute' => TRUE));
|
| 123 |
break;
|
| 124 |
|
| 125 |
case 'node-count':
|
| 126 |
$sql = "SELECT COUNT (1) FROM {taxonomy_term_node} tn WHERE tn.tid = :tid";
|
| 127 |
$count = db_query($sql, array(':tid' => $term->tid))->fetchField();
|
| 128 |
$replacements[$original] = $count;
|
| 129 |
break;
|
| 130 |
|
| 131 |
case 'vocabulary':
|
| 132 |
$vocabulary = taxonomy_vocabulary_load($term->vid);
|
| 133 |
$replacements[$original] = check_plain($vocabulary->name);
|
| 134 |
break;
|
| 135 |
|
| 136 |
case 'parent':
|
| 137 |
$parents = taxonomy_get_parents($term->tid);
|
| 138 |
$parent = array_pop($parents);
|
| 139 |
$replacements[$original] = check_plain($parent->name);
|
| 140 |
break;
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
if ($vocabulary_tokens = token_find_with_prefix($tokens, 'vocabulary')) {
|
| 145 |
$vocabulary = taxonomy_vocabulary_load($term->vid);
|
| 146 |
$replacements += token_generate('vocabulary', $vocabulary_tokens, array('vocabulary' => $vocabulary), $options);
|
| 147 |
}
|
| 148 |
|
| 149 |
if ($vocabulary_tokens = token_find_with_prefix($tokens, 'parent')) {
|
| 150 |
$parents = taxonomy_get_parents($term->tid);
|
| 151 |
$parent = array_pop($parents);
|
| 152 |
$replacements += token_generate('term', $vocabulary_tokens, array('term' => $parent), $options);
|
| 153 |
}
|
| 154 |
}
|
| 155 |
|
| 156 |
elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
|
| 157 |
$vocabulary = $data['vocabulary'];
|
| 158 |
|
| 159 |
foreach ($tokens as $name => $original) {
|
| 160 |
switch ($name) {
|
| 161 |
case 'vid':
|
| 162 |
$replacements[$original] = $vocabulary->vid;
|
| 163 |
break;
|
| 164 |
|
| 165 |
case 'name':
|
| 166 |
$replacements[$original] = $sanitize ? check_plain($vocabulary->name) : $vocabulary->name;
|
| 167 |
break;
|
| 168 |
|
| 169 |
case 'description':
|
| 170 |
$replacements[$original] = $sanitize ? filter_xss($vocabulary->description) : $vocabulary->description;
|
| 171 |
break;
|
| 172 |
|
| 173 |
case 'term-count':
|
| 174 |
$sql = "SELECT COUNT (1) FROM {taxonomy_term_data} td WHERE td.vid = :vid";
|
| 175 |
$count = db_query($sql, array(':vid' => $vocabulary->vid))->fetchField();
|
| 176 |
$replacements[$original] = $count;
|
| 177 |
break;
|
| 178 |
|
| 179 |
case 'node-count':
|
| 180 |
$sql = "SELECT COUNT (1) FROM {taxonomy_term_node} tn LEFT JOIN {taxonomy_term_data} td ON tn.tid = td.tid WHERE td.vid = :vid";
|
| 181 |
$count = db_query($sql, array(':vid' => $vocabulary->vid))->fetchField();
|
| 182 |
$replacements[$original] = $count;
|
| 183 |
break;
|
| 184 |
}
|
| 185 |
}
|
| 186 |
}
|
| 187 |
|
| 188 |
return $replacements;
|
| 189 |
}
|