Problem/Motivation
The Field API Data Structures page describes the $field structure, but most functions which take such a parameter do not reference this page, and many of them describe the parameter as a "field array" or "field data" or even simply "field". These descriptions should be made consistent, and the functions taking such a parameter should reference the Field API Data Structures page.
Proposed resolution
A patch has been submitted which ensures that every relevant @param $field describes its parameter as a "field structure" and also links to the Field API Data Structures page.
Remaining tasks
- The patch should be reviewed for correctness.
- The term "field structure" is confusing to some. Core developers should choose a less confusing term.
- As noted over two years ago, the field type documentation should specify which of the hooks listed in
field.api.phpare required to be implemented, and which are optional.
User interface changes
None; this is a documentation-only patch/fix.
API changes
None; this is a documentation-only patch/fix.
Original report
API page: http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
The documentation for the $field parameter gives absolutely no clue as to what is expected for this parameter.
Possibilities include:
- A string containing the name of the field.
- The field definition structure as returned from
field_info_field()
Comments
Comment #1
jhodgdonDo you know which is the actual value? Care to research and submit a patch?
Comment #2
pillarsdotnet commentedNone of the
hook_field_is_empty()implementations in core do anything with their$fieldparameter.Searching through contrib now, but it will take a while, and I'm not sure that if I did find anything it would be useful as a standard.
When in doubt, do as the most popular contrib module does?
Comment #3
pillarsdotnet commentedExample
hook_field_is_empty()implementations which do not ignore their$fieldparameter:commerce_customer_field_is_empty()computed_field_field_is_empty()date_field_is_empty()Conclusion:
It would appear that
$fieldis expected to be an array structure as returned fromfield_info_field().Tentatively rolling a patch that describes the consensus implementation, even though there is considerable disagreement among these three examples:
The
commerce_customer_field_is_empty()function returnsNULLif$fieldis not the expected type.So
commerce_customer_field_is_empty($item, NULL)would returnNULL.The
date_field_is_empty()function always returnsFALSEif$itemis not an array.So
date_field_is_empty(NULL, $field)would returnFALSE.The
computed_field_field_is_empty()function returns TRUE if $item is not of the expected numeric field type.So
computed_field_field_is_empty(NULL, $field)would returnTRUE.All other examples not shown implicitly assume that
$itemis an array of the expected field type, and many will produce PHP warnings if this assumption is violated.So
hook_field_field_is_empty(NULL, $field)would (in most cases) produce a run-time warning.None of the Field System hooks currently state whether or not they are required. I know that
hook_field_is_empty()is required because of comment #517430-7 by bjaspan and also because of the code and comment in_field_filter_items(). I don't know which, if any, of the other field hooks are required. This documentation bug should be reported and addressed in a separate (meta?) issue, as bjaspan suggested over three years ago.Comment #4
pillarsdotnet commentedComment #5
pillarsdotnet commentedComment #6
jhodgdonA few things need fixing here:
a) The first line of a hook definition has a different verb requirement. Check http://drupal.org/node/1354 for standards.
b)
I don't think this is actually a "field instance". That term has a special meaning in the field API. I think this is just the field data?
c)
I'm not comfortable with calling this the "field type". Most things that in Drupal are called "type" are strings, such as "node type". Please use the term that is used in the rest of the field API... field definition array? not sure.
d)
I'm not happy with saying "undefined" for the return value. We should not express our doubts about what to return, we should document what should be returned. Anyway, the field API is not so stupid that it will invoke this hook with $item not being an instance of type $field, so we don't really have to mention this case at all. So this can be quite simplified. Mention in @param $item that it is an instance of the field, and leave out the "is an instance of ..." in the @return.
Also, "empty of data" ... That's not actually accurate. $item could contain *some* data and still be considered empty (example: an image field that has alt text but no uploaded file). Which is why the old wording was "considers", which is accurate. Please fix this to be accurate; you can reword if you want, but it needs to be clear that the module can decide what "empty" means for this field. You can put it in the description if you want.
e) Also... is this hook actually required? In CCK at least, there was a default way to check if a field was empty, and modules just needed to implement the is_empty hook if they needed to override that. Isn't that possible for fields?
Comment #7
pillarsdotnet commented@#6 by jhodgdon on October 6, 2011 at 3:48pm:
Corrected and re-rolled. Also:
If these functions should never be called from userland, they should say so, and I need to remove the call to
og_field_is_empty()from my contrib module. (Which, BTW, is what prompted me to file this issue in the first place.)Also note the undocumented fact that
field_info_field()may returnNULL.As previously stated:
Follow the links if you want; that's why I wrote them.
(Webchick thinks I should spend more time composing my comments, but do people even bother to read them, before replying?)
Comment #8
pillarsdotnet commentedBAH!!!!
That patch file name was completely wrong, even though the contents are (I believe) correct.
Comment #9
jhodgdonPlease, there is no need to be so snarky! It would be much nicer if you had just said "Actually, it is required" and provided the link politely. I obviously just reviewed your patch and didn't re-scan all the comments on this issue, or have them all in my head (I am involved in a lot of other issues obviously), or read all the other code in the Drupal code base.
So looking at that _field_is_empty function, it has a suspicious line that just says function_exists() but does nothing with the return value. Given that, I'm not sure what the intention is about whether the is_empty() hook should be required or not, but we should resolve that before we proceed to document that it's required. IMO the comment referenced on the other issue does not resolve the question, it merely says "if it's required, we should document it" and "if it's not required, we should test in the code and return". It doesn't answer the question of which is correct. And that issue was eventually closed as having been fixed elsewhere, so there is no answer.
One more thing, maybe "An array of field data that may or may not be considered empty." would be clearer as "Data for a single field value to test." or something like that? "An array of field data" makes it sound like it could be multiple field values together (it can only be one at a time), and "may or may not be considered" seems a bit redundant?
Comment #10
pillarsdotnet commentedWell, if
hook_field_is_empty()is not required, then the following code from_field_filter_items()certainly needs to be changed:Note that the "suspicious call to
function_exists()" was removed (today) by webchick.See: #1300482: Remove cruft function_exists() from _field_filter_items() leftover from the (removed) function registry.
Sorry to be so "snarky", but if you are unwilling to read what I wrote the first time, and are unwilling to read what I wrote the second time, I'm not overly anxious to write it a third time.
The people who developed the field API were crystal-clear that it should be required. If you're unwilling to read their testimony (linked above and quoted in #1300482) then go ask them yourself.
Comment #11
pillarsdotnet commentedRevised as requested.
Comment #12
jhodgdonLooks good now. Thanks for the clarifications. Should be good for 8.x/7.x.
Comment #13
chx commentedAn overwhelming majority of @param $field says "field structure". Just inside the field directory, there are 15 of "The field structure for the operation.", 5 simply is "A field structure." and 4 is "The field structure." However, 3 is "The field definition."
Not to mention that reading the doxygen field_info_field doesnt make you any wiser. You wanted http://api.drupal.org/api/drupal/modules--field--field.module/group/field/7 this. I do not know how to link to a group page from doxygen.
I am surprised by the opening post because there is absolutely no place in field API where such ambiguity exists undocumented. As far as I know only EFQ allows you to pass in both field names and field structures and that is clearly documented even if it says "Either a field name or a field array" and not "field structure", it's not ambigous.
I would recommend agreeing on "field structure" then go over all of the 15 mentions of "field array" in core and also the @params that do not mention the magic word structure.
Comment #14
pillarsdotnet commented@chx: Since you're basically asking me to review and/or rewrite the entire modules/field/field.api.php file, may I get (from you or someone else) a list of the field hooks which, like
hook_field_is_empty(), are required of all field type implementations?I figured it out. I just need to add the following to the end of the function header:
Comment #15
pillarsdotnet commentedWhen I find exceptions to the general rule that
@param $fieldrefers to a field structure, should I submit a patch to rename the parameter?DrupalWebTestCase::assertField()DrupalWebTestCase::assertNoField()Comment #16
lars toomre commentedThis is a perfect example of why we should be type hinting @param directives. Shouldn't this patch correct/modify each docblock touched for @param type hinting?
Comment #17
lars toomre commentedAlso line '+ * @ingrou pfield' needs to be corrected. I believe you meant '@ingroup field'.
Comment #18
pillarsdotnet commented@#16 by Lars Toomre on October 7, 2011 at 5:21am:
No, the issue discussing type-hinting still lacks sufficient "core developer buy-in".
See #711918-69:
Comment #19
pillarsdotnet commented@#17 by Lars Toomre on October 7, 2011 at 5:26am:
Thanks. Corrected.
Comment #20
lars toomre commentedI see that there has been no progress on #711918: Documentation standard for @param and @return data types over the last four days. Maybe this patch should be postponed until that issue is settled one way or another? This really is an example that screams for type hinting.
Comment #21
pillarsdotnet commented@#20 by Lars Toomre on October 7, 2011 at 5:41am:
Sure; why not. Updated issue summary.
Comment #22
salvisI'm confused by the term "field structure" when used to describe an actual instance of field data. PHP has objects (classes) and arrays, but not structures, in the way that term is commonly used in other programming languages. Classes where all properties are public may be called "structures" in data structure terminology, but I don't think this is what we mean here. What we mean is keyed arrays with a certain defined set of keys, but they are still just "arrays", not "structures", because there is no language support for assuring the presence of those keys.
If we go for type hinting on @params (which I hope very much), it'll be just 'array', which, unfortunately, doesn't add a lot of information in this case.
Comment #23
pillarsdotnet commented@#22 by salvis on October 7, 2011 at 7:31am:
Actually, "field structure" or
$fieldrefers to the definition of the field type.A "field instance" or
$instancedescribes the usage of that field type within a content type or "bundle".A "field item" or
$itemrefers to the data contained in a field within a particular node or entity object.See the Field API page.
Comment #24
pillarsdotnet commentedAdded a couple of missing end-of-sentence dots.
Comment #25
jhodgdonParam/return type hinting itself has already been adopted as a mostly optional standard. There is no reason to postpone this issue until it is decided -- as stated in a comment above, this seems like a good time to use it, to disambiguate some documentation, so please go ahead. :)
I agree with #22 - "structure" is an ambiguous term in PHP. It could say "field structure array", though, because "field array" would also be ambiguous (we need to get across the idea that it's the definition, not the data)... hmmm. maybe "field definition array" would be even better? thoughts?
Comment #26
pillarsdotnet commentedAccording to chx, there is absolutely no ambiguity in the term "field structure", especially when used with
@param $field. Therefore, according to coding standards, type-hinting should not be used.But since discussions about coding standards are a complete mis-use of time, energy, and focus, I'll wait until y'all decide what to do before I spend another two hours hand-editing code.
Comment #26.0
pillarsdotnet commentedCreate issue summary for what is no longer a trivial issue.
Comment #26.1
pillarsdotnet commentedUpdated status.
Comment #27
jhodgdonI think chx is wrong about their being no ambiguity in the term "field structure", and so does salvis (see #22). If even a few people think it is confusing, couldn't we pick a better term that wouldn't confuse anyone?
Comment #28
pillarsdotnet commented@#27 by jhodgdown on October 7, 2011 at 3:54pm:
Please re-define the word we so that it does not include pillarsdotnet, or else magically unsubscribe me from this issue.EDIT: updated summary so that "we" is defined as "Core developers".
Comment #29
salvisI think my confusion comes from the ambiguity between
1)
@param $fieldis of type "field structure" and2)
@param $fieldis of type array and contains the field structurebecause I'm used to thinking "data type" when I see "structure".
Making it
@param array $fieldwould go a long way to clear this up for me.
Comment #30
pillarsdotnet commentedWaiting for chx or another Field API maintainer to chime in on this one.
Comment #31
tim.plunkettUn-postponing for more visibility.
Comment #31.0
tim.plunkettwordsmithing
Comment #40
quietone commentedThis discussion and patch here are relevant for Drupal 7. IF there is a need for this documentation change in Drupal 9 new issues should be opened to discuss that.
Therefor moving to the Drupal 7 issue queue.