Is there anyway to group a view by two fields, instead of just one?
I'm trying to create a digital art gallery. The artist of each painting is a taxonomy term. Each artist has their own photo using the Taxonomy Image module. Using the gridview, it's currently grouped by "Taxonomy: All Terms" and currently displays the Artist name with all their paintings, but I can't figure out how to display the Taxonomy Image next to the term. I can choose to group the view by the Taxonomy Term Image, but then it doesn't say the artist name. If I had the Taxonomy Term Image as a field, it displays with every painting, which I don't want.
I was wondering if there's anyway to display them both (only once) with the paintings listed underneath?

Comments

joachim’s picture

This is sort of the same as #673716: no raw data about grouped field in the main template.

I had the same problem -- I wanted more than one piece of data common to the whole group.

I solved this with a fair amount of work in the views-view-unformatted.tpl.php template and preprocessor.

Here's what I did:
- group on Taxonomy term so there is a $tid value accessible
- add fields that I want group-wide, but set them to exclude from display in the field options
- did some work in the template using the preprocessor:

function MYTHEME_preprocess_views_view_unformatted__MYVIEW(&$variables) {
  // We come here for each group; stash in view to avoid repetition
  if (!isset($variables['view']->grouping_data)) {
    foreach ($variables['view']->result as $raw) {
      // We expect the field Term:name and Term:description to be present in the view.
      $grouping_data[$raw->term_data_tid]['name'] = $raw->term_data_name;
      $grouping_data[$raw->term_data_tid]['description'] = $raw->term_data_description;
    }
    // stash data in the view
    $variables['view']->grouping_data = $grouping_data;
  }
  $grouping_data = $variables['view']->grouping_data;
  
  $tid = $variables['title'];
  
  $variables['tid'] = $tid;
  
  // Things to print in the template file views-view-unformatted.tpl.php
  $variables['term_name'] = check_plain($grouping_data[$tid]['name']);
  $variables['term_description'] = check_plain($grouping_data[$tid]['description']);
}
joachim’s picture

Title: Group "Taxonomy: All Terms" and "Taxonomy: Term Image" at the same time » Group by several fields at the same time

More general title so people can find this :)

stongo’s picture

This is fantastic. Thank you!

MBroberg’s picture

I am currently doing this in the view with replacement patterns and field rewrites.
Add all of the fields to your view that you want to display in the grouping and exclude them from display. On the last field in the list (can be any field), use the "rewrite the output" box and include replacement patterns for the fields you want in the group header. You can even use HTML, divs, h2, h3, etc.
Use that rewritten field for the "group by" field. (All other fields must be above the rewritten field or they will not show up).
Anything not excluded will show up in the result list and anything excluded but included in the rewrite will show up in the group headings.

This is working very well on my site.
The dates for my events show up as the actual fields and all data related to the events show up in the headers.

Any format choices you make in the fields will also be applied in the headers, so you can add links on some fields and no links on others and different date formats to show day of week followed by time start and end, etc.

I did have to add a patch to make excluded fields show up in certain contrib views but it worked out of the box for regular views.

stongo’s picture

MBroberg,
I tried to get this working, maybe I'm doing something wrong.
Is the last field ("taxonomy: all terms" - the one being grouped) supposed to be excluded from display as well? When I do this, it still only displays the taxonomy term. It does also output the html I put in the "rewrite output", but not the actual content of the tokens.
Here's what's in the field:

[tid]
<h3>[tid_1]</h3>
[description]

When I uncheck "exclude from display" the output displays properly, but on every item in the view.

MBroberg’s picture

Here is a checklist of things to try:
Are you using the latest dev version of Views? Some modules had issues with not displaying the fields when excluded. Search the issues for that.
Are you using a generic View or a contrib View (Calendar, Taxonomy View, etc)? For some contrib modules that integrate with views, sometimes they don't work properly and there are patches.
Are all of the tokens you are using listed before the rewritten field? If so they will be showing up in the replacement pattern list.
If you do not exclude from display, do all of your fields show up in the display separately and also again in the rewritten field?
If you add words like this:

term: [tid]
header: <h3>[tid_1]</h3>
description: [description]

do the words show up but not the fields?
If you want the rewritten field to be the "group by" field, did you select a View Display Type that uses fields and groupings? And did you select the rewritten field as the "group by" field?
Please check all those first and report the results.

stongo’s picture

I hadn't installed the dev version. As soon as I installed that, it worked. Thanks for the help. This is a great feature! It really opens up views for me.

stongo’s picture

Issue tags: +taxonomy, +views, +term, +group by, +taxonomy image

Check out the result:
http://haliburtonhighway.com/digital-art-gallery
Works out nicely to be able to display artist descriptions and photos. This is a taxonomy term, taxonomy description, and taxonomy image
Thanks again! Might this be worth documenting somewhere?

tim.plunkett’s picture

When you use GROUP BY in a mysql query, it automatically reduces duplicates. The method above does not seem to do that, any ideas?

EDIT:
Assume field 1 is a string "Foo" and field 2 is a string "Bar":
The above method forces views to group on the string "Foo Bar".
My goal is to group all field1 and then group by field2, thus removing duplicates. Should I open another issue?

esmerel’s picture

Assigned: Unassigned » esmerel

Assigning to me for potential documentation integration.

MBroberg’s picture

#9 the Views issue queue and other related modules (taxonomy, lineage, etc) are filled with people looking for a way to get more than one grouping field, as in
Foo
-Bar
--field
--field
--field

Someone is reportedly working on a fix for this
http://drupal.org/node/477338
So I am hopeful

dawehner’s picture

Status: Active » Closed (duplicate)

So this is a duplicate of #477338: Multiple group: group by more than one field

As you all have seen this is a feature request

joachim’s picture

Well the example is really not very clear, but it doesn't seem to me that multiple field grouping is the same as a hierarchy.

A hierarchy puts complete result items into a tree. A grouping just pulls out certain fields.

But more clarification from the OP would be good.

danny englander’s picture

I had been looking how to do this for a while, #6 was a huge help. Thank you!

Anonymous’s picture

Comment #6 is genius and elegant; thank you MBroberg!