From e5074859edbfd0b07a12cec8139406f080cfeac3 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_time to theme.inc Signed-off-by: Jesse Beach --- includes/common.inc | 3 + includes/theme.inc | 114 +++++++++++++++++++++++++++++++++++ modules/simpletest/tests/theme.test | 83 +++++++++++++++++++++++++ 3 files changed, 200 insertions(+), 0 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index 5f7cdb8..9b57db2 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -6438,6 +6438,9 @@ function drupal_common_theme() { 'render element' => 'elements', 'template' => 'region', ), + 'time' => array( + 'variables' => array('text' => NULL, 'attributes' => array(), 'path' => NULL), + ), 'status_messages' => array( 'variables' => array('display' => NULL), ), diff --git a/includes/theme.inc b/includes/theme.inc index bea87c0..ffb15c2 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1447,6 +1447,34 @@ function theme_disable($theme_list) { */ /** + * Returns HTML for a date / time. + * + * @param $variables + * An associative array containing: + * - 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 +2591,92 @@ function theme_get_suggestions($args, $base, $delimiter = '__') { } /** + * Process variables for theme_time(). + * + * 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: + * - gmdate_format: 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 + * - timestamp: the original value of the datetime attribute. This can be used + * to change the output format of the datetime attribute or the time element + * text. + * - offset: A Unix style time from or until the datetime + * attribute value. Providing an offset will cause the time element text + * to be rendered as an interval e.g. 2 months, 3 days ago. To get an + * interval from today, set the offset property to REQUEST_TIME. + * - labels: 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. + * - granularity: The granularity of the interval time, passed to + * format_interval(). + * - langcode: The langcode of the interval time, passed to + * format_interval(). + * - format_options: The options passed to format_date(). + */ +function template_preprocess_time(&$variables) { + // Get the datetime attribute. If none exists, assume REQUEST_TIME; + if (empty($variables['attributes']['datetime'])) { + $variables['attributes']['datetime'] = REQUEST_TIME; + } + // Save the datetime attribute to a variable so that modules can reformat + // the datetime attribute value in subsequent process functions. + $variables['timestamp'] = $variables['attributes']['datetime']; + // Set the datetime attribute date format. + if (empty($variables['gmdate_format'])) { + $variables['gmdate_format'] = 'Y-m-d\TH:i:s\Z'; + } + // Format the datetime attribute with gmdate(). + $variables['attributes']['datetime'] = gmdate($variables['gmdate_format'], $variables['timestamp']); + // If an offset time is provided, print the