View with taxonomy arguments, presented all on the same page: Terms as headlines, displayed hierarchically

Last modified: October 19, 2008 - 17:19

Abstract

The approach described here is intended to create an overview of the hierarchy of a taxonomy & showing the related nodes. The layout of the nodes is defined by the associated view (see below). This could be useful e.g. for a list of all downloads of a page.

Example

Term 1 with Depth 0

  • node one of term 1, layout depending on view-settings
  • node two of term 1, layout depending on view-settings
  • etc., number depending on view-settings

Term 2 with Depth 0

  Term 3 with Depth 1, child of Term 2
    • nodes of term 3, layout depending on view-settings

Term 4 with Depth 0

  • nodes of term 4, layout depending on view-settings
  Term 5 with Depth 1, child of Term 4
    • nodes of term 5, layout depending on view-settings

Example: http://www.3dcenter.org/downloads

Steps to be taken

1. The Vocabulary

Create a new (optional: hierarchically) taxonomy or use an existing taxonomy. Remember its ID. It will be inserted in the PHP snippet shown in step 3.

2. The View

Create a new view for the nodes that use the vocabulary of step 1. As Argument Type for this view, set Taxonomy: Term Name. Remember the views name. It will be inserted in the PHP snippet shown below.

3. The php Snippet

This snippet is for Drupal 5 and views 1. For Drupal 6 and views 2 see comment of stone_d below.
Create new content or use existing content with PHP as input format. Then paste the following code and enter your values of step 1 & 2 in lines 2 & 5:

<?php
/* Enter the ID of the vocabulary which terms you want to show as headlines */
$vocabulary_id = "0";

/* Enter the name of the view that should show the nodes */
$view_name = "YOUR_VIEW";


/* Only edit if you know what you're doing */
$view = views_get_view($view_name);
if (!
$view) {
   
drupal_not_found();
    exit;
}
foreach(
taxonomy_get_tree($vocabulary_id,0,-1,1) as $value) {
  print
t("<div style='margin-bottom: 4em;'><h3>" . $value->name . "</h3>");
  print
views_build_view('embed', $view, array($value->name), FALSE, $view->nodes_per_block);
  if (
taxonomy_get_children($value->tid) ) {
     print
t("<div style='margin-left: 2em;'>");
     foreach(
taxonomy_get_children($value->tid) as $child ) {
        print
t("<h4 style='margin-top:2em;'>" . $child->name . "</h4>");
        print
views_build_view('embed', $view, array($child->name), FALSE, $view->nodes_per_block);
      }
      print
t("</div>");
  }
  print
t("</div>");
}
?>

Known issues

  • Currently the code only support terms of the first two depths (the modifications to support unlimited depths shouldn't be hard to do)
  • Hard coded CSS styles: I suggest to exchange them with your CSS classes.

View showing taxonomy terms with related nodes

mpeal - April 11, 2008 - 20:02

This snippet does exactly what I need, a list of events at each of several facilities.
I needed to make one important change.
As given, it displays the whole content of an event node. But the view associated with my block wants only a few fields to show.
In the line that starts print views_build_view('embed' I changes 'embed' to 'block'
The parameters for views_build_view are explained here: http://drupal.org/node/99721

A solution for Drupal 6 and Views2

stone_d - October 3, 2008 - 10:04

thank your for that snippet, u saved my day *hehe
I just trimmed it to drupal 6 with views2 and selected "formatted layout with rows" for my view - works perfect!

<?php
           
/* Enter the ID of the vocabulary which terms you want to show as headlines */
           
$vocabulary_id = "1";
           
           
/*Enter the name of the view that should show the nodes */
           
$view_name = "NAME_OF_YOUR_VIEW";

            foreach(
taxonomy_get_tree($vocabulary_id,0,-1,1) as $value) {
              print
t("<div><h3>" . $value->name . "</h3>");
              print
views_embed_view($view_name, 'default', $value->name);
              if (
taxonomy_get_children($value->tid) ) {
                 print
t("<div>");
                 foreach(
taxonomy_get_children($value->tid) as $child ) {
                    print
t("<h4 style='margin-top:2em;'>" . $child->name . "</h4>");
                    print
views_embed_view($view_name, 'default', $child->name);
                  }
                  print
t("</div>");
              }
              print
t("</div>");
            }   
?>

 
 

Drupal is a registered trademark of Dries Buytaert.