Problem/Motivation

The function system_date_format_save() implements the functionality to save localized date formats in the table {date_format_locale}. Unfortunately, the check whether or not to save a format localized is broken. A list of enabled languages is fetched with the following code:

  $languages = language_list('enabled');
  $languages = $languages[1];

When checking each date format locale against this list to see if the language is enabled, the function uses if (in_array($langcode, $languages)). However, the langcodes are in the keys of this array. The values are full database records for each language.

Proposed resolution

Correct the function to use if (isset($languages[$langcode])).

Remaining tasks

User interface changes

None.

API changes

None.

Original report by @das-peter

The function system_date_format_save() already implements the functionality to save localized date formats in the table date_format_locale.Unfortunately, the check whether or not to save a format localized is broken.The attached patch fixes this.

Comments

Status: Needs review » Needs work
das-peter’s picture

Status: Needs work » Needs review
StatusFileSize
new774 bytes

Removed path prefix

sun’s picture

+++ modules/system/system.module
@@ -3740,7 +3740,7 @@ function system_date_format_save($date_format, $dfid = 0) {
       // Only proceed if language is enabled.
-      if (in_array($langcode, $languages)) {
+      if (isset($languages[$langcode])) {

Disabled languages are contained in $languages, too, AFAIK.

Powered by Dreditor.

das-peter’s picture

$languages is fetched using this code:

  $languages = language_list('enabled');
  $languages = $languages[1];

Thus there should be only the enabled languages in the variable.
Besides that I'm not sure how evil it would be to store the date formats from disabled languages ;)

sun’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for clarifying!

sun’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Sorry, this needs tests.

das-peter’s picture

Hmm, I'm not sure if the attached tests are sufficient. There is a lot more that could be covered...
But at least the topic of this issue is covered ;)

das-peter’s picture

Status: Needs work » Needs review
sun’s picture

Status: Needs review » Needs work

Testbot says invalid patch file.

das-peter’s picture

Status: Needs work » Needs review
StatusFileSize
new3.12 KB

Status: Needs review » Needs work
sun’s picture

+++ modules/system/system.test
@@ -1044,6 +1044,70 @@ class DateTimeFunctionalTest extends DrupalWebTestCase {
+    $query = db_select('date_formats', 'df');
+    $query->addField('df', 'format');
+    $query->condition('type', 'short');
+    $query->condition('format', 'dmYHis');
+    $format = $query->execute()->fetchColumn();

Can you rewrite these queries to:

$format = db_select('date_formats', 'df')
  ->fields('df', array('format'))
  ->condition('type', 'short')
  ->condition('format', 'dmYHis')
  ->execute()
  ->fetchField();

Powered by Dreditor.

das-peter’s picture

Thx sun - nice hint. Didn't recognized that always the addField kills the chaining.

das-peter’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
pisco’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new4.21 KB

Rerolled the patch against the current 7.x branch and verified that the tests run successfully.

sun’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs review » Needs work
Issue tags: +Needs backport to D7
+++ b/modules/system/system.test
@@ -1048,6 +1048,74 @@ class DateTimeFunctionalTest extends DrupalWebTestCase {
   }
+  /**

Missing blank line.

2 days to next Drupal core point release.

pisco’s picture

Status: Needs work » Needs review
StatusFileSize
new4.21 KB

Sorry for that, should be corrected now.

das-peter’s picture

Thank you sun for the review & thank you Pisco for the updated patch.
Would be nice to get rid of this diff in our own drupal repo. :)

sun’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! Let's get this in.

xjm’s picture

Issue summary added. I also opened #1234848: language_list() doc is missing return value to address the (lack of) documentation for language_list().

xjm’s picture

Issue summary: View changes

Updated issue summary.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x and 7.x. Thanks!

Status: Fixed » Closed (fixed)
Issue tags: -Needs backport to D7

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Added link to latest patch.