From 1ba34030f27f2d58ffc77a1cf821bd0db1ebfc62 Mon Sep 17 00:00:00 2001 From: jessebeach Date: Sun, 7 Aug 2011 22:32:07 -0400 Subject: [PATCH] Issue #1183250 by jessebeach: Introduced theme_datetime to theme.inc Signed-off-by: Jesse Beach --- includes/common.inc | 3 + includes/theme.inc | 106 +++++++++++++++++++++++++++++++++++ modules/simpletest/tests/theme.test | 79 ++++++++++++++++++++++++++ 3 files changed, 188 insertions(+), 0 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index d34c19d..41762ae 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -6435,6 +6435,9 @@ function drupal_common_theme() { 'render element' => 'elements', 'template' => 'region', ), + 'datetime' => array( + 'variables' => array('timestamp' => NULL, 'gmdate_format' => 'Y-m-d\TH:i:s\Z', 'text' => NULL, 'format_options' => array(), 'attributes' => array(), 'href' => NULL), + ), 'status_messages' => array( 'variables' => array('display' => NULL), ), diff --git a/includes/theme.inc b/includes/theme.inc index bea87c0..e43da85 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1447,6 +1447,62 @@ function theme_disable($theme_list) { */ /** + * Returns html for a date / time. + * + * @param $variables + * An associative array containing: + * - timestamp: The Unix style timestamp to be themed. + * - gmdate_format: (optional) The format for the Unix style timestamp passed + * to the gmdate() function. Defaults to 'Y-m-d\TH:i:s\Z'. We use gmdate() + * with Z for the UTC value so that the datestamp remains constant + * regardless of the time on the server that requested it. + * @see http://www.php.net/manual/en/function.gmdate.php + * - text: (optional) The text to display in the '; + + return $output; +} + +/** * Returns HTML for status and/or error messages, grouped by type. * * An invisible heading identifies the messages for assistive technology. @@ -2563,6 +2619,56 @@ function theme_get_suggestions($args, $base, $delimiter = '__') { } /** + * Process variables for theme_datetime(). + * + * The offset value is used to calculate a time interval from the timestamp. + * The labels are used to indicate if the interval represents how long in the + * past the timestamp event occurred or when in the future the timestamp event + * will occur. If the offset and the timestamp are equivalent, the label + * indicates that the timestamp has happened now. + * + * @param $variables + * An associative array containing: + * - offset (optional): A Unix style time from or until the timestamp for + * printing the time element text as an interval + * - labels (optional): An associative array containing the following optional + * properties + * - past: The label for an interval time that occurred in the past + * - future: The label for an interval time that occurs in the future + * - now: The label for an interval time that is the current time + * - granularity: The granularity of the interval time, passed to + * format_interval() + * - langcode: The langcode of the interval time, passed to + * format_interval() + */ +function template_process_datetime(&$variables) { + if (!empty($variables['offset'])) { + // Prepare the interval labels. + $past_label = (!empty($variables['labels']['past'])) ? $variables['labels']['past'] : t('ago'); + $future_label = (!empty($variables['labels']['future'])) ? $variables['labels']['future'] : t('to go'); + $now_label = (!empty($variables['labels']['now'])) ? $variables['labels']['now'] : t('just now'); + $granularity = (!empty($variables['labels']['granularity'])) ? $variables['labels']['granularity'] : 2; + $langcode = (!empty($variables['labels']['langcode'])) ? $variables['labels']['langcode'] : NULL; + // Calculate the time interval. + $interval = $variables['timestamp'] - $variables['offset']; + // Determine if the date is in the past or the future and + // adjust the human readable label according to the interval value. + switch ($interval) { + case ($interval > 0) : + $variables['text'] = format_interval(abs($interval), $granularity, $langcode) . ' ' . $future_label; + break; + case ($interval < 0) : + $variables['text'] = format_interval(abs($interval), $granularity, $langcode). ' ' . $past_label; + break; + case ($interval === 0) : + default : + $variables['text'] = t($now_label); + break; + } + } +} + +/** * The variables array generated here is a mirror of template_preprocess_page(). * This preprocessor will run its course when theme_maintenance_page() is * invoked. diff --git a/modules/simpletest/tests/theme.test b/modules/simpletest/tests/theme.test index 7c68989..44da2bc 100644 --- a/modules/simpletest/tests/theme.test +++ b/modules/simpletest/tests/theme.test @@ -451,3 +451,82 @@ class ThemeHtmlTag extends DrupalUnitTestCase { $this->assertEqual('title test'."\n", theme_html_tag($tag), t('Test title tag generation.')); } } + +/** + * Unit tests for theme_datetime(). + */ +class ThemeDatetime extends DrupalUnitTestCase { + public static function getInfo() { + return array( + 'name' => "Theme HTML time tag", + 'description' => 'Tests the theme_datetime() theme function.', + 'group' => 'Theme', + ); + } + + /** + * Test function theme_datetime(). + */ + function testThemeDatetime() { + // Test the server's timezone + $timestamp = 280281600; + $variables = array( + 'timestamp' => $timestamp + ); + // format_date is called because the display of the timestamp will depend + // on the timezone of the server that runs this function. + $date = format_date($timestamp); + $this->assertEqual('', theme('datetime', $variables), 'Test a basic invocation of theme_datetime with just a timestamp.'); + + // Test a specific timezone - Europe/Brussels + $timestamp = 280281600; + $variables = array( + 'timestamp' => $timestamp, + 'format_options' => array( + 'timezone' => 'Europe/Brussels', + ), + ); + $this->assertEqual('', theme('datetime', $variables), 'Test theme_datetime with a timestamp and the timezone Europe/Brussels.'); + + // Test the text rendered as an interval in the past. + $timestamp = 280281600; + $offset = 1227052800; + $variables = array( + 'timestamp' => $timestamp, + 'offset' => $offset, + 'format_options' => array( + 'timezone' => 'Europe/Brussels', + ), + 'labels' => array( + 'past' => 'in the past', + ), + ); + $this->assertEqual('', theme('datetime', $variables), 'Test theme_datetime with an offset time of 30 years in the past and the timezone Europe/Brussels.'); + + // Test the text rendered as an interval in the future. + $timestamp = 1227052800; + $offset = 280281600; + $variables = array( + 'timestamp' => $timestamp, + 'offset' => $offset, + 'format_options' => array( + 'timezone' => 'Europe/Brussels', + ), + 'labels' => array( + 'future' => 'in the future', + ), + ); + $this->assertEqual('', theme('datetime', $variables), 'Test theme_datetime with an offset time of 30 years in the future and the timezone Europe/Brussels.'); + + // Test wrapping the time text in a link. + $timestamp = 280281600; + $variables = array( + 'timestamp' => $timestamp, + 'format_options' => array( + 'timezone' => 'Europe/Brussels', + ), + 'href' => 'http://buytaert.net/', + ); + $this->assertEqual('', theme('datetime', $variables), 'Test theme_datetime with a timestamp, the timezone Europe/Brussels and a link.'); + } +} \ No newline at end of file -- 1.7.3.4