--- views/modules/taxonomy/views_handler_field_term_node_tid.inc	2009-07-02 01:07:14.000000000 +0200
+++ views2/modules/taxonomy/views_handler_field_term_node_tid.inc	2010-04-12 18:08:21.731647473 +0200
@@ -21,6 +21,8 @@ class views_handler_field_term_node_tid 
     $options['link_to_taxonomy'] = array('default' => TRUE);
     $options['limit'] = array('default' => FALSE);
     $options['vids'] = array('default' => array());
+    $options['limit_depth'] = array('default' => FALSE);
+    $options['limit_depth_value'] = array('default' => '');
 
     return $options;
   }
@@ -30,6 +32,7 @@ class views_handler_field_term_node_tid 
    */
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
+
     $form['link_to_taxonomy'] = array(
       '#title' => t('Link this field to its term page'),
       '#type' => 'checkbox',
@@ -58,6 +61,23 @@ class views_handler_field_term_node_tid 
       '#process' => array('expand_checkboxes', 'views_process_dependency'),
       '#dependency' => array('edit-options-limit' => array(TRUE)),
     );
+
+    $form['limit_depth'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Limit terms by depth'),
+      '#default_value'=> $this->options['limit_depth'],
+    );
+
+    $form['limit_depth_value'] = array(
+      '#prefix' => '<div><div id="edit-options-limit-depth-value">',
+      '#suffix' => '</div></div>',
+      '#type' => 'select',
+      '#title' => t('Max depth of term'),
+      '#options' => array(1, 2, 3, 4, 5, 6, 7, 8 , 9),
+      '#default_value' => $this->options['limit_depth_value'],
+      '#process' => array('views_process_dependency'),
+      '#dependency' => array('edit-options-limit-depth' => array(TRUE)),
+    );
   }
 
   /**
@@ -82,9 +102,49 @@ class views_handler_field_term_node_tid 
         $voc = " AND td.vid IN (" . implode(', ', array_keys(array_filter($this->options['vids']))) . ")";
       }
 
-      $result = db_query("SELECT tn.vid AS node_vid, td.*, v.name as vocabulary FROM {term_data} td INNER JOIN {term_node} tn ON td.tid = tn.tid INNER JOIN {vocabulary} v ON v.vid = td.vid WHERE tn.vid IN (" . implode(', ', $vids) . ")$voc ORDER BY td.weight, td.name");
+      $result = db_query("SELECT tn.vid AS node_vid, td.*, th.parent, v.name as vocabulary FROM {term_data} td INNER JOIN {term_node} tn ON td.tid = tn.tid INNER JOIN {vocabulary} v ON v.vid = td.vid INNER JOIN {term_hierarchy} th ON th.tid = tn.tid WHERE tn.vid IN (" . implode(', ', $vids) . ")$voc ORDER BY td.weight, td.name");
 
+      // Extract data
       while ($term = db_fetch_object($result)) {
+        $temp_term[$term->node_vid][$term->parent] = $term;
+      }
+
+      if($this->options['limit_depth']) {
+        // Calculate depth
+        foreach($temp_term as $node_vid => $terms) {
+          $parent = 0;
+          for($depth = 0; $depth < count($terms); $depth++) {
+            $temp_term[$node_vid][$parent]->depth = $depth;
+            $tmp_parent = $temp_term[$node_vid][$parent]->tid;
+
+            // Exclude term over depth
+            if($depth > $this->options['limit_depth_value']) {
+              unset($temp_term[$node_vid][$parent]);
+            }
+            $parent = $tmp_parent;
+          }
+        }
+
+        // Order term on depth
+        foreach($temp_term as $node_vid => $terms) {
+          foreach($terms as $parent_tid => $term) {
+            $temp_term2[$node_vid][$term->depth] = $term;
+          }
+          ksort($temp_term2[$node_vid]);
+        }
+
+        $temp_term = $temp_term2;
+      }
+
+      $temp_term3 = array();
+      // Reset array structure
+      foreach($temp_term as $node_vid => $terms) {
+        foreach($terms as $parent_tid => $term) {
+          $temp_term3[] = $term;
+        }
+      }
+
+      foreach ($temp_term3 as $term) {
         $this->items[$term->node_vid][$term->tid]['name'] = check_plain($term->name);
         $this->items[$term->node_vid][$term->tid]['tid'] = $term->tid;
         $this->items[$term->node_vid][$term->tid]['vid'] = $term->vid;
