Grouping by year (or month and year) in a view
Views offers a neat grouping mechanism that allows you to list content with matching criteria under said criterion. For example, if you had 3 press releases from 2011 and 2 from 2010, you could display them like this:
2011
- Press Release #5 (April 1, 2011)
- Press Release #4 (March 31, 2011)
- Press Release #3 (January 25, 2011)
2010
- Press Release #2 (December 3, 2010)
- Press Release #1 (June 1, 2010)
To do this, you create a view with three fields:
- Title (linked to node)
- Date (in Month Day, Year format)
- Date (in Year format; exclude this from display) -- if you want to save yourself some confusion later, open this field, go to the "More" section, and enter "Year (grouping header)" to make it clear why this field is there even though it's not shown
Next to your view format (unformatted list, HTML list, etc.), click Settings, and then pick your second date field as the grouping header under "Grouping field Nr.1". Check off "Use rendered output to group rows." Save.
I've done this quite a few times, but on my current site, I found that even when I told it to group by year, it was coming out like this:
2011
- Press Release #5 (April 1, 2011)
2011
- Press Release #4 (March 31, 2011)
2011
- Press Release #3 (January 25, 2011)
2010
- Press Release #2 (December 3, 2010)
2010
- Press Release #1 (June 1, 2010)
This makes a little sense, since the dates all technically are different, but it's supposed to be grouping by the rendered output, which is just 2011 and 2010. I dug around and found that even though it looked like just "2011" to the naked eye, the code was actually <span class="date date-display-year" property="dc:date" datatype="xsd:dateTime" content="2011-04-01T05:00:00-05:00">2011</span>. The month, day, and even time values were still being stored even though they weren't visible.
To fix this, I opened the year field, went to the Rewrite Results section, and checked "Strip HTML tags." This gets rid of the code that differentiates the values from each other, leading to the expected grouping behavior. If you wanted to do the same thing for month and year grouping headers (e.g. December 2010), all you'd need to do is change the format of your grouping field to use the month and day instead of the year.
For more on date formats, see Modifying the page display for a monthly archive view (the "Three steps, for reusing" section).