Friday, August 20, 2010

views hooks to fix calendar display

Ran into a difficult bit of work today -

Views in Drupal based on the Calendar and Date modules are nifty, except when it comes to how you display recurring items.

On views fields, the 'group multiple values' settings don't allow you to set your 'show 1 value starting from' to correspond to the arguments which have been passed to your view.

That means you get the next item, even if it's outside the bounds of the argument driving your month, week or day view.

That's ugly, cause you'll get Sept 4th sandwiched between Aug 12 and Aug 15, since the event already happened, so it jumps forward to the next recurrence, but shows it in the original place on the month view. Ick.

Turns out I had to use hook_views_pre_render() to sift the $view->results array for nodes that were within the bounds of my arguments...here's the code:

function xxx_views_pre_render(&$view) {
// we want to exclude search results from this treatment - but the other calendar displays ( week, day, month ) seem to benefit
if ($view->name == 'eventcalendar' && $view->display_handler->display->id != 'page_2' ) {
// set our date arguments to unix timestamps for comparison with row data.
$startwindow = strtotime($view->date_info->min_date_date);
$endwindow = strtotime( $view->date_info->max_date_date );

foreach ($view->result as $key=>$row) {
$eventdate = strtotime($row->{yourDateFieldHere});
if ( $eventdate >= $startwindow && $eventdate <= $endwindow ) {
// our event is within the window set by the arguments to our view.
$newresult[] = $row;
}
}
$view->result = $newresult;
}
}

Another interesting point to note is that it seems ( at least on this view, need to verify elsewhere ) that $view->current_display is always set to 'calendar_period_2'. That's the month view...but it's set to that even if you're on week or day. That may be a function of the default display for the calendar? need to look into that further.

That meant I couldn't reference $view->current_display to include/exclude certain displays from my hook. I had to go deeper into the $view->display_handler object to get what I was looking for: found it at $view->display_handler->display->id.

Posted via email from dsayswhat's posterous

No comments:

Post a Comment