How to manage contextual filters for a simple date-based view I'm building a simple view to show content of X type, filtered by date. I want this view to be available via URLs like:


/events/2012       - Show all content for this year only
/events/2012/11    - Show all content for this year and month only
/events/2012/11/28 - Show all content for this exact day only


These URLs form part of this section's hierarchy so the patterns are non-negotiable.

The date I'm filtering on is a date field provided by the Date module, and has an optional end date.

I've tried setting the path of the view to /events/%/%/%, and have added three contextual filters for the date field. The granularity for each of the filters is set correctly to the respective date part for that filter.

However, none of that works; I either get all event content, regardless of year, for the year filter, or no content at all if I include the other filters.

There are several related questions (none with answers as it happens):


https://drupal.stackexchange.com/questions/29663/contextual-filters-and-year-by-date-module
Creating a contextual filter for a view based on month and day
Use date as Views Contextual filters


But nothing that covers exactly what I'm looking to do.

How do I tame these contextual filters, without resorting to the PHP filter which is simply not an option? I can't believe that there's no way to do this just with the UI, it seems like one of the most obvious use-cases.

UPDATE

I tried using a filter with a granularity of month, and changed the argument to 2012-11...and it worked.

So I think the answer is simply that the date filter handlers don't work as I expected.
Here is the one approach,


First create a custom call back for /events
Remove the date contextual filters from your view
Add only one filter(not contextual filter) to your view for that date field, accept the default values and save the view
Then use the following code in your custom callback to execute the view

function get_events($year = '', $month = '', $day = '')
{
  // Display ID of the view.
  $display_id = 'default';
  // get the view object.
  $view = views_get_view('events_views'); //Replace with your view name
  $filter = $view-&gt;get_item($display_id, 'filter', 'field_event_date_value');
  if ($year) {
    $filter['value']['value'] = $year;
    $filter['granularity'] = 'year';
  }
  if ($year &amp;&amp; $month) {
    $filter['value']['value'] = $year . '-' . $month;
    $filter['granularity'] = 'month';
  }
  if ($year &amp;&amp; $month &amp;&amp; $day) {
    $filter['value']['value'] = $year . '-' . $month . '-' . $day;
    $filter['granularity'] = 'day';
  }
  $view-&gt;set_item($display_id, 'filter', 'field_event_date_value', $filter);
  // If you have any other arguments read from the URL and pass it here as part of args array
  $result = $view-&gt;execute_display($display_id, $args);
}


Hope you are aware of how to pass arguments to the view programatically. This is what we are using for complex date filters in views which lacks lot of support for date functionality, however we haven't tried with granularity. This is un tested and not optimized. Modify it according to your need.