diff --git a/core/includes/common.inc b/core/includes/common.inc index 44fa367..d7c73f4 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1911,58 +1911,15 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL $langcode = language(LANGUAGE_TYPE_INTERFACE)->langcode; } - switch ($type) { - case 'short': - $format = variable_get('date_format_short', 'm/d/Y - H:i'); - break; - - case 'long': - $format = variable_get('date_format_long', 'l, F j, Y - H:i'); - break; - - case 'html_datetime': - $format = variable_get('date_format_html_datetime', 'Y-m-d\TH:i:sO'); - break; - - case 'html_date': - $format = variable_get('date_format_html_date', 'Y-m-d'); - break; - - case 'html_time': - $format = variable_get('date_format_html_time', 'H:i:s'); - break; - - case 'html_yearless_date': - $format = variable_get('date_format_html_yearless_date', 'm-d'); - break; - - case 'html_week': - $format = variable_get('date_format_html_week', 'Y-\WW'); - break; - - case 'html_month': - $format = variable_get('date_format_html_month', 'Y-m'); - break; - - case 'html_year': - $format = variable_get('date_format_html_year', 'Y'); - break; - - case 'custom': - // No change to format. - break; - - case 'medium': - default: - // 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; + if ($type != 'custom') { + // Retrieve the format of the custom $type passed. + $config = config('system.date_format_type'); + $format = $config->get($type); + + // Fall back to 'medium'. + if (empty($format)) { + $format = $config->get('medium'); + } } // Create a DateTime object from the timestamp. diff --git a/core/includes/form.inc b/core/includes/form.inc index a142e69..b8964fc 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -2951,7 +2951,7 @@ function form_process_date($element) { $element['#tree'] = TRUE; // Determine the order of day, month, year in the site's chosen date format. - $format = variable_get('date_format_short', 'm/d/Y - H:i'); + $format = config('system.date_format_type')->get('short'); $sort = array(); $sort['day'] = max(strpos($format, 'd'), strpos($format, 'j')); $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M')); diff --git a/core/modules/aggregator/aggregator.pages.inc b/core/modules/aggregator/aggregator.pages.inc index 78ca5bc..0c04c7d 100644 --- a/core/modules/aggregator/aggregator.pages.inc +++ b/core/modules/aggregator/aggregator.pages.inc @@ -335,7 +335,7 @@ function template_preprocess_aggregator_item(&$variables) { $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp))); } else { - $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i')); + $variables['source_date'] = format_date($item->timestamp, 'medium'); } $variables['categories'] = array(); diff --git a/core/modules/system/config/system.date_format_type.yml b/core/modules/system/config/system.date_format_type.yml new file mode 100644 index 0000000..60bffb2 --- /dev/null +++ b/core/modules/system/config/system.date_format_type.yml @@ -0,0 +1,10 @@ +long: 'l, F j, Y - H:i' +medium: 'D, m/d/Y - H:i' +short: 'm/d/Y - H:i' +html_datetime: 'Y-m-d\TH:i:sO' +html_date: 'Y-m-d' +html_time: 'H:i:s' +html_yearless_date: 'm-d' +html_week: 'Y-\WW' +html_month: 'Y-m' +html_year: 'Y' diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php index 9639758..2f1a083 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php @@ -19,7 +19,7 @@ class FormatDateTest extends WebTestBase { * * @var array */ - public static $modules = array('language'); + public static $modules = array('language', 'locale'); /** * Arbitrary langcode for a custom language. @@ -37,9 +37,11 @@ class FormatDateTest extends WebTestBase { function setUp() { parent::setUp(); variable_set('configurable_timezones', 1); - variable_set('date_format_long', 'l, j. F Y - G:i'); - variable_set('date_format_medium', 'j. F Y - G:i'); - variable_set('date_format_short', 'Y M j - g:ia'); + $config = config('system.date_format_type') + ->set('long', 'l, j. F Y - G:i') + ->set('medium', 'j. F Y - G:i') + ->set('short', 'Y M j - g:ia') + ->save(); variable_set('locale_custom_strings_' . self::LANGCODE, array( '' => array('Sunday' => 'domingo'), 'Long month name' => array('March' => 'marzo'), diff --git a/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsLanguageTest.php b/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsLanguageTest.php index 2054180..2384f7d 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsLanguageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsLanguageTest.php @@ -61,16 +61,16 @@ class DateFormatsLanguageTest extends WebTestBase { $this->drupalGet('admin/config/regional/date-time/locale'); $this->assertText('French', 'Configured languages appear.'); $edit = array( - 'date_format_long' => 'd.m.Y - H:i', - 'date_format_medium' => 'd.m.Y - H:i', - 'date_format_short' => 'd.m.Y - H:i', + 'date_formats[long]' => 'd.m.Y - H:i', + 'date_formats[medium]' => 'd.m.Y - H:i', + 'date_formats[short]' => 'd.m.Y - H:i', ); $this->drupalPost('admin/config/regional/date-time/locale/fr/edit', $edit, t('Save configuration')); $this->assertText(t('Configuration saved.'), 'French date formats updated.'); $edit = array( - 'date_format_long' => 'j M Y - g:ia', - 'date_format_medium' => 'j M Y - g:ia', - 'date_format_short' => 'j M Y - g:ia', + 'date_formats[long]' => 'j M Y - g:ia', + 'date_formats[medium]' => 'j M Y - g:ia', + 'date_formats[short]' => 'j M Y - g:ia', ); $this->drupalPost('admin/config/regional/date-time/locale/en/edit', $edit, t('Save configuration')); $this->assertText(t('Configuration saved.'), 'English date formats updated.'); diff --git a/core/modules/system/lib/Drupal/system/Tests/System/DateTimeTest.php b/core/modules/system/lib/Drupal/system/Tests/System/DateTimeTest.php index 6494a4a..af1ab88 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/DateTimeTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/DateTimeTest.php @@ -46,7 +46,9 @@ class DateTimeTest extends WebTestBase { // 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'); + config('system.date_format_type') + ->set('medium', 'Y-m-d H:i:s O') + ->save(); // Create some nodes with different authored-on dates. $date1 = '2007-01-31 21:00:00 -1000'; diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 72c50d4..90b23bc 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -2039,12 +2039,13 @@ function system_regional_settings() { * Form builder; Configure the site date and time settings. * * @ingroup forms - * @see system_settings_form() + * @see system_date_time_settings_submit() */ -function system_date_time_settings() { +function system_date_time_settings($form, &$form_state) { // Get list of all available date types. drupal_static_reset('system_get_date_types'); $format_types = system_get_date_types(); + $config = config('system.date_format_type'); // Get list of all available date formats. $all_formats = array(); @@ -2077,14 +2078,15 @@ function system_date_time_settings() { $choices[$f] = format_date(REQUEST_TIME, 'custom', $f); } reset($formats); - $default = variable_get('date_format_' . $type, key($formats)); + $default = $config->get($type); // Get date type info for this date type. $type_info = system_get_date_types($type); $form['formats']['#theme'] = 'system_date_time_settings'; // Show date format select list. - $form['formats']['format']['date_format_' . $type] = array( + $form['formats']['format']['#tree'] = TRUE; + $form['formats']['format'][$type] = array( '#type' => 'select', '#title' => check_plain($type_info['title']), '#attributes' => array('class' => array('date-format')), @@ -2095,7 +2097,8 @@ function system_date_time_settings() { // If this isn't a system provided type, allow the user to remove it from // the system. if ($type_info['locked'] == 0) { - $form['formats']['delete']['date_format_' . $type . '_delete'] = array( + $form['formats']['delete']['#tree'] = TRUE; + $form['formats']['delete'][$type] = array( '#type' => 'link', '#title' => t('delete'), '#href' => 'admin/config/regional/date-time/types/' . $type . '/delete', @@ -2107,7 +2110,18 @@ function system_date_time_settings() { // Display a message if no date types configured. $form['#empty_text'] = t('No date types available. Add date type.', array('@link' => url('admin/config/regional/date-time/types/add'))); - return system_settings_form($form); + return system_config_form($form, $form_state); +} + +/** + * Form submission handler for system_date_time_settings(). + */ +function system_date_time_settings_submit($form, &$form_state) { + $config = config('system.date_format_type'); + foreach ($form_state['values']['format'] as $format_type => $value) { + $config->set($format_type, $value); + } + $config->save(); } /** @@ -2228,8 +2242,9 @@ function system_add_date_format_type_form_submit($form, &$form_state) { $format_type['locked'] = 0; $format_type['is_new'] = 1; system_date_format_type_save($format_type); - variable_set('date_format_' . $machine_name, $form_state['values']['date_format']); - + config('system.date_format_type') + ->set($machine_name, $form_state['values']['date_format']) + ->save(); drupal_set_message(t('New date type added successfully.')); $form_state['redirect'] = 'admin/config/regional/date-time'; } @@ -3263,16 +3278,17 @@ function system_date_format_localize_form($form, &$form_state, $langcode) { // Get configured formats for each language. $locale_formats = system_date_format_locale($langcode); // Display a form field for each format type. + $form['date_formats']['#tree'] = TRUE; foreach ($types as $type => $type_info) { if (!empty($locale_formats) && in_array($type, array_keys($locale_formats))) { $default = $locale_formats[$type]; } else { - $default = variable_get('date_format_' . $type, key($formats)); + $default = config('system.date_format_type')->get($type); } // Show date format select list. - $form['date_formats']['date_format_' . $type] = array( + $form['date_formats'][$type] = array( '#type' => 'select', '#title' => check_plain($type_info['title']), '#attributes' => array('class' => array('date-format')), @@ -3299,10 +3315,7 @@ function system_date_format_localize_form_submit($form, &$form_state) { // Get list of date format types. $types = system_get_date_types(); foreach ($types as $type => $type_info) { - $format = $form_state['values']['date_format_' . $type]; - if ($format == 'custom') { - $format = $form_state['values']['date_format_' . $type . '_custom']; - } + $format = $form_state['values']['date_formats'][$type]; system_date_format_localize_form_save($langcode, $type, $format); } drupal_set_message(t('Configuration saved.')); diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index de8a2ae..c058f73 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -3520,7 +3520,7 @@ function hook_archiver_info_alter(&$info) { * * To define a date type in a module and make sure a format has been assigned to * it, without requiring a user to visit the administrative interface, use - * @code variable_set('date_format_' . $type, $format); @endcode + * @code config('system.date_format_type')->set($type, $format); @endcode * where $type is the machine-readable name defined here, and $format is a PHP * date format string. * @@ -3590,7 +3590,7 @@ function hook_date_format_types_alter(&$types) { * initialization chooses a locale-specific format for the three core-provided * types (see system_get_localized_date_format() for details). If your module * needs to ensure that a date type it defines has a format associated with it, - * call @code variable_set('date_format_' . $type, $format); @endcode + * call @code config('system.date_format_type')->set($type, $format); @endcode * where $type is the machine-readable name defined in hook_date_format_types(), * and $format is a PHP date format string. * diff --git a/core/modules/system/system.install b/core/modules/system/system.install index b8d402c..f44d2a6 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -2062,6 +2062,22 @@ function system_update_8017() { } /** + * Moves systems date format settings from variables to config. + * Find all the variables that match the 'date_format_TYPE' pattern + * for conversion to config. + * + * @ingroup config_upgrade + */ +function system_update_8018() { + $variables = array(); + $format_types = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'date_format_%'")->fetchAll(); + foreach ($format_types as $type) { + $variables[$type->name] = trim(str_replace('date_format_', '', $type->name)); + } + update_variables_to_config('system.date_format_type', $variables); +} + +/** * @} End of "defgroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 69a1f62..259b08c 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -2086,7 +2086,7 @@ function system_init() { // Setup appropriate date formats for this locale. $formats = system_get_localized_date_format($languages); foreach ($formats as $format_type => $format) { - $conf[$format_type] = $format; + $conf['system.date_format_type'][$format_type] = $format; } } @@ -2136,10 +2136,11 @@ function system_init() { */ function system_get_localized_date_format($languages) { $formats = array(); + $config = config('system.date_format_type'); // Get list of different format types. $format_types = system_get_date_types(); - $short_default = variable_get('date_format_short', 'm/d/Y - H:i'); + $short_default = $config->get('short'); // Loop through each language until we find one with some date formats // configured. @@ -2151,12 +2152,13 @@ function system_get_localized_date_format($languages) { foreach ($format_types as $type => $type_info) { // If format exists for this language, use it. if (!empty($date_formats[$type])) { - $formats['date_format_' . $type] = $date_formats[$type]; + $formats[$type] = $date_formats[$type]; } // Otherwise get default variable setting. If this is not set, default // to the short format. else { - $formats['date_format_' . $type] = variable_get('date_format_' . $type, $short_default); + $default = $config->get( $type); + $formats[$type] = !empty($default) ? $default : $short_default; } } @@ -2168,15 +2170,16 @@ function system_get_localized_date_format($languages) { // No locale specific formats found, so use defaults. $system_types = array('short', 'medium', 'long'); // Handle system types separately as they have defaults if no variable exists. - $formats['date_format_short'] = $short_default; - $formats['date_format_medium'] = variable_get('date_format_medium', 'D, m/d/Y - H:i'); - $formats['date_format_long'] = variable_get('date_format_long', 'l, F j, Y - H:i'); + $formats['short'] = $short_default; + $formats['medium'] = $config->get('medium'); + $formats['long'] = $config->get('long'); // For non-system types, get the default setting, otherwise use the short // format. foreach ($format_types as $type => $type_info) { if (!in_array($type, $system_types)) { - $formats['date_format_' . $type] = variable_get('date_format_' . $type, $short_default); + $default = $config->get($type); + $formats[$type] = !empty($default) ? $default : $short_default; } } @@ -3686,7 +3689,7 @@ function system_time_zones($blank = NULL) { // reasons and should not be used, the list is filtered by a regular // expression. if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) { - $zones[$zone] = t('@zone: @date', array('@zone' => t(str_replace('_', ' ', $zone)), '@date' => format_date(REQUEST_TIME, 'custom', variable_get('date_format_long', 'l, F j, Y - H:i') . ' O', $zone))); + $zones[$zone] = t('@zone: @date', array('@zone' => t(str_replace('_', ' ', $zone)), '@date' => format_date(REQUEST_TIME, 'custom', config('system.date_format_type')->get('long') . ' O', $zone))); } } // Sort the translated time zones alphabetically. diff --git a/core/modules/user/lib/Drupal/user/Tests/UserTimeZoneTest.php b/core/modules/user/lib/Drupal/user/Tests/UserTimeZoneTest.php index 3ff4daf..252049c 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserTimeZoneTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserTimeZoneTest.php @@ -28,7 +28,9 @@ class UserTimeZoneTest extends WebTestBase { // Setup date/time settings for Los Angeles time. variable_set('date_default_timezone', 'America/Los_Angeles'); variable_set('configurable_timezones', 1); - variable_set('date_format_medium', 'Y-m-d H:i T'); + config('system.date_format_type') + ->set('medium', 'Y-m-d H:i T') + ->save(); // Create a user account and login. $web_user = $this->drupalCreateUser();