I hate having to look inside the function to see what the array that I have to pass in has to look like.
Also adds IMO useful links for people who are unsure what the first. resp. the second parameter has to look like.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tstoeckler’s picture

Well, a patch would be good, eh...

tstoeckler’s picture

Feels pretty silly to have to upload two patches for such a simple thing, but I thing we have a standard to document the data types.

tstoeckler’s picture

Is there a prize for most worthless patch frenzy of the month?

Since we're already changing the function we should also add the third-person 's' to the function description.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks!

The latest patch needs a "the" in this line:
+ * A date format array containing following keys:
(the following keys)

Otherwise, I think the patch is good.

tstoeckler’s picture

Status: Needs work » Needs review
FileSize
5.45 KB

Well, it's hard to reroll such a patch without digging up other stuff. This fixes all documentation related issues in all of the date-related functions in system.module I found. Most of them are coding-style issues, but 2 functions still referenced the date type as 'normal', 'short', 'long' or 'custom', which is simply incorrect in D7.

jhodgdon’s picture

Status: Needs review » Needs work

Actually, http://api.drupal.org/api/function/system_date_format_types/7 defines long, medium, and short types. So I think it is not a bad idea to leave these strings in the documentation?

Also, since you are fixing things up, let's get everything right. Here are some remaining problems after your patch:

a) Maybe we should be more specific about the "attributes" that system_get_date_types() returns, in its @return? All it says now is "an array of date types", which doesn't tell us much.

b) system_get_date_formats - one-line description still refers to the "format length", which is incorrect.

c) "id" is a psychological term (ego, superego, and id). The abbreviation for identifier should always be ID. Check your dictionary... so this needs fixing:
+ * Gets the format details for a particular id.

d) This could be expanded to say what the "details" are:
+ * An array of date format details.

e) This needs a verb (is):
* The format string, or NULL if no matching format found.

f)

 /**
- * Delete a date type from the database.
+ * Deletes a date type from the database.
  *
  * @param $date_format_type
- *   The date type name.
+ *   A string containing the date type name.
  */
 function system_date_format_type_delete($date_format_type) {

Date type or date format type? I think there is a difference?

tstoeckler’s picture

Will roll a new patch soon, just some replies for now:

Actually, http://api.drupal.org/api/function/system_date_format_types/7 defines long, medium, and short types. So I think it is not a bad idea to leave these strings in the documentation?

Using 'short', 'medium' and 'long' in these functions works just like it did in D6. But if you add new date types (which you couldn't in D6) you don't input 'custom', but 'name-of-date-type'. That is why I think the previous documentation was misleading (especially for people who were accustomed to how it worked in D6). Maybe we can do both:

+ *   A string containing the date type name. 'short', 'medium' and 'long' for instance are date types defined by System module.
Date type or date format type? I think there is a difference?

No there is none. The terminology is currently horribly inconsistent. The current usage is:
In the UI: "Date types"
function names: date_format_type
variables: both $date_format_type and $type (and I think also $format_type).
Maybe for D7 we can at least clean up the internal variable names. Although I don't know if changing the name (but not the usage) of a parameter counts as an API change. Also we would have to decide which is better (date types vs. date format types) and then change either the interface or the function naming in D8.

The rest of the comments will be incorporated in the next patch.

jhodgdon’s picture

How about saying something like "For example, the system module defines 'long', 'medium', and 'short'; other types can be defined in the user interface and by other modules."

Regarding the date types vs. date format types... There is a difference. A date format is something like what's in system_default_date_formats(). A date type is something like what's in system_date_format_types(). They can be associated with each other, but they are not the same thing at all. The terminology needs to be made consistent. There are two hooks too: hook_date_formats() and hook_date_format_types().

tstoeckler’s picture

Regarding the date types vs. date format types... There is a difference. A date format is something like what's in system_default_date_formats(). A date type is something like what's in system_date_format_types(). They can be associated with each other, but they are not the same thing at all. The terminology needs to be made consistent. There are two hooks too: hook_date_formats() and hook_date_format_types().

Sorry, but you got that wrong. I'm only talking about 'date format types' here. If you go to the date and time page in Drupal you can click a link saying "Add date type" not "Add date format type". Also like I said, the variable naming is inconsistent for date format types (not date formats).

jhodgdon’s picture

Looking just at the date-related functions in system.module, there are actually three different types of things that are sometimes called "date formats".

1) Results of hook_date_format_types -- key/label pairs, along with attributes like the module they came from. No PHP date formats are part of this structure, so I think the name "date format type" is really stupid. They are things like 'long' => t('Long'). Functions that deal with these:
- system_get_date_types()
- _system_date_format_types_build()
- hook_date_format_types() (and system_date_format_types(), which implements this hook)
- hook_date_format_types_alter()
- system_date_format_type_save()

2) PHP date format strings (with keys). Functions that deal with these:
- hook_date_formats() (and system_date_formats(), which implements this hook)

3) The associations between #2 items and #1 items. Functions that deal with these:
- system_get_date_formats()
- _system_date_formats_build()
- system_date_formats_rebuild()
- system_date_format_save()
- system_date_format_delete()
- system_get_date_format()
- system_date_format_locale()
- system_date_format_type_delete() [primarily for deleting a #1 item, but also deletes the associations]

We need a consistent nomenclature for these three types of items.

jhodgdon’s picture

Not to mention that the function format_date in common.inc is still hard-wiring 'long', 'short', and 'medium'. Can that possibly be correct?

This looks like a real mess, and it is not only documentation.

jhodgdon’s picture

tstoeckler’s picture

Status: Needs work » Needs review
FileSize
19.65 KB

...Dear lord, I've never written a 20k doc patch...

I think this is as good as it gets for D7. This provides extensive return information and renames the internal variables consistently. I can hardly imagine this is counts as an API change. The proposed naming is:
$date_type (e.g. 'short') < $date_format (A format including locale and date_type information, etc.) < $format (A simple format string).

While doing this I noticed that there is another inconsistency, namely language vs. locale, but it's not really misleading and it's not that prominent anyway.

This patch also fixes one bug, but since I was renaming variables anyway it seemed silly to stop there (l. 3558, system.module):
For date formats that are declared by multiple modules _system_date_formats_build is currently merging the existing array with $format. But $format is undefined, it has to be $module_format as is very evident when looking at the function (http://api.drupal.org/api/function/_system_date_formats_build/7). I would roll a patch without that if necessary, but ...

This definitely needs a completely overhaul in Drupal 8, but with this patch I think Average Joe (c) should be able to make sense of the commonly used functions.

Dries’s picture

Status: Needs review » Needs work
+++ modules/system/system.module	11 Aug 2010 17:36:41 -0000
@@ -3286,22 +3286,33 @@ function system_run_automated_cron() {
-  $date_format_types = &drupal_static(__FUNCTION__);
+function system_get_date_types($date_type = NULL) {
+  $date_types = &drupal_static(__FUNCTION__);
...
-  return $type ? (isset($date_format_types[$type]) ? $date_format_types[$type] : FALSE) : $date_format_types;
+  return $date_type ? (isset($date_types[$date_type]) ? $date_types[$date_type] : FALSE) : $date_types;

I don't understand why you renamed all instances of $type to $date_type? I prefer $type and would prefer to see you undo those changes. Ditto for changing $format into $date_format.

+++ modules/system/system.module	11 Aug 2010 17:36:41 -0000
@@ -3330,45 +3335,66 @@ function system_date_formats() {
+ *   - dfif: The date format ID.

This should be 'dfid' instead of 'dfif'. This typo is repeated a few times.

+++ modules/system/system.module	11 Aug 2010 17:36:41 -0000
@@ -3330,45 +3335,66 @@ function system_date_formats() {
+ *   - dfif: The date format ID.

Is that correct? Shouldn't 'dfif' be 'dfid'? This typo is repeated a few times.

+++ modules/system/system.module	11 Aug 2010 17:36:41 -0000
@@ -3381,35 +3407,37 @@ function system_date_formats_rebuild() {
-function system_date_format_locale($langcode = NULL, $type = NULL) {
-  $formats = &drupal_static(__FUNCTION__);
+function system_date_format_locale($langcode = NULL, $date_type = NULL) {
+  $date_formats = &drupal_static(__FUNCTION__);

I don't understand why you renamed $type to $date_type. $type is cleaner IMO.

Powered by Dreditor.

tstoeckler’s picture

I can live with $type but not really with $format for the above mentioned reasons. To explain in detail:

$format = 'Y-M-d H:i:s';
$date_format = array(
  'dfid' => 1,
  'format' => 'Y-M-d H:i:s',
  'type' => 'long',
  'locale' => array('de', 'en'),
);

We currently use $format for both, which leads to confusion (at least it confused me), that's why I introduced the discrepancy between $format vs. $date_format.

jhodgdon’s picture

I agree with tstoeckler on #15. There are three different kinds of information floating around with date formats, and I think they need to be distinguished. Having different variable names for each kind of information will help in that effort.

tstoeckler’s picture

Even though I personally find #13 to be the best solution, something that Dries might like:
$type vs. $format vs. $format_string ?!

jhodgdon’s picture

That works for me... Though maybe $format_info or $format_item would be more descriptive for the array of format information?

tstoeckler’s picture

$format_info vs. $format_string. That actually gets a +1 from me. That simple change would have saved me around half an hour when porting Clock module. And then together with $type Dries will be happy, too!?

jhodgdon’s picture

Let's try it out in the form of a patch and see if it flies. I actually haven't reviewed the previous patch yet...

jhodgdon’s picture

Title: Improve documentation for system_date_format_save » Improve documentation for date-related functions and hooks in system.module
Status: Needs work » Needs review
FileSize
17.45 KB

Here's a new patch.

It has fewer local variable name changes than the previous patch, and more changes to the documentation. I've attempted to be very clear about what is a PHP date format string, what is an array of information, and what is the name of a date type.

jhodgdon’s picture

#21: 854396-21.patch queued for re-testing.

tstoeckler’s picture

+++ modules/system/system.api.php	31 Aug 2010 22:23:06 -0000
@@ -3429,17 +3429,13 @@
  * A module may also extend the list date formats defined for a date type
  * provided by another module.

Not (yet) touched by this patch, but that's a pretty weird sentence.

+++ modules/system/system.module	31 Aug 2010 22:23:06 -0000
@@ -3396,15 +3423,20 @@
+ *   If $type and $langcode are specified, returns the corresponding date format
+ *   string. If only $langcode is specified, returns an array of all date
+ *   format strings for that locale, keyed by the date type. If neither is
+ *   specified, or if no matching formats are found, returns FALSE.

If the function always returns FALSE if $langcode is not specified, why is it optional?

I just looked at the patch for now. I'll dive into the code now and then reroll.

Powered by Dreditor.

jhodgdon’s picture

Can you provide just $type?

tstoeckler’s picture

FileSize
22.04 KB

I rewrote the documentation at system.api.php a bit. I left the system.module hunks alone as I think this is as good as we can do in D7.

My second question in #23 is valid, but it would be an API change to clean that up.
Let's go with this and clean up the whole mess in D8.

Also, thanks jhodgdon for bumping this and working on it, I had long since forgotten!

jhodgdon’s picture

This is looking pretty good. A few mostly minor suggestions:

a)

+ *   - 'locales': (optional) an array of 2 and 5 character locale codes; for

"An" should be capitalized (list items should start with capital letters, and the rest of this list is fine).

b)

 /**
- * Alters date types and formats declared by another module.
+ * Alters date formats declared by another module.

Alters -> Alter. Hooks are written as if to say "You can use this hook to...".

c)
Looking at the code for http://api.drupal.org/api/drupal/modules--system--system.module/function... -- it looks like it returns an object and not an array? It's using fetch(), which I think should be returning an object.

jhodgdon’s picture

Status: Needs review » Needs work
jhodgdon’s picture

I was going to fix up these few problems... but I'm having another problem with the patch. After patching, for some reason my system.api.php file is ending up with Windows rather than Unix line endings. The other two files are not having that problem... could be an artifact of Eclipse I suppose, but I'm not sure. So I guess I'll leave this to someone else to fix up.

tstoeckler’s picture

Status: Needs work » Needs review
FileSize
21.78 KB

Here we go.
Sorry, I had forgotten this issue (again!).

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to me, thanks tstoeckler!

Please note that although it looks like code changes occasionally in this patch, it's really just local variable names and doc blocks that are changing. There should be no API or functionality change. It should be clearer what's going on in the date format world after this patch, though.

tstoeckler’s picture

+++ modules/system/system.module	8 Dec 2010 19:50:25 -0000
@@ -3671,7 +3728,7 @@ function _system_date_formats_build() {
-      $date_formats[$module_format['type']][$module_format['format']] = array_merge_recursive($date_formats[$module_format['type']][$module_format['format']], $format);
+      $date_formats[$module_format['type']][$module_format['format']] = array_merge_recursive($date_formats[$module_format['type']][$module_format['format']], $module_format);

That is correct, except the above hunk.

I noted this above, but to reiterate for people coming late to this issue, that is actually a bug fix. If you look at _system_date_formats_build() you will see, though that
1. This code is only reached in edge cases (two modules provide the same date format)
2. The fix is incredibly obvious, because $format (which HEAD currently uses in the array_merge()) doesn't even exist in that function.
Since we were changing the internal variables anyway it would make no sense whatsoever to not make that change.

Thanks for the RTBC. Let's finally get this in!

Powered by Dreditor.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Feel free to re-open based on #31. Thanks all.

tstoeckler’s picture

By #31 I just meant that, strictly speaking, because of that one hunk, this patch is not only docs changes.
I still advocated committing it and see no reason to re-open this.
Sorry for being unclear about that.

jhodgdon’s picture

Status: Fixed » Closed (fixed)

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