There are several hooks in modules/field/field.api.php that do not have function body examples to go with them. They need to be filled out.

There's also one hook in field_ui.api,php that I don't think is ever invoked (at least it's not obvious to me where):
hook_field_formatter_settings_form()
If it is a real hook, in needs a function body. If it isn't, it should be removed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

hook_field_formatter_settings_form() is not invoked currently, because so far no good UI solution was found for formatter settings :-(.

jhodgdon’s picture

It also looks like hook_field_update_instance() is not ever called, so it should be removed? That should be verified...

gdd’s picture

Another problem in field.api.php is that many of the hooks don't indicate that their params should be passed by reference. These needs to be fixed as well.

jhodgdon’s picture

Just as a note on #3, this probably mostly applies to _alter hooks. Regular hooks generally return their information, whereas alter hooks alter existing information and usually that info has to be passed by reference into the hook function.

fago’s picture

jhodgdon’s picture

Bump. This is the only unpatched issue left to get all the new hooks documented in D7 (barring people finding more issues with the other hook doc).

Anyone want to take it on? :)

jhodgdon’s picture

Assigned: Unassigned » jhodgdon

OK, I'll do it.

jhodgdon’s picture

Status: Active » Needs review
FileSize
3.01 KB

Here's a patch for field_ui.api.php

Still need to do a separate patch for field.api.php

EDIT (added): Note that I removed that hook that wasn't being called anywhere. If someone figures out how to use it, they can add it back in then. For now we don't want nonexistent hooks documented...

sun’s picture

Lovely.

jhodgdon’s picture

Does that mean you want to mark it RTBC?

And by the way, separate patch for field.api.php is still to come.

jhodgdon’s picture

FileSize
43.01 KB

Note on field.api.php - the prepare_translation hook section is being worked on separately:
#362021: field_attach_prepare_translation needs to be updated for D7 API

So I decided to split this up further.... I did some (actually a lot of) cleanup on the doc in field.api.php, and put @todo tags in the function bodies for all hooks that need bodies. There are a LOT of them... so I think the hook function bodies should be done in separate patches. Sigh.

This is about to become a meta-issue...

Anyway, this patch cleans up the doc in field.api.php (plus one place in field_storage.module where a comment mentioned the wrong hook name), and is in addition to the patch in #8 for field_ui.api.php. webchick said it was OK to go with separate patches...

Sorry it's so big. The doc was a mess. Sigh.

jhodgdon’s picture

A note on the "forbid" hooks: It looks like the name of the hook was changed from hook_field_update_field_forbid() to hook_field_update_forbid() at some point. So there were two function docs for that hook. I removed one.

sun’s picture

+++ modules/field/field.api.php	2 May 2010 01:13:41 -0000
@@ -131,13 +133,12 @@
+ *     flexibility than field-level settings.  It is recommended to put settings

@@ -148,6 +149,8 @@
 function hook_field_info() {

Double space here. (please use diff -up for rolling patches)

+++ modules/field/field.api.php	2 May 2010 01:13:41 -0000
@@ -308,27 +311,29 @@
- * $entities, $instances and $items parameters are arrays keyed by entity id.
+ * $entities, $instances and $items parameters are arrays keyed by entity ID.
...
- *   Array of entities being displayed, keyed by entity id.
+ *   Array of entities being displayed, keyed by entity ID.
...
  *   Array of instance structures for $field for each entity, keyed by entity
- *   id.
+ *   ID.

There was a time we wrote "id" upper-case everywhere, but at some point we decided that "ID" is not correct. Please don't ask me when or where, but since that happened, most new docs use "id", when referring to some "xyz id".

+++ modules/field/field.api.php	2 May 2010 01:13:41 -0000
@@ -370,8 +378,9 @@
  *   to this array. Each error is an associative array, with the following
  *   keys and values:
- *   - 'error': an error code (should be a string, prefixed with the module name)
- *   - 'message': the human readable message to be displayed.
+ *   - 'error': An error code (should be a string, prefixed with the module
+ *      name).
+ *   - 'message': The human readable message to be displayed.
  */
 function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, &$items, &$errors) {

@@ -594,17 +616,19 @@
- *   - behaviors: (optional) An array describing behaviors of the widget.
- *     - multiple values:
...
- *     - default value:
- *       FIELD_BEHAVIOR_DEFAULT (default) if the widget accepts default values.
...
+ *   - behaviors: (optional) An array describing behaviors of the widget, with
+ *     the following elements:
+ *     - 'multiple values': One of the following constants:
+ *       - FIELD_BEHAVIOR_DEFAULT (default): If the widget allows the input of
...
+ *       - FIELD_BEHAVIOR_CUSTOM: If one single copy of the widget can receive
...
+ *     - default value: One of the following constants:
+ *       - FIELD_BEHAVIOR_DEFAULT (default): If the widget accepts default
...
+ *       - FIELD_BEHAVIOR_NONE: if the widget does not support default values.
...
 function hook_field_widget_info() {

When describing array keys and values, we do not wrap the key/value (before the colon/description) in quotes. At least, in all new docs.

The usage of "(default)" squeezed behind the value looks wrong to me. I'm not sure whether we use "(default)" anywhere else, but if we want to keep using it here, then it should be placed like "(optional)" behind the colon, before the description.

+++ modules/field/field.api.php	2 May 2010 01:13:41 -0000
@@ -1810,40 +1911,25 @@
- *   The type of $entity; e.g. 'node' or 'user'.
+ *   The type of $entity; e.g., 'node' or 'user'.
...
 function hook_field_access($op, $field, $entity_type, $entity, $account) {

wow, as I've seen this in another patch just recently -- is this really proper English grammar? I've never seen this anywhere else...

Especially here, the sentence turns into a punctuation flood; x.x., so replacing with "for example" might be a better idea.

Powered by Dreditor.

jhodgdon’s picture

- Sorry about the diff -up. I use eclipse to make patches on this machine and it doesn't have that option that I've been able to find.
- id vs. ID - my dictionary definitely says ID means identifier/identification, and id is a psychological term (ego, supergo, and id). I believe that is standard English usage. Our doc standards say to use standard English punctuation and usage, and I haven't seen us say to use "id" rather than "ID" in any of our style manuals. If "id" has become the standard, I think it's wrong.
- key in quotes: we're not consistent, but I thought it was merited here because the keys have spaces in them.
- (default) is used all over the place in other doc, but again it's not consistent where it's placed.... I'd be happy if we agreed upon a standard and followed it, and I'm not sure what the standard should be. What are you proposing? I'm happy to adopt anything reasonable as the standard.
- "e.g." means "for example. It should always be followed by a comma. Here's one reference (the bottom example shows the comma in action): http://www.grammarbook.com/punctuation/commas.asp -- I'm happy if it gets replaced by "for example" though.

jhodgdon’s picture

On the id vs. ID thing: http://dictionary.reference.com/browse/id -- they list ID or I.D. as possibilities for identification, and "id" as "the part of the psyche, residing in the unconscious, ..."

I checked several other on-line dictionaries as well -- they all agree.

sun’s picture

wow. Love that punctuation reference. Just learned a lot. :)

So it seems that my issues on "ID" and "e.g." are moot... if we want to be super-nitpicky, then we might want to open a separate issue to fix all docs throughout core regarding them.

Regarding quotes for key/value in lists: Most recent patches changing or adding documentation code removed or didn't contain them. For me, they'd only make sense if the key/value would contain a colon; but as of now, this case does not exist anywhere in core and contrib. Additionally, the API's HTML output is easier to read for me (less punctuation, so visually easier to parse).

Regarding (default), the only reasonable thing we can do is to follow our existing syntax for (optional); i.e., write it after the key + colon, but before the description. btw, totally separate issue: I always wondered whether there is an industry standard for "(optional)" in parameter descriptions. I like our usage, but the amount of developers who need to get used to it, likely means that it's a Drupal-only standard.

jhodgdon’s picture

Status: Needs review » Needs work

OK, it sounds like we have conventions and agreement:

a) Don't put array keys in quotes, when used as lists unless they contain colons (which is unlikely anyway).

b) For default/optional in lists, do:
- key: (optional) The optional something.
- Key: (default) The default something.

c) Follow standard English usage on ID (not id) for identification
d) Follow standard English punctuation on putting commas after e.g. and i.e.

I'll put (a) and (b) into http://drupal.org/node/1354 (doxygen doc) and fix the patch in #11 accordingly, but probably not until Monday. (c) belongs in http://drupal.org/node/338208 if anywhere, but since both c and d are standard English, I really don't think they need to be mentioned anywhere. Maybe (c) because it's been done wrong all over the place...

There's still a separate patch on #8 that needs review, by the way. #13 is needs work.

jhodgdon’s picture

I just took care of putting (a) and (b) in the list section of node 1354 and added (c) to the style guide.

sun’s picture

Status: Needs work » Reviewed & tested by the community

oh, sorry, I thought that #13 would contain #8. #8 is RTBC then.

jhodgdon’s picture

FileSize
46.02 KB

Here's a replacement patch for #11. Changes (to address sun's comments in #13):
- removed double-spaces after periods at end of sentences.
- removed '' around array keys in lists
- replaced e.g. with for example in several spots
- made sure (default) comes after : in lists

To review: sun has marked #8 patch as RTBC. This patch has not yet been reviewed.

HedgeMage’s picture

RTBC with one tiny minor gotcha...

On line 185 of the patch (line 317 of the patched field.api.php), "formatters' " is changed to "formatter's" when it should really be "formatters' " (because the previous line indicates that there may be more than one formatter).

webchick’s picture

Status: Reviewed & tested by the community » Fixed

YEAH!! Great job on this!!

Made that one adjustment and committed to HEAD! :D

jhodgdon’s picture

Status: Fixed » Reviewed & tested by the community

OK... The patch in #8 is separate and RTBC and I don't think it was committed.

Also, you may have noticed all the @todo lines in field.api.php -- a lot of those hook doc functions are still lacking documentation, so this issue is not quite fixed. :) So after #8 is committed, please set back to "active" so we can add function bodies to the hooks. Thanks!

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed #8 to CVS HEAD.

jhodgdon’s picture

Status: Fixed » Active

Putting back to "active" so we can remember we still need to add function bodies to all the hooks in field.api.php. They should all have @todo lines.

jhodgdon’s picture

OK. I'm working on a patch for this now... should be done today or tomorrow.

jhodgdon’s picture

Status: Active » Needs review
FileSize
23.63 KB

Here's a patch that adds function bodies to many of the hooks in field.api.php. Most of these were copied directly from implementations in taxonomy.module, field_ui.module, field_sql_storage.module, etc. A few were adapted slightly from implementations in field test modules.

I didn't do any editing on the ones that were copied from core implementations, but maybe some of them should be edited. Thoughts?

Oh yeah, and I removed the body for hook_field_attach_form() because it was totally wrong (was assuming totally different arguments).

Still missing:
hook_field_attach_delete
hook_field_attach_delete_revision
hook_field_attach_form
hook_field_attach_insert
hook_field_attach_load
hook_field_attach_preprocess_alter
hook_field_attach_presave
hook_field_attach_submit
hook_field_attach_update
hook_field_attach_validate
hook_field_create_field
hook_field_create_instance
hook_field_delete_field
hook_field_delete_instance
hook_field_update_instance
hook_field_storage_pre_load
hook_field_storage_pre_query
hook_field_read_field
hook_field_read_instance

All of these missing hook bodies are for hooks added "just in case" or for API consistency, which currently have no application in core. Any contrib modules we can look at for implementation examples?

joachim’s picture

@#1 -- we have a hook for field formatter settings but we're not using it? O_o

jhodgdon’s picture

This hook was half-way imagined but it was never invoked, so I wouldn't say we "have" one.

joachim’s picture

Is there an issue for this? I couldn't find anything on the hook name. Lack of formatter options was a major stumbling block in D6 and led to TonOfFormatters syndrome. It would be a real shame to have the same problem on D7 for just a lack of a UI.

aspilicious’s picture

Quick style review

+++ modules/field/field.api.php	4 May 2010 19:59:12 -0000
@@ -874,7 +923,41 @@
+    // Iterate through the fieldable entities again to attach the loaded term data.

I think this line exceeds 80 characters...

Powered by Dreditor.

jhodgdon’s picture

RE #31: It is allowed for in-code comments to exceed 80 characters. The 80-character line limit is for doxygen headers only.

RE #30: I have no idea. See comment #1 above - yched may know. My concern is that we don't document a hook that doesn't ever get invoked.

yched’s picture

re #30 - UI for formatter settings : hm, actually there's no dedicated existing issue that I know of.

#553298: Redesign the 'Manage Display' screen is the current issue for the 'display fields' page, but focuses on a different aspect - see comment #104 over there.

sun’s picture

RE #31: It is allowed for in-code comments to exceed 80 characters. The 80-character line limit is for doxygen headers only.

um, no? The 80 characters limit per line for comments applies to all code comments. Somehow, this seems to have gotten lost on the coding standards pages over time. :-7

dhthwy’s picture

It's per line or 80 character limit per the comment? Is that with the // or minus that?

"// Iterate through the fieldable entities again to attach the loaded term data." is actually only 79 characters. And "Iterate through the fieldable entities again to attach the loaded term data." is 76 characters. If the 80 character limit is for the entire line including whitespace then worst case scenario is (depending on the IF block level you're commenting on) you wouldn't have any room for comments at all. Of course you shouldn't have that many levels of IFs, but point stated.

jhodgdon’s picture

80 character limits include whitespace in general.

I think the reasons in #35 are why the coding standard for in-code comments is the same as for code -- we allow wrapping past 80 characters. I think?

jhodgdon’s picture

#27: 675116-somebodies.patch queued for re-testing.

aspilicious’s picture

Still green but I think it's best to wait until #780154: There is no listing API for field API gets in.

This one will need a reroll afterwards.

jhodgdon’s picture

Or that one could wait until this one gets in. That other patch has some style issues (as I just noted there) and needs editing anyway.

aspilicious’s picture

Yeah but that patch changes a function header, so the example needs to be rewritten.
(or maybe I just overlooked it)

jhodgdon’s picture

Hopefully that patch has a new function header in it for whatever it changed.

jhodgdon’s picture

Assigned: jhodgdon » Unassigned
Status: Needs review » Needs work

OK... that issue has gotten in.

So the patch above doesn't appear to apply at all. Needs a complete reroll. If someone wants to take it on, feel free. I may or may not get to it later in the week.

jhodgdon’s picture

Status: Needs work » Needs review

#27: 675116-somebodies.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 675116-somebodies.patch, failed testing.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
23.64 KB

Here is a reroll of the patch in #27, with fix for #31. See comments there.

NOTE: If this patch is accepted, please set status back to "active", since there are still hooks missing function bodies after this patch. This patch just does some of them (the easy ones).

jhodgdon’s picture

#45: 675116-45.patch queued for re-testing.

bleen’s picture

I've been looking at all 24k of this patch for about 30 mins now and everything looks really good. I think someone with some more field api experience still needs to give his/her blessing ... but from my point of view this is a great chunk of much-needed documentation

yched’s picture

Status: Needs review » Reviewed & tested by the community

This looks great, thanks a ton, jhodgdon.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

I agree that this looks good. Committed to CVS HEAD.

bleen’s picture

Status: Fixed » Active

#45 asked this stay active so we can add even more docs via this issue

andypost’s picture

http://drupal.org/cvs?commit=397310 also commited some taxonomy and theme related hunks

jhodgdon’s picture

Status:
- field_ui.api.php looks OK to me now.
- In modules/field/field.api.php there are still some hooks missing function bodies:
hook_field_attach_form
hook_field_attach_load
hook_field_attach_validate
hook_field_attach_submit
hook_field_attach_presave
hook_field_attach_insert
hook_field_attach_update
hook_field_attach_preprocess_alter
hook_field_attach_delete
hook_field_attach_delete_revision
hook_field_storage_pre_load
hook_field_create_field
hook_field_create_instance
hook_field_delete_field
hook_field_update_instance
hook_field_delete_instance
hook_field_read_field
hook_field_read_instance

They all have @todo lines in there as the function body, so it's pretty easy to find them in the file... the harder part will be writing sample function bodies for them. Does anyone know of a place in core/contrib that would have examples?

Damien Tournoud’s picture

Priority: Normal » Critical
Status: Active » Needs work

Duplicating the body of core functions in the example API is a really bad idea. Please stop immediately.

We currently have *the whole* field SQL storage module duplicated in field.api.php. We don't need the duplicate maintenance burden of this. If a core function is the best example for the implementation of a callback of hook, just add a link to there.

Damien Tournoud’s picture

Priority: Critical » Major
Status: Needs work » Needs review
FileSize
17.02 KB

At the very minimum, we need to remove the Field SQL Storage from here. It's a complex beast.

tstoeckler’s picture

Could we use '@see' instead of 'See' to make the functions clickable on api.drupal.org? That way, I think the user 'overhead' (for not having the 'example' code right there) would be minimal.

Damien Tournoud’s picture

@tstoeckler: I think the API document makes any reference to comments clickable... but maybe that only works in the docblock.

jhodgdon’s picture

Any function name followed by () within a docblock /** */ is clickable within api.drupal.org. Any function call in a function body is clickable too. Nothing in code comments // within function bodies is clickable.

So. That aside, the philosophy of the *.api.php hook body functions is to provide, **on that page on api.drupal.org** an example of how your module could implement the hook. If you would like to write different examples, or simplify the functions taken from core down to where they would be better examples, that would be lovely. But just linking to the existing implementation in core is not how the rest of the hook_* documentation is done, and not what we want to do here. So please don't do that. OK?

By the way, I am on a cycling holiday right now on the Danube river, and will be out for the next six weeks, checking email and/or issue queues occasionally, so I'm not up to writing any patches... If someone else wants to write these function bodies, that would be great. But just linking to a different function is not OK.

Damien Tournoud’s picture

God invented hyperlinks, probably for a reason :)

jhodgdon’s picture

Sorry..... We need function bodies for hooks. Real function bodies. That is how we document hooks, until we change the standards, which is a whole different discussion, which you could open as a separate issue. But until and unless that is changed, we need function bodies. Period.

dmitrig01’s picture

Bump. To quote Damien above, "We currently have *the whole* field SQL storage module duplicated in field.api.php. We don't need the duplicate maintenance burden of this. If a core function is the best example for the implementation of a callback of hook, just add a link to there."

gdd’s picture

I agree with jhodgdon that these should stay as they are for D7. Changing them just for the field API hook implementations would be confusing and irritating, and changing them drupal-wide is a discussion that should happen in the D8 cycle.

jhodgdon’s picture

It is definitely too late to change how we document hooks Drupal-wide.

We also don't have a really good way of putting a link inside a function body that will show up as a link on api.drupal.org (see #57), and our current doc standards say that we need sample function bodies in all hook documentation blocks.

So. We need some function bodies. Unless someone wants to step up and write sample functions, let's get this in... any seconds?

webchick’s picture

Agreed that we're not changing the way hook docs are done now. Sounds like a nice patch for some combination of API module/core in Drupal 8, though.

Let's just get 'er done. AFAIK this is the last of the issues we need to close to be at 100% hook documentation compliance.

My only other suggestion would be to see if Examples module has a simpler example that wouldn't duplicate quite as much code. But if not, copy/paste from core and let's close this out.

jhodgdon’s picture

Status: Needs review » Active

As per #63, please ignore Damien's patch in #54 that removes the function bodies for the hooks that had them.

So what we need now is function bodies for the hooks still missing bodies, which are listed in #52... let's see, I'll double check the list. None of these have examples in core that I can tell...

Yes, these hooks are still missing function bodies (all in modules/field/field.api.php):

hook_field_attach_form
hook_field_attach_load
hook_field_attach_validate
hook_field_attach_submit
hook_field_attach_presave
hook_field_attach_insert
hook_field_attach_update
hook_field_attach_preprocess_alter
hook_field_attach_delete
hook_field_attach_delete_revision
hook_field_storage_pre_load
hook_field_create_field
hook_field_create_instance
hook_field_delete_field
hook_field_update_instance
hook_field_delete_instance
hook_field_read_field
hook_field_read_instance

Setting back to "active" since we have no patch for these hooks at this time.

jhodgdon’s picture

Title: Several problems in field.api.php and field_ui.api.php » field.api.php hook documentation is incomplete
jhodgdon’s picture

I've proposed this as a GCI task here:
#948752: Create hook function bodies for Field API hooks

EvanDonovan’s picture

Could we just copy and paste them from core then as webchick suggested in #63? I think Damien's idea made more sense, but if there's no way to create a hyperlink from a docblock, then I guess it's no go for this release.

jhodgdon’s picture

These particular hooks have no examples in core, as far as I know (last time I checked anyway).

The idea from Damien above was about the last set that went in, which were (slightly simplified in some cases) examples from core. He wanted to delete them.

EvanDonovan’s picture

I thought that Damien was saying that field_sql_storage was where core implemented them. Sorry if I misunderstood.

jhodgdon’s picture

Right. field_sql_storage module implemented the hooks added as examples in the patch in #45, which was committed. Damien was trying to get that patch rolled back in his patch in #54. It's confusing. You need to read the entire thread starting after #45 to understand the discussion.

EvanDonovan’s picture

Oh, I understand now. I knew that Damien wanted to remove from api.php & cross-reference the core implementations, but I didn't realize that the hooks from #64 had no example implementations in core. Sorry for creating confusion in my efforts to help...

jhodgdon’s picture

Issue tags: +gci-docs, +gci-medium, +gci-code, +gci-task

This issue has been posted as a Google Code-In task.
http://www.google-melange.com/gci/task/show/google/gci2010/drupal/t12904...

rschwab’s picture

Subscribination!

catch’s picture

Category: bug » task
Issue tags: -gci-docs, -gci-medium, -gci-code, -gci-task +Needs backport to D7

Moving to a task. Not clear if this was fixed in other issues?

jhodgdon’s picture

Version: 7.x-dev » 8.x-dev
Category: task » bug
Priority: Major » Normal

This is a documentation bug. I'm willing to have it not marked "major", and I understand that we don't want to hold up progress based on this issue not being fixed, but let's leave it as a bug. (It's a bug that this documentation is missing that should be there.)

It looks like the list in #64 is still correct. I may not have checked all of them, but at least most of them still have
// @todo Needs function body.
as their function body.

jhodgdon’s picture

Version: 8.x-dev » 7.x-dev
Issue summary: View changes
Issue tags: -Needs backport to D7

These hooks do not exist in 8 any more. Most of them still need function bodies in 7.

  • webchick committed 3f28263 on 8.3.x
    #675116 by jhodgdon, sun: Fixed Several problems in field.api.php and...
  • Dries committed 6ce1ae7 on 8.3.x
    - Patch #675116 by jhodgdon: documentation improvements.
    
    
  • Dries committed 8a3098d on 8.3.x
    - Patch #675116 by jhodgdon: several problems in field.api.php and...

  • webchick committed 3f28263 on 8.3.x
    #675116 by jhodgdon, sun: Fixed Several problems in field.api.php and...
  • Dries committed 6ce1ae7 on 8.3.x
    - Patch #675116 by jhodgdon: documentation improvements.
    
    
  • Dries committed 8a3098d on 8.3.x
    - Patch #675116 by jhodgdon: several problems in field.api.php and...

  • webchick committed 3f28263 on 8.4.x
    #675116 by jhodgdon, sun: Fixed Several problems in field.api.php and...
  • Dries committed 6ce1ae7 on 8.4.x
    - Patch #675116 by jhodgdon: documentation improvements.
    
    
  • Dries committed 8a3098d on 8.4.x
    - Patch #675116 by jhodgdon: several problems in field.api.php and...

  • webchick committed 3f28263 on 8.4.x
    #675116 by jhodgdon, sun: Fixed Several problems in field.api.php and...
  • Dries committed 6ce1ae7 on 8.4.x
    - Patch #675116 by jhodgdon: documentation improvements.
    
    
  • Dries committed 8a3098d on 8.4.x
    - Patch #675116 by jhodgdon: several problems in field.api.php and...

  • webchick committed 3f28263 on 9.1.x
    #675116 by jhodgdon, sun: Fixed Several problems in field.api.php and...
  • Dries committed 6ce1ae7 on 9.1.x
    - Patch #675116 by jhodgdon: documentation improvements.
    
    
  • Dries committed 8a3098d on 9.1.x
    - Patch #675116 by jhodgdon: several problems in field.api.php and...