Fix for token

  1. <?php
  2.  
  3. /**
  4.  * Implementation of hook_token_list().
  5.  */
  6. function token_vocab_token_list($type = 'all'){
  7.   $tokens = array();
  8.   if ($type == 'node' || $type == 'all' && module_exists('taxonomy')) {
  9.     $tokens['node']['jens-test'] = t("Jen's Test Token");
  10.     $vocabs = taxonomy_get_vocabularies(NULL);
  11.     foreach ($vocabs as $vid => $vocab){
  12.       $name = _token_vocab_clean_name($vocab->name);
  13.       $tokens['node']['vocab-'.$name.'-term'] =
  14.         t("Top term name in vocabulary: ".$name);
  15.       $tokens['node']['vocab-'.$name.'-term-raw'] =
  16.         t("Unfiltered name of top term in vocabulary: ".$name.". WARNING - raw user input.");
  17.       $tokens['node']['vocab-'.$name.'-term-id'] =
  18.         t("ID of top term in vocabulary: ".$name);
  19.     } // foreach
  20.   } // if type node or all and taxonomy installed
  21.   return $tokens;
  22. }
  23.  
  24. /**
  25.  * Implementation of hook_token_values().
  26.  */
  27. function token_vocab_token_values($type, $object = NULL){
  28.   $values = array();
  29.   $node = $object;
  30.   if (($type == 'node' || $type == 'all')
  31.     && module_exists('taxonomy') && !empty($node->taxonomy) && is_array($node->taxonomy)) {
  32.      
  33.       $values['jens-test'] = 'test';
  34.    
  35.     // iterate over vocabs
  36.     $vocabs = taxonomy_get_vocabularies(NULL);
  37.     foreach ($vocabs as $vid => $vocab){
  38.       $name = _token_vocab_clean_name($vocab->name);
  39.      
  40.       // pull top term for this vocab
  41.       $query = db_query("SELECT td.tid, td.name FROM {term_data} td INNER JOIN {term_node} tn ON tn.tid = td.tid WHERE td.vid = %d AND tn.nid = %d ORDER BY weight LIMIT 1", $vid, $node->nid);
  42.      
  43.       if ($result = db_fetch_array($query){
  44.         // set token values
  45.         $values['vocab-'.$name.'-term'] = check_plain(_token_vocab_clean_name($result['name']));
  46.         $values['vocab-'.$name.'-term-raw'] = _token_vocab_clean_name($result['name']);
  47.         $values['vocab-'.$name.'-term-id'] = $result['tid'];
  48.       }
  49.       else {
  50.         // set default empty strings for all vocabs
  51.         $values['vocab-'.$name.'-term'] = '';
  52.         $values['vocab-'.$name.'-term-raw'] = '';
  53.         $values['vocab-'.$name.'-term-id'] = '';
  54.       }
  55.      
  56.     } // foreach    
  57.    
  58.     $data = array();
  59.     foreach ($result as $term) {
  60.       $name = _token_vocab_clean_name($vocab->name);
  61.       // build an array of [vocab-name] => [top-term-tid, top-term-name, top-term-name-raw] values here
  62.       print_r($term);exit;
  63.       $data['']
  64.      
  65.     } // foreach term on node
  66.    
  67.     foreach ($data as $name => $term){
  68.       // set the token values
  69.       $values['vocab-'.$name.'-term'] = $term['name'];
  70.       $values['vocab-'.$name.'-term-raw'] = $term['name-raw'];
  71.       $values['vocab-'.$name.'-term-id'] = $term['tid'];
  72.     } // foreach vocab-term
  73.  
  74.   } // if node has vocab
  75.   return $values;
  76. }
  77.  
  78. function _token_vocab_clean_name($name){
  79.   $hyphen = str_replace(' ', '-', $name);
  80.   $clean = strtolower($hyphen);
  81.   return $clean;
  82. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.