Last updated February 23, 2010. Created by Rowanw on March 2, 2009.
Edited by bekasu. Log in to edit this page.
Views has a feature called grouping, which allows you to split a list of results into arbitrary categories. Basically, if several items fall under the same category then they'll form their own list. For grouping to work though, your view must have a couple of fields, luckily you can use any available field for your groups.

Modules used:
Create a content type for your events
Using CCK, create a new content type and name it Event. Add a new field named Date and choose Date as the type. Configure the field as you require.
Create a new view
Using Views, add a view named events.
Choose a style for the view, preferably table.
Add the fields Node: Title and Content: Date, these will be visible fields so configure them to look presentable.
Even though we just added the date field, we need to add it a second time so it can be used for grouping. This field should not be presented alongside the other fields, so in the settings check "Exclude from display" and set the label to none.
The reason we need a second field for the date is because we're going to modify its output to return only a month and year.
Now click on the settings icon for the Style option and under Grouping field select our hidden date field.
You may also want to add a filter for Node: Published and Node: Type.
If you preview the view you should notice that each node has been separated into its own group, and each group is headed with a unique date. Since the dates are all unique we don't get any actual groups.
What we need to do now is modify the hidden date field so it only returns the month and year.
Ensure your view has a page or block display type and save it.
Override the date field output
To find out which file we need to copy for theming our field we need to click on the Theme: Information link, this gives us a list of potential file names for various parts of the view. From the list, find the identifier of our hidden field and look at the second suggested file name, in my case it's called: "views-view-field--field-date-value-1.tpl.php". Yours may or may not be different.
To use this file we need to copy the file named "views-view-field.tpl.php" from "sites/all/modules/views/theme" and paste it into our own theme directory. Immediately rename the file to the suggested file name above, include all the hyphens.
Open the new file and replace <?php print $output ?> with:
<?php print date('F Y', strtotime($row->node_data_field_date_field_date_value)); ?>F = Month
Y = Year
Now when you load the final view you should get a nice grouped list similar to the screenshot.
Customizations
How to customise the date output: http://php.net/manual/en/function.date.php
How to change the look of the text: http://v1.reinspire.net/blog/2005/09/15/css_font_styling/
Caution
If you wonder why your output is not grouping things together, disable Theme developer, and see if that helps.
In case it saves others from scratching their heads, here's something to note about this example:
The reason you write a template is that the grouping field does its comparison based on the output of the template rather than the raw data. This was apparently done to implement things like granular date grouping relatively easily.
One BIG side effect of this is that any tools that monkey with the output, say... THEME DEVELOPER ... will cause what seem like equivalent date strings to be different. More specifically, the output will be wrapped in a tag, that includes a thmr attribute that increments.
Comments
Now groups by 'Event dates: January 1970'
I followed this and it doesn't group by month, it just groups them all into one big group of 'Event dates: January 1970'.
Theme developer is not enabled.
A solution
I was only getting January 1970 as well until I changed the name of the date field in the output override example (field_date) to match the name I had given this field in my content type (field_release_date).
For me this meant changing
<?phpprint date('F Y', strtotime($row->node_data_field_date_field_date_value));
?>
to
<?phpprint date('F Y', strtotime($row->node_data_field_release_date_field_release_date_value));
?>
Once I made that change it worked like a charm.
I have my override properly
I have my override properly named as well and I'm getting December 1969 regardless of what I name it.
EDIT: Never mind, I had to uncheck "Group Multiple Values". Then it worked fine.
You can also bypass the need
You can also bypass the need to override the view theme by using the awesome views_customfield module. In the view, add the date field you want to use, then add a new custom php field and use the date formatting code above. Finally, group by this custom field in the view's style setting.
grouped by January 1970
Thanks for the tutorial, unfortunately it is not working for me.
I keep getting everything grouped under January 1970.
I have tried
<?phpprint date('F Y', strtotime($row->node_data_field_date_field_date_value));
?>
and
<?phpprint date('F Y', strtotime($row -> node_data_field_event_date_field_event_date_value));
?>
which I got from the following SQL
SELECT node.nid AS nid,
node_revisions.title AS node_revisions_title,
node_data_field_event_date.field_event_date_value AS node_data_field_event_date_field_event_date_value,
node.type AS node_type,
node.vid AS node_vid,
node_revisions.body AS node_revisions_body,
node_revisions.format AS node_revisions_format
FROM drupal_node node
LEFT JOIN drupal_node_revisions node_revisions ON node.vid = node_revisions.vid
LEFT JOIN drupal_content_type_event node_data_field_event_date ON node.vid = node_data_field_event_date.vid
WHERE node.type in ('event')
ORDER BY node_data_field_event_date_field_event_date_value ASC
I have tried custom_field, but still getting no where.
If I find the solution I will post it here for others to see. If anybody else has any suggestions please let me know.
Many thanks
Did you find a solution i
Did you find a solution i have the same problem, thanks you to reply!
MMMMMh!! i found an other
MMMMMh!! i found an other way!
just need to create a custom format in date-time and to put it in Short format for exemple
Same problem, I find why. My
Same problem, I find why.
My date format is timemestamp, and strtotime converts string to timestamp.
Just take strtotime() off, (and adjust the right name for your field) and it's ok.
Solved the problem
Hi Folks I have solved the problem.
If you are using the custom_field module you need to use $data in the place of $row. I should have read the module information better (that will teach me in future!!!)
So
Follow all the instructions given above.
When you come to do the "Override the date field output" use the custom_field module and use as folllows
- Add a field and choose Customfield from the group drop down list.
- Choose "Customfield: PHP code"
- Add
- Check "Exclude from display"
- In the Value enter the following:
<?phpprint date('F Y', strtotime($data -> node_data_field_event_date_field_event_date_value));
?>
(To find the correct variable to replace "node_data_field_event_date_field_event_date_value" check your SQL output and copy the text between ORDER and ASC on the last line. You may need to check the style configuration to change the grouping to the title you have chosen for the PHP Customfield)
- Click update and you should have the result you require.
This is my first time solving a problem, so hope I am clear with my instructions.
Would it be possible to show
Would it be possible to show the grouped items by page (one group by page)?
What about the translation of
What about the translation of the month ?
I'm trying to display the month in french.
I have tried this, but does not work, the month is still displayed in english.
<?phpprint t(date('F', strtotime($data -> node_data_field_event_date_field_event_date_value))) . ' ' . date('Y', strtotime($data -> node_data_field_event_date_field_event_date_value));
?>
Any suggestions ?
Oupsss... Found it
Oupsss...
Found it :
<?phpprint format_date(strtotime($data ->node_data_field_event_date_field_event_date_value), 'custom', 'F') . ' ' . date('Y', strtotime($data -> node_data_field_event_date_field_event_date_value));
?>
Thanks for all
Slightly different path for view templates
Hi, cheers got it to work, a few things were slightly different on my install, probably because I am a newbie, but I thought I would note them in case anyone had similar setup.
1 .My system has the view themes installed in modules/views/theme rather than sites/all/modules/views/theme so this is where I had to copy the view template from.
2. Consequently I had to copy the view theme to sites/all/themes/zen/templates ( this being where the zen theme automatically installed itself ).
3. The grouped view did not work in the preview in views administation, only when emdeded in a node ( which I called via php ).
You don't need to overwrite the date field output
You don't need to overwrite the output in a views template file. Just add a custom date format here: admin/settings/date-time/formats/add and choose it as format of the date field in the view!
(thanks a lot for the tip to disable devel)
I'll second this as the
I'll second this as the preferred method. Means you can avoid unnecessarily creating an ugly template that sits in your theme cluttering it up, and furthermore you can edit the date format through the interface later if you need to without editing code.
unordered list
Hi guys,
I was using this amazing option for the creation of events listing but nowI have a problem. I followed all steps and the view is working but, after the addition of 4 months (events grouped by month), the last one goes to the first place (april, may, june and july is working ok, but when I added august its come to the first place on listing). Can anybody help me?
Thanks in advance,
Alex
I'll third that
I'll third that.
DONT CREATE A TEMPLATE OVERRIDE unless you must.
I did both, both methods work, but setting up a custom date format in the admin/config and using that for grouping is ofcourse much cleaner.
+1 - thanks. This problem is
+1 - thanks.
This problem is an easy one to solve after reading this post :)