Add Individual Event to Calendar
A Drupal site I’m working on requires links for each event that would add the event to the user’s calendar. I’d like to provide this functionality for iCal, Outlook and Google. Drupals Calendar module offers something similar out of the box–there’s a feed which can deliver a an ics file on a per-day basis. I found some great instructions on how to deliver the ics for a single node. This method handles repeating event really well, but I just can’t make it deal with all-day events. No matter how I set my all day events up, the ics file delivers a start and end time. I’ll keep digging and hopefully find a solution.
To add the event to a Google calendar, I wrote a little code in my template.php file. Here’s my code, combined with the code from above to handle the single node ics feed:
function THEME_preprocess_node(&$vars, $hook) {
if ($vars['type'] == 'event') {
$node = $vars['node'];
$ics_link = "/calendar-single_ev_date/ical/".$node->nid;
$from = strtotime($node->field_ev_date[0]['value']);
$to = strtotime($node->field_ev_date[0]['value2']);
$event_title = str_replace(' ','+',$node->title);
$from_date = date('Ymd',$from);
$to_date = date('Ymd',$to);
$from_time_EST = date('His',strtotime($node->field_ev_date[0]['value']." UTC"));
$to_time_EST = date('His',strtotime($node->field_ev_date[0]['value2']." UTC"));
if ($from_time_EST == '000000' && $to_time_EST == '000000') {
$google_link =
'http://www.google.com/calendar/event?action=TEMPLATE&text='.
$event_title.'&dates='.$from_date.'/'.
$to_date.'&
location=CUSTOMER&trp=false&sprop=website:www.example.org&'.
'sprop;=name:CUSTOMER';
}else{
$from_time = date('His',$from);
$to_time = date('His',$to);
$google_link =
'http://www.google.com/calendar/event?action=TEMPLATE&text='.
$event_title.'&dates='.$from_date.'T'.$from_time.
'Z/'.$to_date.'T'.$to_time.
'Z&location=CUSTOMER&trp=false&'.
'sprop=website:www.example.org&sprop;=name:CUSTOMER';
}
$repeat_string = $vars['node']->content['field_ev_date']['field']['#children'];
$regex = '/^<div>(.*?)<\/div>/';
$matches = array();
if (preg_match($regex, $repeat_string, $matches)) {
$search_strings = array('Repeats',' .',' ',' ');
$replace_strings = array('repeats','.','+','+');
$repeat_string =
'&details=Note:+This+event+'.str_replace($search_strings, $replace_strings, $matches[1]).'
++Google+Calendar+does+not+allow+us+to+set+up+this+repeat+for+you.
++Please+use+the+Repeats+button+above+to+do+so,+if+desired.';
$google_link .= $repeat_string;
}
$vars['add_to_ical'] = "iCal";
$vars['add_to_outlook'] = "Outlook";
$vars['add_to_google'] = '<img src="http://www.google.com/calendar/images/ext/gc_button1.gif" border=0>';
}
}
Google has a great API on how to use this functionality. It handles all day events perfectly, but doesn’t seem to have any way to deal with repeating events, despite the “All day” checkbox you get on their site. I put a note for the user in the description box, which is unideal but will have to do.
We’re going to add some icons for iCal and Outlook, and we’ll be pretty much set.
Hey James, thanks for your help with this!

My pleasure. Sometimes you just need to talk it out!