On one of my Drupal 6 sites I have a list of events, where the Event content type is provided by the Calendar module using Date for their date fields. These events need to be filtered by a date range and ordered with the most recent first. This would be easy if it weren’t for the fact that some events don’t have dates. Specifically, ‘from’ and ‘to’ dates.
I had to do two things:
-
To prevent events with an empty date being filtered out, I installed the Views Or module and use it to filter by date range OR events with empty date:
Views Or: Begin alternatives = Date: Date (node) (Content: Date (field_date) - From date OR Content: Date (field_date) - To date) Exposed Popup Day Views Or: Next alternative = Content: Date - From date Content: Date (field_date) - From date AND Content: Date (field_date) - To date empty Views Or: End alternatives =
-
To move the events with an empty date to the bottom of the list, I created a custom module to insert an ‘order by’ clause so that events with null dates would be listed last:
/** * Implementation of hook_views_query_alter(). * Sort null date fields last (rather than first) for the calendar view. */ function mymodule_views_modifications_views_query_alter(&$view, &$query) { if ($view->name == 'calendar') { array_unshift($query->orderby, 'ISNULL(node_data_field_date_field_date_value) ASC'); } }
In this code, replace ‘mymodule’ with the name of your module, replace ‘calendar’ with the name of your view and replace ‘node_data_field_date_field_date_value’ with the name of your date field. You can determine these by printing out the
$view
and$query
objects to inspect their contents.