Drupal - Date Repeat Rule Change Theme of "Repeats every day until"
Category:
In my use case, the most common date repeat rule used by authors is the "Repeats every day until". For example, an event spanning from October 2012 through April 2013, read something like this:
Repeats every day until Tue Apr 30 2013 .
Date is an awesome module, but this isn't an awesome way to show a date range. Let's change it to something like this instead:
October 5th, 2012 - April 30th, 2013
Looks better, eh? Charming, indeed.
So, how is it done? By implementing theme_date_repeat_display(), that's how. So inside your theme's template.php file:
/** * Implements theme_date_repeat_display(). */ function MY_THEME_date_repeat_display(&$vars) { if (isset($vars['item']['rrule'])) { // Let's pull out the rrule properties. For example, take this: // RRULE:FREQ=DAILY;INTERVAL=1;UNTIL=20130501T035959Z;WKST=SU // and turn it into: // array( // 'FREQ' => 'DAILY', // 'INTERVAL' => '1' , // 'UNTIL' => '20130501T035959Z', // 'WKST' => 'SU' // ); $rrule = array(); $rules = explode(';', str_replace('RRULE:', '', $vars['item']['rrule'])); foreach($rules as $rule) { $parts = explode('=', $rule); $key = $parts[0]; $value = $parts[1]; $rrule[$key] = $value; } // Now that we have our repeat rule, let's change the way it renders itself. // Change the 'Repeats every day until [date] .' display to our liking. if ($rrule['FREQ'] == 'DAILY' && $rrule['INTERVAL'] == 1) { $from = strtotime($vars['item']['value']); $to = strtotime($rrule['UNTIL']); $from_month = date('F', $from); $to_month = date('F', $to); $from_year = date('Y', $from); $to_year = date('Y', $to); $date = ''; if ($from_year == $to_year) { // Same year. if ($from_month == $to_month) { // Same month. $date = date('F jS', $from) . ' - ' . date('jS, Y', $to); } else { // Different months. $date = date('F jS', $from) . ' - ' . date('F jS, Y', $to); } } else { // Different year. $date = date('F jS, Y', $from) . ' - ' . date('F jS, Y', $to); } return '<div>' . $date . '</div>'; } } // If we made it this far, then we assume that we didn't want to theme this // repeat rule in a custom way, so let's just render it normally. return theme_date_repeat_display($vars); }
Flush Drupal's Theme Registry cache after adding this code to your template.php file. Now watch time fly by as your date repeat rule makes a little more sense, or so it seems.
Comments
squarecandy (not verified)
Wed, 08/21/2013 - 18:18
Permalink
Nice post! Got me headed in
Nice post! Got me headed in the right direction for sure. I ended up using the theme_preprocess_field() function so I could get rid of the default date or dates that display in addition to the text output by theme_date_repeat_display().
Here's what I came up with:
diarmy (not verified)
Fri, 11/29/2013 - 05:58
Permalink
Thanks Tyler! You can also
Thanks Tyler! You can also use theme_date_display_combination to determine how the repeat date display is used with the date.