system_get_date_formats() mentions that it returns the following fields:
* - dfid: The date format ID.
* - format: The format string.
* - type: The machine-readable name of the date type.
* - locales: An array of language codes. This can include both 2 character
* language codes like 'en and 'fr' and 5 character language codes like
* 'en-gb' and 'en-us'.
* - locked: A boolean indicating whether or not this date type should be
* configurable from the user interface.
* - module: The name of the module that defined this date format in its
* hook_date_formats(). An empty string if the format was user-defined.
* - is_new: A boolean indicating whether or not this date type is as of yet
* unsaved in the database.
yet a call to system_get_date_formats('short') returns
Array
(
[Y-m-d H:i] => Array
(
[type] => short
[format] => Y-m-d H:i
[locales] => Array
(
)
[locked] => 1
[is_new] =>
[dfid] => 1
[language] =>
)
...
This has an extra 'language' field and is missing the 'module' field.
Either the documentation or the code will need to be fixed. Due to a lack of tests (see #1013980: Date types/formats need proper tests), it's hard to know what's correct.
Comments
Comment #1
jhodgdonGood catch! Here's the function that actually builds the list returned by this function:
_system_date_formats_build()
http://api.drupal.org/api/drupal/modules--system--system.module/function...
- It appears that [language] being there is an artifact of how the list is generated, so I don't think we should document it. [locales] is the return component to use. What is happening is that individual $format items are being read from the database and each one may have a language value, but the individual languages are being merged into the locales array. So the fact that language is left on there is just an artifact. We should probably remove it so it doesn't confuse anyone again.
- It appears that [module] was supposed to be added to the return value, but it's only being added as '' for formats that were in the database, so it's pretty much useless. For module-provided types, it's not being defined at all, which pretty much defeats the purpose. My opinion is that we should remove it from the doc and remove the line that adds '' for the database-provided types.
Here's a patch, which removes the doc for module, removes the code that was adding an empty string to module to items from the database, and unsets the misleading language element. Also, when I was tinkering with this and figuring out what [language] was, I also couldn't resist rewriting the locales doc just a bit for these two functions. Hope that's OK.
Comment #2
jhodgdonWhoops. Use this one. Last patch had a bad copy/paste artifact in it.
Comment #3
bfroehle commentedAfter the patch in #2,
system_get_date_formats('short')returnsThis makes the doc-block consistent with the code, which is good. However is this correct, or should we be changing the code to return 'module'?
Comment #4
bfroehle commentedHere's an alternative patch, which instead of altering the doc alters the function to return 'module' as promised. This is very different than the patch in #2, although some of the doc wording changes were incorporated.
The operation of _system_date_formats_build() in this patch is very similar to that for _system_date_format_types_build() --- in fact the changes required were relatively trivial, just replace
with
Comment #5
jhodgdonI'm not sure what we need the 'module' component for. It was obviously not being used for anything.
Comment #6
bfroehle commentedI skimmed through the issue that created the code -- it's all in #11623: Localize date formats -- but didn't find any reference to the use of the 'module' field. I also looked through the date module in contrib and didn't notice any ['module'] or ->module strings.
One reason for keeping it is consistency with system_get_date_types which also returns the 'module' field.
Comment #7
sunI'd like @plach to review + confirm this patch.
Looks like we don't need the $modules variable.
This unset() could use a code comment.
Powered by Dreditor.
Comment #8
jhodgdonMaybe sun or platch could comment on which approach (#1 explanation/#2 patch, or #4) would be better. I personally don't think we need 'module', and 'language' clearly needs to go...
Comment #9
plachsubscribing while I find the time to have a look
Comment #10
bfroehle commentedImprovements to #4 based upon sun's comments in #7.
Added a comment:
Switched to not use
$modules:and
This still doesn't address jhodgdon's concern in #8
It's completely trivial to populate the 'module' field and brings consistency with system_get_date_types() which also returns a 'module' field. However, it's probably not used by anything so... it'd be good to have somebody else weigh in.
See #1013980: Date types/formats need proper tests for test routines.
Comment #11
sunNormally, when collecting data from modules and exposing them as configurable items, an important use-case of storing the originating 'module' is to clean up and delete the configuration data in hook_modules_uninstalled().
Comment #12
jhodgdonRE #11 - that isn't being done here, and I'm pretty sure we don't want to. These hooks are really just for getting date formats and date types into the UI. Once they've been set in the UI, they can be used elsewhere on the site in the UI (at least, I think so -- for instance as a format choice for a date field), so you wouldn't want to delete things that were originally defined by a module.
Comment #13
bfroehle commentedComment #14
webchickThis function no longer seems to exist in D8, and I can't find any reference to the string in question in D8 code base. (Though I didn't search overly long.)
Comment #15
webchickOops. My mistake; I didn't read far enough down the patch, and thought this was just a docs thing.
Comment #16
webchickDerp. This is about system_get_date_formats(), not system_get_date_types().
So the function does exist in D8, but looks like the return value docs have already been addressed there.
Comment #17
jhodgdonactually I think the current 8.x docs are exactly what are shown in the original report?
Comment #18
jhodgdonUgh. I agree that the current patch goes way beyond documentation. Changing component back. Sorry for the noise.
Comment #29
quietone commentedsystem_get_date_formats was removed in #2003892: Convert date formats to config entities. I checked the Drupal 7 api page and this issue is still valid. Changing version.