Index: modules/taxonomy/views_handler_field_term_node_tid.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/modules/taxonomy/views_handler_field_term_node_tid.inc,v
retrieving revision 1.4.2.3
diff -u -p -r1.4.2.3 views_handler_field_term_node_tid.inc
--- modules/taxonomy/views_handler_field_term_node_tid.inc	12 Dec 2010 10:18:11 -0000	1.4.2.3
+++ modules/taxonomy/views_handler_field_term_node_tid.inc	3 Jan 2011 10:54:45 -0000
@@ -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' =>  array());
 
     return $options;
   }
@@ -58,6 +60,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' => 'checkboxes',
+      '#title' => t('Display term with depth'),
+      '#options' => array_combine(range(1, 10), range(1, 10)),
+      '#default_value' => $this->options['limit_depth_value'],
+      '#process' => array('expand_checkboxes', 'views_process_dependency'),
+      '#dependency' => array('edit-options-limit-depth' => array(TRUE)),
+    );
   }
 
   /**
@@ -78,14 +97,61 @@ class views_handler_field_term_node_tid 
 
     if ($vids) {
       $voc = '';
-      $voc_ids = array_filter($this->options['vids']);
-      if (!empty($this->options['limit']) && !empty($voc_ids)) {
-        $voc = " AND td.vid IN (" . implode(', ', array_keys($voc_ids)) . ")";
+      if (!empty($this->options['limit']) && !empty($this->options['vids'])) {
+        $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");
+      if ($this->options['limit_depth']) {
+        $thjoin = " INNER JOIN {term_hierarchy} th ON th.tid = tn.tid";
+        $thselect = " th.parent,";
+      } else {
+        $thjoin = "";
+        $thselect = "";
+			}
+      $result = db_query("SELECT tn.vid AS node_vid, td.*,$thselect 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$thjoin WHERE tn.vid IN (" . implode(', ', $vids) . ")$voc ORDER BY td.weight, td.name");
 
+      // Extract data
+      $all_term = array();
       while ($term = db_fetch_object($result)) {
+        $all_term[$term->node_vid][$term->parent] = $term;
+      }
+
+      if ($this->options['limit_depth']) {
+        // Calculate depth
+        foreach ($all_term as $node_vid => $terms) {
+          $parent = 0;
+          for ($depth = 0; $depth < count($terms); $depth++) {
+            $all_term[$node_vid][$parent]->depth = $depth;
+            $tmp_parent = $all_term[$node_vid][$parent]->tid;
+            // Exclude term over depth
+            if ($this->options['limit_depth_value'][($depth + 1)] != ($depth + 1)) {
+              unset($all_term[$node_vid][$parent]);
+            }
+            $parent = $tmp_parent;
+          }
+        }
+
+        // Order term on depth
+        $temp_term2 = array();
+        foreach ($all_term as $node_vid => $terms) {
+          foreach ($terms as $parent_tid => $term) {
+            $temp_term2[$node_vid][$term->depth] = $term;
+          }
+          ksort($temp_term2[$node_vid]);
+        }
+
+        $all_term = $temp_term2;
+      }
+
+      // Reset array structure
+      $temp_term3 = array();
+      foreach ($all_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;
