diff --git a/includes/common.inc b/includes/common.inc index a05a09a..1e288e5 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -6426,6 +6426,9 @@ function drupal_common_theme() { 'render element' => 'elements', 'template' => 'region', ), + 'datetime' => array( + 'variables' => array('timestamp' => NULL, 'text_type' => 'medium', 'value_type' => 'datetime', 'text' => NULL, 'attributes' => array()), + ), 'status_messages' => array( 'variables' => array('display' => NULL), ), diff --git a/includes/theme.inc b/includes/theme.inc index e50df06..ea57300 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -1460,6 +1460,39 @@ 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 the + * datetime cannot be represented as a UNIX timestamp, instead use a valid + * date string with optional time in $variables['attributes']['value'] + * and a human readable string in $variables['text']. + * - text_type: (optional) The format type to use to create a human readable + * string for the text content of the element. This may be a + * built-in date format type ('short', 'medium', or 'long'), a date format + * type defined in hook_date_format_types() if it has a format assigned, or + * the machine name of an administrator-defined date format. Defaults to + * 'medium'. + * - value_type: (optional) Either 'date' or 'datetime'. Defaults to + * 'datetime'. + * - text: (optional) The text content to display in the element. Use + * this variable if you are providing the formatted date value in + * $variables['attributes']['value'] or if an irregular text content 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_datetime + */ +function theme_datetime($variables) { + return '' . $variables['text'] . ''; +} + +/** * Returns HTML for status and/or error messages, grouped by type. * * An invisible heading identifies the messages for assistive technology. @@ -2582,6 +2615,37 @@ function theme_get_suggestions($args, $base, $delimiter = '__') { } /** + * Preprocess variables for theme_datetime(). + */ +function template_preprocess_datetime(&$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 ($variables['value_type'] == 'datetime') { + $variables['attributes']['value'] = gmdate('Y-m-d\TH:i:s\Z', $variables['timestamp']); + } + else { + $variables['attributes']['value'] = gmdate('Y-m-d', $variables['timestamp']); + } + } + } + + // If no text for the element was provided, print a human readable + // version of the 'value' attribute. + if (!isset($variables['text']) && isset($variables['timestamp'])) { + $text_type = $variables['text_type']; + $variables['text'] = format_date($variables['timestamp'], $text_type); + } + + // Preprocess functions usually store attributes in 'attributes_array'. Move + // 'attributes' to 'attributes_array'. + $variables['attributes_array'] = $variables['attributes']; + unset($variables['attributes']); +} + +/** * 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 fd88124..181ff18 100644 --- a/modules/simpletest/tests/theme.test +++ b/modules/simpletest/tests/theme.test @@ -488,3 +488,62 @@ class ThemeHtmlTplPhpAttributesTestCase extends DrupalWebTestCase { $this->assertTrue(count($attributes) == 1, t('Attribute set in the body element via hook_preprocess_html() found.')); } } + +/** + * Tests for theme_datetime(). + */ +class ThemeDatetime extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Theme Datetime', + 'description' => 'Test the theme_datetime() function.', + 'group' => 'Theme', + ); + } + + /** + * Test function theme_datetime(). + */ + function testThemeDatetime() { + // 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('datetime', $variables), t('Test theme_datetime with timestamp.')); + + // Test with text and timestamp. + $variables = array( + 'timestamp' => $timestamp, + 'text' => 'Dries\'s birthday', + ); + $this->assertEqual('Dries\'s birthday', theme('datetime', $variables), t('Test theme_datetime 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('datetime', $variables), t('Test theme_datetime with text and timestamp.')); + + // Test with text formatting. + $variables = array( + 'timestamp' => $timestamp, + 'text_type' => 'short', + ); + $this->assertEqual('' . $short_date . '', theme('datetime', $variables), t('Test theme_datetime text formatting.')); + + // Test with value attribute formatting. + $variables = array( + 'timestamp' => $timestamp, + 'value_type' => 'date', + ); + $this->assertEqual('' . $date . '', theme('datetime', $variables), t('Test theme_datetime value attribute formatting.')); + } +}