Views2 is powerful, and kind of confusing. I have a taxonomy vocab ("Colors") and I want to create a page that displays the following:

Colors (Vocabulary)
- Red (term)
- - Node 1
- - Node 2
- Blue (term)
- - Node 3
- - Node 4
- Green (term)
- - Node 5
- - Node 6

How can I do this? I've played around with the override-taxonomy view that comes pre-installed, but I don't see anywhere where it will display all the terms inside a vocab (unless they are nested child terms, which mine aren't).

Help! This must be super easy, but I would love some guidance in getting there.

Thanks!

Comments

xjm’s picture

A node view grouped by a taxonomy term field (click the gear icon doodad next to "style" to make the view grouped) will accomplish this for a flat grouping.

I'm still looking for a solution for nested groupings, where a term is a subgroup under its parent, e.g.:

Places (vocabulary)
- World (term)
- - Node
- - North America (term)
- - - Node
- - - Node
- - - United States (term)
- - - - Node
- - - Mexico (term)
- - - - Node
- - - - Node
- - - Canada (term)
- - - - Node
- - South America (term)
- - - Node
- - - Brazil (term)
- - - - Node
- - - - Node
etc.

I'm also looking for similar functionality outside of taxonomy hierarchies, actually (for example, grouping items first by "Location" and then by "Color" within each Location).
I did this in Views 1 with a phptemplate override that manually sorted my list view into nested groups and subgroups, but my code was inefficient and ugly.

It probably requires creating a custom style template or plugin for the view.

Edit: here's the relevant section of views_plugin_style.inc:

  function options_form(&$form, &$form_state) {
    // Only fields-based views can handle grouping.  Style plugins can also exclude
    // themselves from being groupable by setting their "use grouping" definiton
    // key to FALSE.
    // @TODO: Document "uses grouping" in docs.php when docs.php is written.
    if ($this->uses_fields() && $this->definition['uses grouping']) {
      $options = array('' => t('<None>'));
      foreach ($this->display->handler->get_handlers('field') as $field => $handler) {

        if ($label = $handler->label()) {
          $options[$field] = $label;
        }
        else {
          $options[$field] = $handler->ui_name();
        }
      }

      // If there are no fields, we can't group on them.
      if (count($options) > 1) {
        $form['grouping'] = array(
          '#type' => 'select',
          '#title' => t('Grouping field'),
          '#options' => $options,
          '#default_value' => $this->options['grouping'],
          '#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
        );
      }
    }
  }

So, it looks like grouping based on a single field only is assumed at that level, before it even looks at style templates. I'm not sure how to expand the functionality to allow nested groupings.

Edit: here's the function that does the actual grouping.

  /**
   * Group records as needed for rendering.
   *
   * @param $records
   *   An array of records from the view to group.
   * @param $grouping_field
   *   The field id on which to group.  If empty, the result set will be given
   *   a single group with an empty string as a label.
   * @return
   *   The grouped record set.
   */
 function render_grouping($records, $grouping_field = '') {
    $sets = array();
    if ($grouping_field) {
      foreach ($records as $row) {
        $grouping = '';
        // Group on the rendered version of the field, not the raw.  That way,
        // we can control any special formatting of the grouping field through
        // the admin or theme layer or anywhere else we'd like.
        if (isset($this->view->field[$grouping_field])) {
          $grouping = $this->view->field[$grouping_field]->theme($row);
          if ($this->view->field[$grouping_field]->options['label']) {
            $grouping = $this->view->field[$grouping_field]->options['label'] . ': ' . $grouping;
          }
        }
        $sets[$grouping][] = $row;
      }
    }
    else {
      // Create a single group with an empty grouping field.
      $sets[''] = $records;
    }
    return $sets;
  }

petarb’s picture

After mucking around trying to do a very similar thing myself, I've come to realise this is not something the present incarnation of Views is set up to do very well. I've read that Views for 7 might have some sort of functionality along these lines, but it doesn't seem to do what I needed... yet. Instead there are some alternative which might need some tweaking, or work out of the box for you. They are the following modules:

Taxonomy Menu

Vocabulary Directory

Directory

What you really want is "nested tree" display of your taxonomy. The 'nested tree' style is supported by "Taxonomy Lineage" module PLUS "View Bonus" module (they need to work together), although currently this seems to be broken. After mucking around quite a bit with both modules, I could not get them to work, and as Lineage seems to not be working at this point in time, it's probably not worth wasting your effort. I would love to be proved wrong though!

There was a nice module called Node Browser which did almost exactly what you want. It's been withdrawn due to a security issue though. I wish they'd resurrect it though, I can see a lot of people would love this functionality. It seem so simple, such a good idea....

xjm’s picture

...Although I never got the grouping in the Views Bonus module to work quite; it had some bug in my case. Lineage in D6 needs some work, too. Unfortunately more generic taxonomy browser-type stuff doesn't have the advanced filtering, theming, and argument handling I need; I really need a view.

I think the trick is to write a style handler plugin thingy for views... I just have to figure out how!