#826486 by kscheirer, drunken monkey, tstoeckler, jhodgdon, sun: Fixed format_date() does not respect admin-defined date formats
parent
6c881d9c5a
commit
35d221ea41
|
@ -1819,26 +1819,29 @@ function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format a date with the given configured format or a custom format string.
|
* Formats a date, using a date type or a custom date format string.
|
||||||
*
|
|
||||||
* Drupal allows administrators to select formatting strings for 'short',
|
|
||||||
* 'medium' and 'long' date formats. This function can handle these formats,
|
|
||||||
* as well as any custom format.
|
|
||||||
*
|
*
|
||||||
* @param $timestamp
|
* @param $timestamp
|
||||||
* The exact date to format, as a UNIX timestamp.
|
* A UNIX timestamp to format.
|
||||||
* @param $type
|
* @param $type
|
||||||
* The format to use. Can be "short", "medium" or "long" for the preconfigured
|
* (optional) The format to use, one of:
|
||||||
* date formats. If "custom" is specified, then $format is required as well.
|
* - 'short', 'medium', or 'long' (the corresponding built-in date formats).
|
||||||
|
* - The name of a date type defined by a module in hook_date_format_types().
|
||||||
|
* - The machine name of an administrator-defined date format.
|
||||||
|
* - 'custom', to use $format.
|
||||||
|
* Defaults to 'medium'.
|
||||||
* @param $format
|
* @param $format
|
||||||
* A PHP date format string as required by date(). A backslash should be used
|
* (optional) If $type is 'custom', a PHP date format string suitable for
|
||||||
* before a character to avoid interpreting the character as part of a date
|
* input to date(). Use a backslash to escape ordinary text, so it does not
|
||||||
* format.
|
* get interpreted as date format characters.
|
||||||
* @param $timezone
|
* @param $timezone
|
||||||
* Time zone identifier; if omitted, the user's time zone is used.
|
* (optional) Time zone identifier, as described at
|
||||||
|
* http://php.net/manual/en/timezones.php Defaults to the time zone used to
|
||||||
|
* display the page.
|
||||||
* @param $langcode
|
* @param $langcode
|
||||||
* Optional language code to translate to a language other than what is used
|
* (optional) Language code to translate to. Defaults to the language used to
|
||||||
* to display the page.
|
* display the page.
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
* A translated date string in the requested format.
|
* A translated date string in the requested format.
|
||||||
*/
|
*/
|
||||||
|
@ -1869,15 +1872,26 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL
|
||||||
case 'short':
|
case 'short':
|
||||||
$format = variable_get('date_format_short', 'm/d/Y - H:i');
|
$format = variable_get('date_format_short', 'm/d/Y - H:i');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'long':
|
case 'long':
|
||||||
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
|
$format = variable_get('date_format_long', 'l, F j, Y - H:i');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'custom':
|
case 'custom':
|
||||||
// No change to format.
|
// No change to format.
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'medium':
|
case 'medium':
|
||||||
default:
|
default:
|
||||||
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
|
// Retrieve the format of the custom $type passed.
|
||||||
|
if ($type != 'medium') {
|
||||||
|
$format = variable_get('date_format_' . $type, '');
|
||||||
|
}
|
||||||
|
// Fall back to 'medium'.
|
||||||
|
if ($format === '') {
|
||||||
|
$format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a DateTime object from the timestamp.
|
// Create a DateTime object from the timestamp.
|
||||||
|
|
|
@ -2090,6 +2090,32 @@ class FormatDateUnitTest extends DrupalWebTestCase {
|
||||||
$this->refreshVariables();
|
$this->refreshVariables();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test admin-defined formats in format_date().
|
||||||
|
*/
|
||||||
|
function testAdminDefinedFormatDate() {
|
||||||
|
// Create an admin user.
|
||||||
|
$this->admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
||||||
|
$this->drupalLogin($this->admin_user);
|
||||||
|
|
||||||
|
// Add new date format.
|
||||||
|
$admin_date_format = 'j M y';
|
||||||
|
$edit = array('date_format' => $admin_date_format);
|
||||||
|
$this->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
|
||||||
|
|
||||||
|
// Add new date type.
|
||||||
|
$edit = array(
|
||||||
|
'date_type' => 'Example Style',
|
||||||
|
'machine_name' => 'example_style',
|
||||||
|
'date_format' => $admin_date_format,
|
||||||
|
);
|
||||||
|
$this->drupalPost('admin/config/regional/date-time/types/add', $edit, t('Add date type'));
|
||||||
|
|
||||||
|
$timestamp = strtotime('2007-03-10T00:00:00+00:00');
|
||||||
|
$this->assertIdentical(format_date($timestamp, 'example_style', '', 'America/Los_Angeles'), '9 Mar 07', t('Test format_date() using an admin-defined date type.'));
|
||||||
|
$this->assertIdentical(format_date($timestamp, 'undefined_style'), format_date($timestamp, 'medium'), t('Test format_date() defaulting to medium when $type not found.'));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for the format_date() function.
|
* Tests for the format_date() function.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue