Drupal: Views: how to group fields
Asked Answered
R

4

15

I've created a views of node and I've selected the "fields" option in order to select which fields to display and in which order.

Is there a way to group such fields ? It would be very useful to have parent divs in my html code.

thanks

Recusant answered 2/2, 2011 at 22:39 Comment(0)
U
1

Yes, click on the gear next to the Style option (under Basic Settings) and set the Grouping field. Keep in mind, the grouping field needs to be added to the view, but you can check the Exclude from display option to hide it, if needed.

I'm pretty sure that would give you a parent div. If not, you could add the parent div, by overriding the default templates (look to the Theme: Information section, under Basic Settings, for more information).

Unanswerable answered 2/2, 2011 at 22:48 Comment(2)
I've found this: echodittolabs.org/blog/2010/09/…Recusant
I believe the way Matt V. described (grouping field) will group the rows, not the fields, as Patrick asked.Melo
M
34

There's a simpler way to do it: http://www.arsnova.cc/web-development-articles/2012-07-27/grouping-fields-together-drupal-views

Let's say you want to group fields 1-3 together within a div.

  1. First, exclude fields 1 and 2 from display. (By "exclude", I do not mean to delete them from the views display; rather, click on the "exclude from display" button within each field's options.) The fields you exclude from display must come before the other field for this to work. If they don't, rearrange them so they do.
  2. Go to field 3, and rewrite the results, inserting the token for each of fields 1-3.

That's all you need to do. Fields 1, 2, and 3 will now appear within field 3's div. If you want, you can also add additional markup within the "rewrite results" area.

Melo answered 1/8, 2012 at 16:12 Comment(2)
I know this is an old answer but this should be the accepted answer.Mishmash
This reply is acceptable if AND ONLY IF you don't want to change any field's template. When you use the rewriting technique, the field is not rendered upon rewriting. For example, imagine a date field that must look different depending on the date being past or future. You need to evaluate the date and add some theme logic to the template or preprocess function... but you can't, because the field is not fully rendered.Unmixed
T
12

Recently found the best way for my needs and apparently for the needs of many people: Use views_fieldsets module https://drupal.org/project/views_fieldsets, it allows yo to create a grouping div to put fields inside.

Towhaired answered 28/5, 2014 at 6:7 Comment(2)
This is the right answer in 2014! Great find. Theming views is a headacheHackberry
This is the perfect example of answering the question asked. This question is very old, and there are lots of ways to do this in Drupal, but this seems to be the easiest by far. Thanks.Elson
E
2

Grouping is built into Drupal 8 Views. So no need for any extra modules. Here's how:

  1. Edit your view (be sure it has fields)
  2. Click Format's Settings
  3. A box will pop up with all your selected fields
  4. Look right underneath all the field names, there should be "Grouping field Nr.1"
  5. Select the field you'd like to group by
  6. Click Apply
  7. If you want to subgroup with a second field, just go back to 2 then you'll see "Grouping field Nr.2"

That's it.

Eighteenth answered 7/4, 2020 at 23:8 Comment(0)
U
1

Yes, click on the gear next to the Style option (under Basic Settings) and set the Grouping field. Keep in mind, the grouping field needs to be added to the view, but you can check the Exclude from display option to hide it, if needed.

I'm pretty sure that would give you a parent div. If not, you could add the parent div, by overriding the default templates (look to the Theme: Information section, under Basic Settings, for more information).

Unanswerable answered 2/2, 2011 at 22:48 Comment(2)
I've found this: echodittolabs.org/blog/2010/09/…Recusant
I believe the way Matt V. described (grouping field) will group the rows, not the fields, as Patrick asked.Melo

© 2022 - 2024 — McMap. All rights reserved.