Code for creating path alias token
<?php
/**
* Implementation of hook_token_list().
*/
function gmapnode_token_list(
$type = 'all') {
if($type == 'taxonomy' || $type == 'all') {
$tokens['taxonomy']['catpath-raw-not-self'] = t('As [cat-raw], but including its supercategories separated by /. NOT INCLUDING ITSELF! WARNING - raw user input.');
return $tokens;
}
elseif($type == 'node') {
$tokens['node']['termfirst-leaf-catpath-raw'] = t('Unfiltered path of the first leaf term. WARNING - raw user input.');
$tokens['node']['termfirst-parent-termname'] = t('Term name of first parent.');
$tokens['node']['termfirst-parent-termalias'] = t('Term alias of first parent.');
return $tokens;
}
}
/**
* Implementation of hook_token_values().
*/
function gmapnode_token_values($type, $object = NULL, $options = array()) {
$values = null;
if($type == 'taxonomy' && isset($object->tid)) {
$parents = taxonomy_get_parents_all($object->tid);
array_shift($parents);
$catpath_raw_not_self = '';
foreach($parents as $parent) {
$catpath_raw_not_self = pathauto_cleanstring(preg_replace('/\//', '', $parent->name)) . '/' . $catpath_raw_not_self;
}
$values['catpath-raw-not-self'] = $catpath_raw_not_self;
}
elseif($type == 'node' && isset($object->nid)) {
$vid = db_result(db_query_range("SELECT t.vid FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight ASC, t.name", $object->nid, 0, 1));
$sql = "SELECT t.tid, t.name FROM {term_data} t
INNER JOIN {term_node} r ON r.tid = t.tid
INNER JOIN {term_hierarchy} th ON t.tid = th.tid
WHERE t.vid = %d AND r.nid = %d AND th.parent <> 0
ORDER BY weight";
$category = db_fetch_object(db_query_range($sql, $vid, $object->nid, 0, 1));
$category->vid = $vid;
$label = 'term';
if(isset($category->tid)) {
$parents = taxonomy_get_parents_all($category->tid);
array_shift($parents);
$parent = $parents[0];
$values[$label . 'first-leaf-catpath-raw'] = drupal_get_path_alias('taxonomy/term/' . $category->tid);
$values[$label . 'first-parent-termname'] = $parent->name;
$values[$label . 'first-parent-termalias'] = drupal_get_path_alias('taxonomy/term/' . $parent->tid);
}
else { // Provide some defaults if they aren't set.
$values[$label . 'first-leaf-catpath-raw'] = '';
}
}
return $values;
}
?>
Knowledge keywords: