开发者

Drupal show all terms of different vocabulary

I'm trying to show every terms of 2 vocabularies from a node, but I have this situation.

I have 2 vocabularies, somethings like "brands" and "cars".

  • Brands
    • Ford
    • BMW
    • Mercedes
  • Cars
    • Blue
    • Light Blue
    • Red
    • Green

My node has to be even in BMW and even in "Light Blue."

In my template.tpl i have this function:

function theme479_print_terms($node, $vid = NULL, $ordered_list = TRUE) {
     $vocabularies = taxonomy_get_vocabularies();
     if ($ordered_list) $output .= '<ul>'; //checks to see if you want an ordered list
     if ($vid) { //checks to see if you've passed a number with vid, prints just that vid
        $output = '<div class="tags-'. $vid . '">';
        foreach($vocabularies as $vocabulary) {
         if ($vocabulary->vid == $vid) {
           $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vi开发者_运维百科d);
           if ($terms) {
             $links = array();
             $output .= '<span class="only-vocabulary-'. $vocabulary->vid . '">';
             if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': ';
             foreach ($terms as $term) {
               $links[] = '<span class="term-' . $term->tid . '">' . l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';
             }
             $output .= implode(', ', $links);
             if ($ordered_list) $output .= '</li>';
             $output .= '</span>';
           }
         }
       }
     }
     else {
       $output = '<div class="tags">';
       foreach($vocabularies as $vocabulary) {
         if ($vocabularies) {
           $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
           if ($terms) {
             $links = array();
             $output .= '<ul class="vocabulary-'. $vocabulary->vid . '">';
             if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': ';
             foreach ($terms as $term) {
               $links[] = '<span class="term-' . $term->tid . '">' . l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';
             }
             $output .= implode(', ', $links);
             if ($ordered_list) $output .= '</li>';
             $output .= '</ul>';
           }
         }
       }
     }
     if ($ordered_list) $output .= '</ul>';
     $output .= '</div>';
     return $output;
}

In my node.tpl, I have this:

print theme_print_terms($node, $unordered_list = TRUE);

The result is the following:

Brands: BMW

Colors: Light Blue

How can I get something like the following?

Brands: BMW

Colors: Blue/Light Blue

So with every parent categories. May you help me?


EDIT:

it's possible to show all hierarchy of a vocabulary? for example i have a vocabulary with this structure (Foods>Fruits>Fruit_with_seeds ->apple) but this function only show Fruits>Fruit_with_seeds)


You simply need to check each term for parent terms. If a parent term is found place it in a variable and then prepend it to your link text. A way to check for a term parent:

if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) {
  foreach ($get_parents as $parents) {
    $parent = $parents->name . '/';
  }
}

To add to your link text:

$links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';

All put together: (note: This is a pretty nasty looking function, the HTML should really be separated from the retrieval of term parents to format output in another function).

function theme479_print_terms($node, $vid = NULL, $ordered_list = TRUE) {
     $vocabularies = taxonomy_get_vocabularies();
     if ($ordered_list) $output .= '<ul>'; //checks to see if you want an ordered list
     if ($vid) { //checks to see if you've passed a number with vid, prints just that vid
        $output = '<div class="tags-'. $vid . '">';
        foreach($vocabularies as $vocabulary) {
         if ($vocabulary->vid == $vid) {
           $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
           if ($terms) {
             $links = array();
             $output .= '<span class="only-vocabulary-'. $vocabulary->vid . '">';
             if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': ';
             foreach ($terms as $term) {
              // Check for terms parent, if found assign to $parent variable
              if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) {
                foreach ($get_parents as $parents) {
                  $parent = $parents->name . '/';
                }
              }
               $links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';
             }
             $output .= implode(', ', $links);
             if ($ordered_list) $output .= '</li>';
             $output .= '</span>';
           }
         }
       }
     }
     else {
       $output = '<div class="tags">';
       foreach($vocabularies as $vocabulary) {
         if ($vocabularies) {
           $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
           if ($terms) {
             $links = array();
             $output .= '<ul class="vocabulary-'. $vocabulary->vid . '">';
             if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': ';
             foreach ($terms as $term) {
              // Check for terms parent, if found assign to $parent variable
              if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) {
                foreach ($get_parents as $parents) {
                  $parent = $parents->name . '/';
                }
              }
               $links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';
             }
             $output .= implode(', ', $links);
             if ($ordered_list) $output .= '</li>';
             $output .= '</ul>';
           }
         }
       }
     }
     if ($ordered_list) $output .= '</ul>';
     $output .= '</div>';
     return $output;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜