22

I need to create something similar like this:

Colors (Vocabulary)

  • Red (term)
    • Dark (term)
      • Node 1
      • Node 2
    • Light (term)
      • Node 3
      • Node 4
  • Blue (term)
    • Node 5
    • Node 6
  • Green (term)
    • Node 7
    • Node 8

(Bullet points are indents.)
Is there a way to do this with Drupal 7 and views 3? If not, is there another way to do this?

2
  • To clarify - as perhaps I misunderstood your question - you want to display a structure like this on one page? Commented Sep 21, 2011 at 15:22
  • Yes on only 1 page
    – iStryker
    Commented Sep 24, 2011 at 23:55

8 Answers 8

15
+100

Thanks to refino, I found a nice solution using the following modules:

Below you find the explanation:

  1. First create a new Taxonomy (let's call it "Folder")
  2. Then, create a new content type (let's call it "Element") with a term reference field to the "Folder" taxonomy
  3. Create a new "Content" View (let's call it "Element View")
  4. Choose Field Display
  5. Add a Contextual Filter to your term reference field
  6. Create a new "Taxonomy" View (let's call it "Folder Tree")
  7. Add 2 fields (term name and term id). The second field can be excluded from display
  8. In Relation: add a "parent term"
  9. Add a third field (term id) and assign it to your parent relation. This field can be excluded from display
  10. Choose Format "Tree (Adjacency model)" and select in the parameters : Ordered list + Main field (your first tid), Parent field (your tid assigned to the parent relation). You now should have a nice tree with all your terms nested
  11. Add a 4th field "Global:View" (this comes with Views Field View) and choose your "Element view", display Master add [tid] in the Arguments textfield

Hope this help

Cheers

Edit by iStryker: This is what I got. The only thing I changed was make Element View a table view, and removed the default (row & strip) classes.

Image of the final product by iStryker

4
  • A great help from this comment. Just a minor note: you might want to rename the fields (Administrative titles) because in the VT settings dropdowns both the "Term ID" fields will be indistinguishable. Commented Dec 29, 2011 at 12:49
  • This answer got me 90% there. I needed to remove default classes on each field; Remove table classes on 'Element View' view; Theme a little
    – iStryker
    Commented Sep 11, 2012 at 19:06
  • I've added a step to reduce duplicates in the Content view. Also, "Arguments" field in Global:View is now labeled "Contextual filters" and the token to use is "[!tid]". Commented Aug 22, 2015 at 13:37
  • Is it possible to do this but with Organic Groups instead of Taxonomy?
    – IamOnStage
    Commented Mar 20, 2017 at 20:24
14
+25

I created three views in Drupal 7.8 and Views 3 + Views Field, nesting one view into another using Include View, and I got such a result (attached). enter image description here

Step-by-step how to do it:

  1. Create vocabulary "Colors" and fill it in with tags.

  2. Create a new view of taxonomy nodes Colors - I call it "Nodes of Colors"

2.1 Format Table , Settings: Grouping field: Taxonomy term: Name

2.2 Advanced Contextual filters: Taxonomy term: Parent term

2.3 Advanced Relationships: Taxonomy term: Content using Color

2.4 Set Fields: Taxonomy term: Term ID, Taxonomy term: Name, (field_color) Content: Title

2.5 Filter criteria: Taxonomy term: Vocabulary (= Colors)

2.6 If you want you can also set "Exclude from display" for Term ID, Taxonomy term: Name

2.7 Set Path:/nodes-of-colors/%

2.8 Set no results behavior: Global: View area (we will come back to this later) 2.9 save

3.Create a new view of taxonomy nodes Colors - I call it "Nodes of Colors 2"

3.1 Format Table , Settings: Grouping field: none

3.2 Advanced Contextual filters: Taxonomy term: Term ID

3.3 Advanced Relationships: Taxonomy term: Content using Color

3.4 Set Fields: Taxonomy term: Term ID, Taxonomy term: Name, (field_color) Content: Title

3.5 Filter criteria: Taxonomy term: Vocabulary (= Colors)

3.6 If you want you can also set "Exclude from display" for Term ID, Taxonomy term: Name

3.7 Set Path:/nodes-of-colors-2/%

3.8 Set no results behavior: Global: none

3.9 Save

Go back and edit "Nodes of Colors" again

4.1 go to Set no results behavior: Global: View area

4.2 Set View to insert: View: nodes_of_colors2 Display: page

4.3 set Inherit contextual filters

4.4 save

5 Go back to Views again (Type Term)

5.1 Create a new view of taxonomy nodes Colors - I call it "Colors Tree"

5.1 Format Table , Settings: Grouping field: none

5.2 Advanced Contextual filters: none

5.3 Advanced Relationships: Taxonomy term: Parent term

5.4 Set Fields: Taxonomy term: Term ID, Taxonomy term: Term ID , Taxonomy term: Name, View: Include View (Include View)

5.5 Filter criteria (1): Taxonomy term: Vocabulary (= Colors)

5.6 Filter criteria (2): (Parent) Taxonomy term: Term

5.7 If you want you can also set "Exclude from display" for Term ID and a label of Include View

5.8 Set Path:/colors-tree

5.9 Set no results behavior: none

5.10 save

...and this should be it, finally.

6
  • can you clarify "5.4 set fields ... view: include view" a bit more. I don't see an option for that anywhere. Thanks
    – uwe
    Commented Sep 22, 2011 at 18:17
  • I am sorry I didn't write I used Views 7.x-3.0-rc1 + Views Field 7.x-1.0-rc1
    – LLub
    Commented Sep 22, 2011 at 21:09
  • ok, thank you. I followed some instructions on their issue queue as well drupal.org/node/1171604
    – uwe
    Commented Sep 23, 2011 at 3:26
  • Wow, this answer has gotten a massive upvote in the pass 7 days. Has anyone tested this though, or you/they just upvoting because @Refineo gave the most detailed answer?
    – iStryker
    Commented Sep 11, 2012 at 18:00
  • I tried this walkthrough again, and didn't work. I hated you all who upvoted this question is the last week without testing
    – iStryker
    Commented Sep 11, 2012 at 18:34
4

I haven't tried the D6 or D7 version of this myself but I thought I would mention it in case it's what you're looking for: Views Tree Module - D7 progress

Edit: There is now a D7 version, checkout the module homepage Views Tree

3

If you want to get the vid from the URL then use the following code.

$vid = $_GET['vid'];
$tid = 0;
$level = 0;
print taxonomy_tree($vid, $tid, $level);
function taxonomy_tree($vid, $tid = 0, $level) {
  $children = taxonomy_get_children($tid, $vid);
  $output = '';
  if ($children) {
    $output .= '<ul>'; 
    foreach ($children as $child) {
      $level++;
      $output .= '<li class="taxonomy-tree-lvl' . $level . ' ">'; 
print $level;
      $output .= l($child->name, 'taxonomy/term/'. $child->tid) . '(' . taxonomy_term_count_nodes($child->tid) . ')'; 
      $output .= taxonomy_tree($vid, $child->tid, $level);
      $output .= '</li>';

      $level--; 
    }
    $output .= '</ul>'; 
  }

  return $output;

}

Or use the following code.

$vocabulary = taxonomy_vocabulary_load(2);//Here pass the vid
print $vocabulary->name;
$vid1 = $_GET['vid'];
if(!$vid1) {
$vid1 = 2;
print 'empty';
}
$vid = 2;
$parent = 0;
print get_child_terms($parent, $vid1);
 function get_child_terms($parent, $vid) {
      $sql = "SELECT td.tid, td.vid, td.name"
         . "  FROM {term_data} td"
         . "  JOIN {term_hierarchy} th on th.tid = td.tid"
         . " WHERE th.parent = %d"
         . "   AND td.vid = %d"
         . " ORDER BY td.weight, td.name";
    $terms = db_query($sql, $parent, $vid);
    $output = "";
    while ($aterm = db_fetch_object($terms)) {
        $output .= "<li>";
        $output .= l("$aterm->name", "taxonomy/term/$aterm->tid/all") . " ("
                .  taxonomy_term_count_nodes($aterm->tid) . ")</li>\n"
                .  get_child_terms($aterm->tid, $vid);
    }
    return ($output != "") ? "<ul style='list-style-type: none;
  margin: 0px 0px 0px 20px;
  padding: 0px;'>\n". $output ."</ul>\n" : "";
  }
2

Of course there is:

  1. Set up your hierarchival vocabulary,
  2. Add it to any selected content type,
  3. Create a view listing nodes from that content type, with contextual filter:

    • either "Content: Has taxonomy term ID" - if you want to list only nodes assigned to specific term,
    • or "Content: Has taxonomy term ID (with depth)" - if you want to list nodes assigned to a specific term as well as to its child terms.

      In this case, in filter configuration you need to set the depth - how many levels below (or above) you want this filter to work.

Then, assuming your view's URL is http://mysite.com/myview and 'Red' tid = 1, if you go to http://mysite.com/myview/1 it will list you all nodes assigned to either one of terms 'red', 'dark' and/or 'light'.

2
  • I'm not looking for an answer for a specific term. I'm looking for a view for the entire vocabulary
    – iStryker
    Commented Sep 11, 2012 at 18:38
  • And that's exactly what I've described. Commented Sep 14, 2012 at 13:55
2

You could use the Taxonomy menu module. Here is a quote about it (from the module's project page):

Transforms your taxonomy vocabularies into menus with ease!

Features:

  • Custom Menu per vocabulary
  • Uses the default taxonomy term path or custom paths
  • Integrate with Views
  • Integrate with Pathauto: use URL/path aliases
  • Does not interfere with other menu modules
1
  • Welcome to Drupal SE! taxonomy_menu is a cool project and it can do this nicely. OP wants to do it in Views though.
    – AKS
    Commented Nov 3, 2012 at 18:18
0

I'm trying to accomplish the same but haven't found much available in D7. Taxonomy Blocks module does it in a block - not views.

0

I believe I have found yet another way to solve this problem, and I think it's simpler than the rest of these solutions. It does not involve any extra modules and you only need one view.

Assuming you have created a two level hierarchical vocabulary (Colors) and a content type with a term reference to that vocab, do the following:

  1. Create a new view and filter by "Taxonomy vocabulary: Machine name (= Colors)"
  2. Add relationships: (1) Taxonomy term: Content using Color and (2) Taxonomy term: Parent term
  3. Add the fields to the View: (1) Title (choose field_color in "Relationship" and require this relationship), (2) Taxonomy term name (do not use relationship and exclude from display) (3) Taxonomy term name (yes, again, this time choose Parent for relationship and exclude from display).
  4. Now group it by clicking on Settings of Format: (1) grouping 1: (Parent) Taxonomy term: Name, (2) Taxonomy term: Name

Done. If you have more than two levels just add another Relationship of Parent using Parent as the relationship's relationship.

EDIT: if you don't require the relationship you'll end up with dupes. Parents will be listed twice if they have child terms that are referenced by nodes. So this limits the answer a bit in that top-level terms without nodes that reference it won't display. Also, because sorting is probably important, having any content referencing a top level term would potentially cause problems for you depending on display needs.

1
  • There is no filter "Taxonomy vocabulary: Machine name (= Colors)" until add relationship. I suspect first step is to be second. Also I could not understand how to add field Title when choosing field_color in "Relationship".
    – Dimetry
    Commented Nov 24, 2017 at 8:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.