Profile picture for user admin
Daniel Sipos
08 Oct 2013

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.

Profile picture for user admin

Daniel Sipos

CEO @ Web Omelette

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.

Contact us

Comments

cjoy 16 Oct 2013 17:59

Additional field required?

Nice solution for grouping display based views.

As in your example, I think the display of date related content listing is where this kind of grouping really makes sense - e.g. n archive/listing of events grouped by year. To group by date values, such as month or year, it can even be simplified.

No additional Grouping field is required on the content type. Instead, add the existing date field to the view and exclude it from display as explained in the article. Additionally, set the date format to your desired value* and in the "Rewrite results" section, check "Strip HTML tags" and "Remove whitespace" (this is the important bit as the date values will not be suitable for grouping otherwise!)

This should give the same result without duplicating content fields.

  • For example, I use a custom format "Year" which gives me a yyyy value, but you can choose any date format you want as long as you have defined it previously (see admin/config/regional/date-time/formats/add to define a new php date format and admin/config/regional/date-time/types/add to make it available).
Reggie 18 Aug 2018 19:46

In reply to by cjoy (not verified)

cjoy, your approach is a much

cjoy, your approach is a much better, more logical and efficient. I was thinking along the same lines. There should never be a need to add a helper field to a table. If a helper field is needed, it should be dynamically created in the query, in this case Views. Views comes with all the functionality (i.e. custom fields) needed to create helper fields.

Benjamin Melançon 16 Apr 2014 14:39

Views Node Field

For a simple module to do this without needing a reference module, or to make the reference yourself: https://drupal.org/project/viewsnodefield

Deg 19 Jul 2016 16:22

Thanks for the post! Saved me

Thanks for the post! Saved me a lot of time. I was surprised to see grouping options were only available to fields (though I suppose it makes sense).

Joachim Feltkamp 08 Feb 2019 12:41

In Drupal 8 you don't need Entity Reference anymore

Thanks for this great article. You saved my day.
I just want to add, that in Drupal 8 there is already a "Field" named "Redered Entity". You don't need the Module Entity Reference anymore.

Grant K 18 Sep 2019 01:51

Like Joachim above I'm using Content: Rendered entity in D8

Useful recipe. Thanks. Got me what I wanted, though it is a bit different using the "Content: Rendered entity" field in Drupal 8, the basis is the same, but I also had to add my grouping field to the fields list as a hidden field in order to be able to select it as the view's grouping field.

Add new comment