diff --git a/includes/common.inc b/includes/common.inc index 20cc82b..6cbefac 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -2044,16 +2044,30 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL // input string. // Paired backslashes are isolated to prevent errors in read-ahead evaluation. // The read-ahead expression ensures that A matches, but not \A. + $original_format = $format; $format = preg_replace(array('/\\\\\\\\/', '/(? $date_time, + 'timestamp' => $timestamp, + 'type' => $type, + 'format' => $original_format, + 'timezone' => $timezone, + 'langcode' => $langcode + ); + drupal_alter('format_date', $formatted_date, $context); + + return filter_xss_admin($formatted_date); } /** diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module index 2eda351..6d8f328 100644 --- a/modules/simpletest/tests/system_test.module +++ b/modules/simpletest/tests/system_test.module @@ -420,3 +420,11 @@ function system_test_authorize_init_page($page_title) { system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title); drupal_goto($authorize_url); } + +/** + * Implements hook_format_date_alter(). + */ +function system_test_format_date_alter(&$formatted_date, array $context) { + // Reverse the date string for testing purposes. + $formatted_date = strrev($formatted_date); +} \ No newline at end of file diff --git a/modules/system/system.api.php b/modules/system/system.api.php index 0af6156..2d9a758 100644 --- a/modules/system/system.api.php +++ b/modules/system/system.api.php @@ -4796,6 +4796,26 @@ function hook_filetransfer_info_alter(&$filetransfer_info) { } /** + * Alter the formatted date. + * + * @param string $formatted_date + * The formatted date. + * @param array $context + * An associative array containing: + * - date_time: The PHP DateTime object with the date to format. + * - timestamp: The Unix timestamp. + * - type: The format to use as passed to format_date(). + * - format: If type is 'custom', a PHP date format string suitable for input + * to date(). + * - langcode: The language code as passed to format_date(). + * + * @see format_date() + */ +function hook_format_date_alter(&$formatted_date, array $context) { + $formatted_date .= ' ' . $context['type']; +} + +/** * @} End of "addtogroup hooks". */ diff --git a/modules/system/system.test b/modules/system/system.test index aefbfbc..c384282 100644 --- a/modules/system/system.test +++ b/modules/system/system.test @@ -1367,6 +1367,30 @@ class DateTimeFunctionalTest extends DrupalWebTestCase { ->fetchColumn(); $this->assertFalse($format, 'Localized date format for disabled language is ignored.'); } + + /** + * Test format_date_alter. + */ + function testFormatDateAlter() { + module_enable(array('system_test')); + // Setup date/time settings for Honolulu time. + variable_set('date_default_timezone', 'Pacific/Honolulu'); + variable_set('configurable_timezones', 0); + variable_set('date_format_medium', 'Y-m-d H:i:s O'); + + // Create some nodes with different authored-on dates. + $date1 = '2007-01-31 21:00:00 -1000'; + $date2 = '2007-07-31 21:00:00 -1000'; + $node1 = $this->drupalCreateNode(array('created' => strtotime($date1), 'type' => 'article')); + $node2 = $this->drupalCreateNode(array('created' => strtotime($date2), 'type' => 'article')); + + // Confirm date format was changed by implemented hook function. + $this->drupalGet("node/$node1->nid"); + $this->assertText('0001- 00:00:12 13-10-7002', 'Date was reversed successfully.'); + $this->drupalGet("node/$node2->nid"); + $this->assertText('0001- 00:00:12 13-70-7002', 'Date was reversed successfully.'); + module_disable(array('system_test')); + } } class PageTitleFiltering extends DrupalWebTestCase {