function hs_taxonomy_token_values in Hierarchical Select 5.3
Same name and namespace in other branches
- 6.3 modules/hs_taxonomy.module \hs_taxonomy_token_values()
Implementation of hook_token_values().
File
- modules/
hs_taxonomy.module, line 402 - Implementation of the Hierarchical Select API for the Taxonomy module.
Code
function hs_taxonomy_token_values($type, $object = NULL, $options = array()) {
static $hs_vids;
static $all_vids;
$separator = variable_get('hs_taxonomy_separator', variable_get('pathauto_separator', '-'));
$values = array();
switch ($type) {
case 'node':
$node = $object;
// Default values.
$values['save-lineage-termpath'] = $values['save-lineage-termpath-raw'] = FALSE;
// If $node->taxonomy doesn't exist, these tokens cannot be created!
if (!is_object($node) || !isset($node->taxonomy) || !is_array($node->taxonomy)) {
return $values;
}
// Find out which vocabularies are using Hierarchical Select.
if (!isset($hs_vids)) {
$hs_vids = array();
$result = db_query("SELECT SUBSTRING(name, 30, 3) AS vid FROM {variable} WHERE name LIKE 'taxonomy_hierarchical_select_%' AND value LIKE 'i:1\\;';");
while ($o = db_fetch_object($result)) {
$hs_vids[] = $o->vid;
}
}
// Get a list of all existent vids, so we can generate an empty token
// when a token is requested for a vocabulary that's not associated with
// the current content type.
if (!isset($all_vids)) {
$all_vids = array();
$result = db_query("SELECT vid FROM {vocabulary}");
while ($row = db_fetch_object($result)) {
$all_vids[] = $row->vid;
}
}
// Generate the per-vid "save-lineage-termpath" tokens.
foreach ($all_vids as $vid) {
$terms = array();
if (in_array($vid, $hs_vids) && isset($node->taxonomy[$vid])) {
$selection = $node->taxonomy[$vid];
$terms = _hs_taxonomy_token_termpath_for_vid($selection, $vid);
}
$values["save-lineage-termpath:{$vid}"] = implode($separator, array_map('check_plain', $terms));
$values["save-lineage-termpath-raw:{$vid}"] = implode($separator, $terms);
}
// We use the terms of the first vocabulary that uses Hierarchical
// Select for the default "save-lineage-termpath" tokens.
$vids = array_intersect(array_keys($node->taxonomy), $hs_vids);
if (!empty($vids)) {
$vid = $vids[0];
$values['save-lineage-termpath'] = implode($separator, array_map('check_plain', $terms));
$values['save-lineage-termpath-raw'] = implode($separator, $terms);
}
break;
}
return $values;
}