From 8961c4b758e49bb56d501fe04e72ee04659a3a19 Mon Sep 17 00:00:00 2001 From: indigoxela Date: Wed, 14 Oct 2020 13:46:14 +0200 Subject: [PATCH] Issue #4632: Date fields should respect localized display formats. By @indigoxela, @stpaultim & @herbdool. --- core/modules/date/date.module | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/core/modules/date/date.module b/core/modules/date/date.module index 09a627abba6..f241b8aee4e 100644 --- a/core/modules/date/date.module +++ b/core/modules/date/date.module @@ -529,22 +529,36 @@ function date_formatter_format($formatter, $settings, $granularity = array(), $l } /** - * Helper function to get the right format for a format type. + * Helper function to get the right date format for a format type. * - * Checks for locale-based format first. + * @param string $format_type + * Name of the date format, for instance "short", "medium" or "long". + * @param string $langcode + * Language code provided, for instance "en". + * + * @return string + * PHP date format pattern for selected or current language, default pattern + * if no localized variant exists. */ function date_format_type_format($format_type, $langcode = NULL) { $static = &backdrop_static(__FUNCTION__); if (!isset($static[$langcode][$format_type])) { $date_format = system_date_format_load($format_type); - $format = isset($date_format['locales'][$langcode]) ? $date_format['locales'][$langcode] : $date_format['pattern']; - // If locale enabled and $format_type inexistent in {date_format_locale} - // we receive (due to inconsistency of core api) an array of all (other) - // formats available for $langcode in locale table. - // However there's no guarantee that the key $format_type exists. - // See http://drupal.org/node/1302358. + // Fallback to (undeletable) medium format if the requested one does not + // exist anymore. + if (!$date_format) { + $date_format = system_date_format_load('medium'); + } + // If no langcode has been provided or only LANGUAGE_NONE, use the current + // language to determine the correct localized format. + if (!isset($langcode) || $langcode == LANGUAGE_NONE) { + global $language; + $langcode = $language->langcode; + } + + $format = isset($date_format['locales'][$langcode]) ? $date_format['locales'][$langcode] : $date_format['pattern']; $static[$langcode][$format_type] = $format; } return $static[$langcode][$format_type];