diff --git a/core/includes/common.inc b/core/includes/common.inc index 60f57f2..9a0e6a5 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1900,7 +1900,17 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL 'langcode' => $langcode, 'format_string_type' => $key, ); - return filter_xss_admin($date->format($format, $settings)); + $formatted_date = $date->format($format, $settings); + + $context = array( + 'date_time' => $date, + 'type' => $type, + 'format' => $format, + 'langcode' => $langcode, + ); + drupal_alter('format_date', $formatted_date, $context); + + return filter_xss_admin($formatted_date); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php b/core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php index 698ea1a..329f5fb 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php @@ -26,7 +26,7 @@ public static function getInfo() { /** * Set up required modules. */ - public static $modules = array(); + public static $modules = array('system_test'); /** * Test setup. @@ -109,4 +109,29 @@ public function testDateTimezone() { } + + /** + * Test hook_format_date_alter() function and format_date() function. + */ + function testFormatDate() { + // Setup date/time settings for Honolulu time. + $config = config('system.timezone') + ->set('default', 'Pacific/Honolulu') + ->set('user.configurable', 0) + ->save(); + $config = config('system.date') + ->set('formats.medium.pattern.php', 'Y-m-d H:i:s O') + ->save(); + // 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.'); + } } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 8ddbf09..40c5dd1 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -3614,6 +3614,27 @@ function hook_filetransfer_info_alter(&$filetransfer_info) { } /** + * Alter the formatted date. + * + * A module may implement this hook in order to alter the formatted date string. + * + * @param string $formatted_date + * The formatted date. + * @param array $context + * An associative array containing: + * - date_time: The DrupalDateTime object with the date to format. + * - 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/core/modules/system/tests/modules/system_test/system_test.module b/core/modules/system/tests/modules/system_test/system_test.module index 4494299..cac3907 100644 --- a/core/modules/system/tests/modules/system_test/system_test.module +++ b/core/modules/system/tests/modules/system_test/system_test.module @@ -380,3 +380,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 purpose. + $formatted_date = strrev($formatted_date); +} \ No newline at end of file