diff --git a/taxonomy_title.module b/taxonomy_title.module index 10a5669..7df801c 100644 --- a/taxonomy_title.module +++ b/taxonomy_title.module @@ -264,12 +264,87 @@ function taxonomy_title_token_list($type = 'all') { */ function taxonomy_title_token_values($type, $object = NULL, $options = array()) { $values = array(); - if ($type == 'taxonomy') { - $category = $object; - // Use taxonomy title if it exists, else the category name. - $title = _taxonomy_title_get($category->tid); - $values['term-title'] = ($title) ? check_plain($title) : check_plain($category->name); - $values['term-title-raw'] = ($title) ? $title : $category->name; + switch ($type) { + case 'node': + $node = $object; + // This code is copied from the token module which i adapting + // pathauto's handling code; it's intended for compatability with it. + if (!empty($node->taxonomy) && is_array($node->taxonomy)) { + foreach ($node->taxonomy as $term) { + $original_term = $term; + if ((object)$term) { + // With freetagging it's somewhat hard to get the tid, vid, name values + // Rather than duplicating taxonomy.module code here you should make sure your calling module + // has a weight of at least 1 which will run after taxonomy has saved the data which allows us to + // pull it out of the db here. + if (!isset($term->name) || !isset($term->tid)) { + $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, t.name", $object->nid, 0, 1)); + if (!$vid) { + continue; + } + $term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1)); + $term->vid = $vid; + } + + // Ok, if we still don't have a term name maybe this is a pre-taxonomy submit node + // So if it's a number we can get data from it + if (!isset($term->name) && is_array($original_term)) { + $tid = array_shift($original_term); + if (is_numeric($tid)) { + $term = taxonomy_get_term($tid); + } + } + $vid = $term->vid; + // i18ntaxonomy_vocabulary($vid) = 1 for vocabolaries that has translation enabled + // we only want to translate terms when for nodes that has a language selected as we + // wont really will be able to tell which language will be used for the token. Since + // it will depend on the active language of the user creating the alias and not a + // chosen language for the node. + + // convert to title + $title = _taxonomy_title_get($term->tid); + + if (function_exists('i18nstrings') && i18ntaxonomy_vocabulary($vid) == 1 && $node->language) { + // if node is langiage neutral, language is set to NULL + $name = i18nstrings("taxonomy:term:$term->tid:name", $term->name, $node->language); + + $values['term-title'] = ($title) ? check_plain($title) : check_plain($name); + $values['term-title-raw'] = ($title) ? $title : $name; + } + else { + $name = $term->name; + + $values['term-title'] = ($title) ? check_plain($title) : check_plain($name); + $values['term-title-raw'] = ($title) ? $title : $name; + } + break; + } + } + } + // It's possible to leave that block and still not have good data. + // So, we test for these and if not set, set them. + if (!isset($values['term-title'])) { + $values['term-title'] = ''; + $values['term-title-raw'] = ''; + } + break; + case 'taxonomy': + if (!empty($object)) { + $category = $object; + // Use taxonomy title if it exists, else the category name. + $title = _taxonomy_title_get($category->tid); + + // Translate the category name too. + if (function_exists('i18nstrings')) { + $name = i18nstrings("taxonomy:term:$category->tid:name", $category->name); + } + else { + $name = $category->name; + } + $values['term-title'] = ($title) ? check_plain($title) : check_plain($name); + $values['term-title-raw'] = ($title) ? $title : $name; + } + break; } return $values; }