In this tutorial I will show you a useful technique if you are looking to group the results of your View based on a field even if you want/have to use the content format instead of fields for individual rows. In other words, you can maintain your custom display mode as the output format for the rows but use the value of a field on your content type to group the rows around it and even display this value as a heading for the groups.
Why I find this useful? I had a View displaying nodes representing some events spanning three days. They were of course ordered by the date, but for an increased visibility, I was asked to "group" them by the day the events were in and display that day as a heading (Monday, Tuesday, etc). My problem was that I was using a custom display mode for the formatting of the rows so the initial approach to add another field and group by it was not so obvious.
So what can you do without having to redo the design of the display mode in question and use fields instead? You can install the Entity Reference module and follow along. I will use an example View in this tutorial in which I display my Article nodes using the Teaser display mode. Let’s say this is the starting point as I don’t want to stop using the nice Teasers.
The first thing you need to do is add another field to the content type. I went with a simple text field in my case because it was simple (and called it Grouping). Doesn't matter what you call it, the label will never be visible. Feel free to adapt to your needs. Then edit each node in this View, and type the day into this field (let’s say for some Monday and for others Tuesday). Then go back to editing the View.
First of all, change the row styles from Content to Fields. Yeah, I know I said I wouldn’t change this but bear with me. Then remove the Title field that is added by default and add a new field - the one you just added to your content type (Grouping). You should remove the label, exclude it from being displayed and leave the rest as default. Next up, add another field called Content: Rendered Node. This one will be available only if you have Entity Reference installed.
Make sure you remove the label again, under Display go with
Show complete entity and select the View mode you’d like the nodes to be displayed with (for me I said it’s
Teaser). Save the field and check in the View preview: you should see a list of node teasers. The next thing you need to do is set the grouping to be made by this field we added (Grouping). Under
Format -> Settings you can select the field to be used for this. And that’s it.
If you save the View and check it out, you’ll see it in action. If you used the days of the week in the field you added, you should see Monday at the top followed by all the nodes for which you added Monday, then Tuesday and so on. Nifty a?
There is one thing I’d like still to talk about. You installed and enabled the Entity Reference module, which although both cool and awesome at the same time, might be a bit too much just for using this technique. In my site it was already installed because I was using it for its core purpose and for any site larger than a blog you might too. But if not, you should think a bit whether it is worth adding this module to your site only for this small thing.
And of course, if you figured out another or better way of achieving what I just did, please let me know in the comments. I’d be very interested to see what others might have come up with.
Danny founded WEBOMELETTE in 2012 as a passion project, mostly writing about Drupal problems he faced day to day, as well as about new technologies and things that he thought other developers would find useful. Now he now manages a team of developers and designers, delivering quality products that make businesses successful.