Main content begins here

Theming the Date Module for Drupal 5

Thursday, May 7, 2009

WayCoolWebDesign recently had two Drupal web sites in production at the same time that had design goals of using combined event dates. While the default display for dates running more than one day is something like "07 Apr 2009 - 12 Apr 2009" we instead needed the dates to appear shortened as "7-12 APR 2009". While the date module allows for many different lengths of dates I didn't see that this option was offered. It was time for more drastic measures.

Theming the Date Module

While I'm not certain this exactly qualifies as theming since I am not changing display code but rather some functionality. Semantics aside, thank goodness the function used to display the dates is offered as a themable function.

The function you'd look for is the "date_display_combination" function inside "date.theme" file within the Date module. Before adding this function to my template.php file I had used the normal course of choosing pretty short dates within the Drupal functionality, so I knew that dates were set to show something like "07 Apr 2009" before fiddling with the function.

Next I copied the entire function to the template.php file. I then added this block of code right after the point in the function where $date1 and $date2 have been assigned the Drupal-formatted dates (@line 15 or so):

$jbyear1 = substr($dates['value']['db']['datetime'],0,4);
$jbmonth1 = substr($dates['value']['db']['datetime'],5,2);
$jbyear2 = substr($dates['value2']['db']['datetime'],0,4);
$jbmonth2 = substr($dates['value2']['db']['datetime'],5,2);
$jbday1 = substr($dates['value']['db']['datetime'],8,2);
if (($jbyear1 == $jbyear2) && ($jbmonth1 == $jbmonth2) && ($date1 != $date2)) {
$date1 = $jbday1;
switch($date1) {
case '01': $date1='1'; break;
case '02': $date1='2'; break;
case '03': $date1='3'; break;
case '04': $date1='4'; break;
case '05': $date1='5'; break;
case '06': $date1='6'; break;
case '07': $date1='7'; break;
case '08': $date1='8'; break;
case '09': $date1='9'; break;
default: $date1=$date1; break;
// END:

More Customization of the Drupal Date Module

I'm not a heavy PHP guy so my code is never elegant. I was not hoping to solve every date issue that may come up, only this particular one. All I was hoping to achieve was that if an event date had more than one day, and the years of both dates were the same, plus the months of both dates were the same, I wanted to combine them. I could see that if Drupal wanted to output "07 Apr 2009" all I need to do was grab the first 2 digits and make that into $date1. Then I ran a switch to get rid of the annoying zero that appears when the date is a single digit 1-9.

After adding this block of code Drupal would now have the first date as a one or two digit number and the second date would be kept as the unaltered second date. Then when combined as normal they'd show the way we intended. The final piece to the puzzle was changing "5-7 Apr 2009" into "5-7 APR 2009" by using CSS text-transform: uppercase.

Blog terms:

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <h2> <br> <img>
  • Lines and paragraphs break automatically.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
You're the bomb John.
Y. G. D.
Copyright ©2001-2018 Way Cool Web Design LLC. All Rights Reserved.