Hello, I've been searching through all the issues and I've been unable to find a solution for this.

I've set up an exposed filter for taxonomy terms using the Hierarchy Select module. When the user applies the filter, the results of the view are set to be grouped by Taxonomy: Term. The problem is that the grouping header only displays the parent term, whereas I need the parent and all children displayed in the grouping header.

For example, if a user selects:

Parent Term
-Child Term
--Grandchild Term

Then I'd like the grouping header to display:

Parent Term - Child Term - Grandchild Term

I've tried grouping by Taxonomy: All Terms, but that actually changes the results of the view and I end up with duplicate nodes or the wrong taxonomy combination. Is there a simple way to do this that I'm missing?

Comments

merlinofchaos’s picture

Status: Active » Fixed

Views does not support ordering by taxonomy terms hierarchically because there is no easy way to sort them in that order. There used to be a module called 'Taxonomy lineage' that added that. I have no idea if that module is kept current anymore.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sansui’s picture

Just curious if you got anywhere with this gallegos. I'm looking to achieve the same effect using views to display parents, but after reading dozens of threads on the subject and another recent confirmation here, it sounds like this doesn't exist in views because of the sorting problems.

A module like node hierarchy could be used, instead of taxonomy, to build parent/child lists, but it's clumsier than taxonomy IMO, and doesn't give you the nice search/categorization options that you can get with taxonomy.

Here's to hoping that one day being able to group by multiple parent terms will become a reality in views :D

sansui’s picture

I haven't tried out this solution myself yet, but after some extensive googling I came across this blog entry about creating hierarchical listings in views and displays parents as in a directory structure - http://www.group42.ca/hierarchical_listings_in_views_2_replicating_the_l...

sansui’s picture

Following the example in the blog entry, I'm using this in my "unformatted" views tpl for my particular view:



  // Create a node list indexed by taxonomy term id.
  // $rows contains the formatted nodes,
  // $view->result contains the raw field data
  $nodes_by_tid = array();
  foreach($view->result as $index => $node_fields) {
    $nodes_by_tid[$node_fields->term_data_tid][] = $rows[$index];
  }

  // Create a category list indexed by parent. i.e. Parent/child term map
  $subcategories = array();
  $tree = taxonomy_get_tree($view->result[0]->term_data_vid);
  foreach($tree as $taxonomyobject) {
    foreach($taxonomyobject->parents as $parent) {
      $subcategories[$parent][] = $taxonomyobject;
    }
  }

  if ($nodes_by_tid) {
    print _phptemplate_rendertree($subcategories, $nodes_by_tid);
  }


/**
* Recursive function to render nested display tree
*/
function _phptemplate_rendertree($subcategories, $nodes_by_tid, $taxonomy_term = NULL) {
  if ($taxonomy_term) {
    $tid = $taxonomy_term->tid;
    $head = '<div><h2>' . $taxonomy_term->name . '</h2>';
    $tail = '</div>';
  }
  else {
    $tid = 0;
  }

  // Items for this term
  $term_item_output = '';
  if ($nodes_by_tid[$tid]) {
    foreach ($nodes_by_tid[$tid] as $item) {
      $term_item_output .= $item;
    }
  }

  // Subcategories of this term
  $subcategory_output = '';
  if ($subcategories[$tid]) {
    foreach($subcategories[$tid] as $term) {
      $subcategory_output .= _phptemplate_rendertree($subcategories, $nodes_by_tid, $term);
    }
  }

  if ($term_item_output || $subcategory_output) {
    $output = $head . $term_item_output . $subcategory_output . $tail;
  }

  return $output;

}

This works great and is almost exactly what I need - just need one little step further that I can't figure out. I'd like to give the top level parents a different formatting - how can I break it apart so that the very top level of terms gets something like h1 and every other term heading gets h2.

zeezhao’s picture

subscribing

summit’s picture

Bookmarking, greetings, Martijn