diff --git a/includes/common.inc b/includes/common.inc index 5f7cdb8..d8bf716 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('timestamp' => NULL, 'format_types' => array('text' => 'medium', 'value' => NULL), 'text' => NULL, 'attributes' => array()), + ), 'status_messages' => array( 'variables' => array('display' => NULL), ), diff --git a/includes/theme.inc b/includes/theme.inc index 7fad5f3..e296bc9 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1447,6 +1447,42 @@ function theme_disable($theme_list) { */ /** + * Returns HTML for a date / time. + * + * @param $variables + * An associative array containing: + * - timestamp: (optional) The UNIX timestamp for the datetime. If your + * datetime cannot be represented as a UNIX timestamp, use a formatted + * datetime string in $variables['attributes']['value'] instead. + * - format_types: (optional) The date format types to use for the text + * content and value attribute. These may be built-in date formats ('short', + * 'medium', or 'long'), date formats defined in hook_date_format_types(), + * or the machine names of administrator-defined date formats. + * - text: (optional) The format type to use to create a human readable + * string which will be used for the text content of the element. + * Defaults to 'medium'. + * - value: (optional) The format type to use for the value attribute. If + * no format type is set, the 'value' attribute's format defaults to the + * custom format 'Y-m-d\TH:i:s\Z' and is processed into UTC. + * - text: (optional) The text content to display in the element. This + * should only be used if an irregular text value is needed, such as + * "my birthday". + * - attributes: (optional) Associative array of attributes to be placed in + * the element. If a 'value' attribute is passed in, it will + * override the timestamp. The 'value' attribute must be a valid date + * string with optional time. + * See http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#valid-date-string-with-optional-time + * + * @see template_preprocess_time + */ +function theme_time($variables) { + $attributes = drupal_attributes($variables['attributes']); + $text = $variables['text']; + + return "$text"; +} + +/** * Returns HTML for status and/or error messages, grouped by type. * * An invisible heading identifies the messages for assistive technology. @@ -2564,6 +2600,43 @@ function theme_get_suggestions($args, $base, $delimiter = '__') { } /** + * Preprocess variables for theme_time(). + */ +function template_preprocess_time(&$variables) { + // A value attribute must always be present on the element. If no + // 'value' attribute was passed in, format the 'value' attribute from the + // timestamp. + if (empty($variables['attributes']['value'])) { + if (isset($variables['timestamp'])) { + // If the user hasn't specified the format type for the 'value' attribute, + // it is processed to a full datetime string in UTC. + if ($value_format = $variables['format_types']['value']) { + $variables['attributes']['value'] = format_date($variables['timestamp'], $value_format); + } + else { + $variables['attributes']['value'] = gmdate('Y-m-d\TH:i:s\Z', $variables['timestamp']); + } + } + } + + // If no text for the element was provided, print a human readable + // version of the 'value' attribute. + if (!isset($variables['text'])) { + $text_format = $variables['format_types']['text']; + try { + $date = new DateTime($variables['attributes']['value']); + $variables['text'] = format_date($date->getTimestamp(), $text_format); + } + catch (Exception $e) { + // If the date isn't properly formed, log it and set the text to an + // empty string. + watchdog_exception('theme', $e); + $variables['text'] = ''; + } + } +} + +/** * 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..c13f3fe 100644 --- a/modules/simpletest/tests/theme.test +++ b/modules/simpletest/tests/theme.test @@ -451,3 +451,87 @@ class ThemeHtmlTag extends DrupalUnitTestCase { $this->assertEqual('title test'."\n", theme_html_tag($tag), t('Test title tag generation.')); } } + +/** + * Unit tests for theme_time(). + */ +class ThemeTime extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Theme Time', + 'description' => 'Tests the theme_time() theme function.', + 'group' => 'Theme', + ); + } + + /** + * Test function theme_time(). + */ + function testThemeTime() { + // Create timestamp and formatted date for testing. + $timestamp = 280281600; + $date = format_date($timestamp); + $short_date = format_date($timestamp, 'short'); + + // Test with timestamp. + $variables = array( + 'timestamp' => $timestamp, + ); + $this->assertEqual('' . $date . '', theme('time', $variables), t('Test theme_time with timestamp.')); + + // Test with value attribute. + $variables = array( + 'attributes' => array( + 'value' => '1978-11-19', + ), + ); + $this->assertEqual('' . $date . '', theme('time', $variables), t('Test theme_time with value attribute set.')); + + // Test with value attribute and timestamp. Timestamp should be ignored. + $variables = array( + // Use a different timestamp. + 'timestamp' => 1319497322, + 'attributes' => array( + 'value' => '1978-11-19T00:00:00Z', + ), + ); + $this->assertEqual('' . $date . '', theme('time', $variables), t('Test theme_time with both timestamp and value attribute.')); + + // Test with text and timestamp. + $variables = array( + 'timestamp' => $timestamp, + 'text' => 'Dries\'s birthday', + ); + $this->assertEqual('Dries\'s birthday', theme('time', $variables), t('Test theme_time with text and timestamp.')); + + // Test with text and value attribute. + $variables = array( + 'text' => 'Dries\'s birthday', + 'attributes' => array( + 'value' => '1978-11-19', + ), + ); + $this->assertEqual('Dries\'s birthday', theme('time', $variables), t('Test theme_time with text and timestamp.')); + + // Test with special text formatting. + $variables = array( + 'timestamp' => $timestamp, + 'format_types' => array( + 'text' => 'short', + 'value' => NULL, + ), + ); + $this->assertEqual('' . $short_date . '', theme('time', $variables), t('Test theme_time with special text formatting.')); + + // Test with special value attribute formatting. Since the value attribute + // isn't a valid datetime, no text will be generated. + $variables = array( + 'timestamp' => $timestamp, + 'format_types' => array( + 'text' => 'medium', + 'value' => 'short', + ), + ); + $this->assertEqual('', theme('time', $variables), t('Test theme_time with special value attribute formatting.')); + } +}