Sort Multiple Formats of Date Fields in Views

Scenario: two or more content types. First content type uses a CCK Date field with date, hours, minutes, while the other content type(s) only need a date field without the hours and minutes. Because a CCK Date field sets the granularity globally, most users set up two different fields with two different granularity settings. This works well, until there is a need to display all of the content sorted by date - Views can't sort and/or merge two independent fields and then do a global sort.

The answer is surprisingly simple. In the appropriate content type, define only one CCK Date field and set the granularity to the highest level used - most likely minutes. Use that same field in both content types. If you are deleting the second Date field, be sure to migrate the data first from the existing nodes. That gives Views only a single field to sort and you a consolidated query that incorporates both all day and scheduled events. Two more adjustments may be desirable for the user interface.

How do you remove the text box for hours:minutes so that users won't get confused? No need to remove it with CSS - edit the field in the content types and under Customize Default Value choose the appropriate Input Format - for the content that only requires dates, choose a format without hours and minutes; for content type that requires hours and minutes, choose accordingly. CCK is smart to display or hide the hours:minutes text box as needed.

The full day events, however, will display the date with (All day) tacked to the end of it. Removal is accomplished by adding three lines of code to template.php of your chosen theme.

function themename_date_all_day_label() {
	return '';
}

Of course, replace themename with the name of your theme. Be sure to flush the theme registry and utilize the same Date field with different settings across multiple content types.

Share this post