Currently, format_date() only supports short, medium, long, and 'custom' types. However, Drupal's admin interface allows you to create your own date types at admin/config/regional/date-time/formats/add and then admin/config/regional/date-time/types/add.
If the user has gone through the trouble to create these extra date types, I think format_date() should be able to use them. This would prevent proliferation of "helper" functions that are simply a call to format_date($timestamp, 'custom', $a_custom_format);. Modules which allow the user to select date types could make use of this as well.
This patch is a very simple change to format_date() - when $type is not custom or a predefined style (short, medium, long) try and find a matching variable to use. If not found, default to medium, this matches the current default behavior.
The test creates an admin-defined date type, then uses it via format_date(). It also tests that the medium type is used when an undefined type is passed in.
(also remove an errant space in the comments for url_is_external())
| Comment | File | Size | Author |
|---|---|---|---|
| #47 | format-date-locale.47.patch | 4.73 KB | drunken monkey |
| #43 | format-date-locale.42.patch | 4.78 KB | drunken monkey |
| #40 | format-date-locale.40.patch | 4.72 KB | drunken monkey |
| #38 | format-date-locale.38.patch | 4.72 KB | drunken monkey |
| #31 | drupal.format-date-locale.31.patch | 4.6 KB | sun |
Comments
Comment #1
kscheirererr, ignore the part about url_is_external() - managed to remove that, though there are still some minor whitespace issues in common.inc and common.test.
Comment #2
kscheirergoing for D7 to get testbot review, and maybe someone will think this is worthwhile :)
Comment #4
jhodgdonAt a minimum, this would need some updated documentation added to the documentation header for the function to explain what it's supposed to be doing, before it can get an adequate review.
Comment #5
kscheirerSure thing. I was waiting on the docs to see if anyone else would find this improvement useful.
Comment #6
redhatmatt commented#5: admin_defined_format_date_3.patch queued for re-testing.
Comment #7
jhodgdonThis looks very nice, and I think it should be committed. Probably needs another review before it can be RTBC.
I think it's actually a bug in Drupal that format_date() is not respecting types that are defined by the hooks and in the UI, since in D7, 'short', 'medium', and 'long' are no longer supposed to be so special -- they just happen to be the date format type names that system_date_format_types() returns, and should not be treated specially as opposed to any other hook_date_format_types() implementations or any user-defined types.
So I am changing this from a feature request to a bug.
Comment #8
drunken monkeyThe patch still applies and is a great addition, in my opinion. Then, also, the documentation of hook_date_format_types() won't be wrong anymore.
Comment #9
sunSorry, but I have troubles to follow... http://api.drupal.org/api/function/system_get_date_types/7 does not use variables to store custom date types. So what is this code for?
Is it possible that this patch does not account for a major new feature in Drupal 7? (Custom date types and formats)
Should be !isset()
Powered by Dreditor.
Comment #10
drunken monkeyThis is where the selected format finally gets stored, apparently. Just try it out!
Write a module implementing hook_date_format_types(), or create a new date type in the admin UI, apply this patch and you'll be able to use
format_date()with your new type.The fact that people have trouble following what is going on in the date format/type functions is a reason to fix at least part of it, not to hold up this patch. I myself probably wouldn't have found out how to format a date with an admin-created format, without finding this patch.
Comment #11
plach@sun:
See http://api.drupal.org/api/function/system_date_time_settings/7 (it's a system settings form) and http://api.drupal.org/api/function/locale_init/7.
I think we can do better than that for localized values, at least because now we have http://api.drupal.org/api/function/hook_language_init/7 to initialize multilingual variables. Moreover we might want to store the values in the variables table as a serialized array to avoid querying the DB at each page request.
But this is another issue, I'd say the patch is good and can go in once that
is_nullis fixed.Comment #12
drunken monkeyOK, good. Here is a revised patch with
isset().Comment #13
drunken monkeyComment #14
kscheirerLooks good to me!
Comment #15
plachCrossposted. I was setting this RTBC too :)
Comment #16
dries commentedThis is a feature request, not a bug report so moving to Drupal 8. I haven't reviewed the actual code yet.
Comment #17
plachActually this fixes a core API misbehavior: we have introduced admin-defined date formats but we can't use them through the related API. The code is pretty straightforward and does not introduce any API or other kind of harmful change. Moreover it provides additional test coverage. Please reconsider.
Comment #18
drunken monkeyThe bug here is that we already have documentation stating that this is possible. We can either fix the documentation, or fix the behaviour right away, and the latter already has an RTBC patch (besides having a far better result for developers). I second plach here.
Comment #19
sunGiven earlier and most recent explanations in this issue, I agree that this is a bug. I was under the impression that the new date formats would work differently, but I was wrong, sorry.
Comment #20
plachRelated issue #939392: Localized date formats are initialized too late.
Comment #21
tstoecklerActually this allows us to completely remove the special-casing for the core provided date types.
Also revamps the documentation for that function a bit, and makes e.g. the use of "date type" consistent, and makes the parameters consistently "(optional)".
Also: this is definitely a bug.
Comment #22
sunAre the default variable values already set elsewhere (during installation) ?
There should be a blank line between different switch cases, unless the code intentionally falls through.
However, depending on above mentioned system variable default values, we could as well replace the entire switch statement with a simple if ($type != 'custom') condition.
Powered by Dreditor.
Comment #23
sunBetter title.
Comment #24
kscheirerI think we need those cases for the default values, as sun mentioned. Can we go back to RTBC for the patch in #12?
Comment #25
jhodgdonThe documentation in this patch for the format_date() function is not clear, in my opinion. How about this (may need line wrapping and whitespace cleanup, since I just edited it here in this comment form):
Comment #26
das-peter commentedsubscribe & +1 for getting this fix into D7
Comment #27
tstoecklerRerolled #12 with jhodgon's PHPdoc from #25.
Comment #28
jhodgdonThe whitespace on the doc isn't right. There should be a space before each *, and the indentation in the @param $format is 3 spaces instead of 2.
The code looks right to me though.
Comment #29
sunFixed the phpDoc.
Comment #30
sunFixed the code logic.
Comment #31
sunWasn't happy with the isset($format) check, slightly revised. I'm happy now.
Comment #32
kscheirersun, I notice in #30 and #31 you're providing default values for variable_get, which aren't really needed anymore after #88264: variable_get($name, $default = NULL) went in.
But thats a minor quibble, I don't think it should hold up this patch, esp with D7 getting ready to go. RTBC from me, but I won't change the status since I'm the OP.
Comment #33
sunyes, I explicitly added the variable_get() default value of '' (empty string) in order to decrease confusion with the already existing function argument $format, which also defaults to ''.
Comment #34
jhodgdon+1 for RTBC on #31.
Comment #35
sunNo need to be shy ;)
Comment #36
webchickNow, this is just weird. We don't do this "default falls through to something specific" anywhere else with a switch statement and neither does php.net; default always comes last. I didn't even know this was possible.
It's not clear to me why it is needed, either. Everywhere else where we want the same behaviour to happen on a specific and default we do:
then..
Since you decided to kill kittens and make this "fix everything that was ever wrong with format_date() instead of just fixing the bug, let's also specify an example of what is meant here by "time zone identifier." Is this "UTC" or is it "+4" or?
Comment #37
kscheirerI don't think the timezone-related code was changed, just a doc update form jhodgdon to better reflect the current functionality.
Comment #38
drunken monkeyAttached is an updated patch without the switch-fallthrough-weirdness.
Since "time zone identifier" is, as far as I can see, never explained in Drupal but always uses strings like "America/Los_Angeles", I don't think we have to document that here. Otherwise we should probably just add a link to the PHP documentation.
Comment #40
drunken monkeyIf I just had a cent for every wrong negation in my code …
Comment #41
tstoecklerCode looks good. Documentation should be more explicit about time zone identifiers, as per #36. It might be used consistently throughout core, but for most people this will be the one and only date-relate function they'll ever call in Drupal, so an example can't hurt.
Comment #42
sunBoth the old and the new reads weird. How about changing to:
"A UNIX timestamp to format."
Duplicate information about the default. Let's keep the last only.
The idea was to not duplicate the date_format_medium variable. Let's do:
Powered by Dreditor.
Comment #43
drunken monkeyOK, makes sense. Only change here is the documentation for the $timezone parameter.
Comment #45
plach#43: format-date-locale.42.patch queued for re-testing.
Comment #46
jhodgdon#42 has not been fully addressed.
Also is "Defaults to the user's time zone." really accurate? Which user? And wouldn't that depend on settings -- I think there is a setting for using a site-wide time zone?
Comment #47
drunken monkeyLike the patch's file name suggests, I hadn't even seen that comment when creating the patch.
In fact, I just saw it now that you pointed it out. The new patch contains all of sun's comments in #42, as well as Jennifer's from #46.
Comment #48
sunThanks!
Comment #49
webchickLooks like all my feedback was addressed, thanks!
Committed to HEAD.
Comment #50
jhodgdonfollow-up issue (or at least related issue):
#989366: format_date() doesn't use programmatically created format.
Comment #52
claudiu.cristeaWhile it's a bug shouldn't this go into D6 too?
Comment #53
tstoecklerI might be completely on crack, but I don't think Drupal 6 allows you to define your own date formats?! (IIRC that was a date.module feature in D6)
Marking back to fixed.