Hi, I'm not sure what the purpose of the first argument is, because bundle is used as the type of the object for the field API,
field_info_fields is declared as: field_info_fields($bundle_type = NULL, $field = NULL, $field_type = NULL)
the parameter $bundle_type is used later for as the $object argument to call field_info_instances:
..
$instances = field_info_instances($bundle_type);
..
the documentation of field_info_fields says:
$bundle_type (optional) The bundle type on which to filter the list of fields. In the case of nodes, this is the node type.
So I guess the correct usage should be:
..
$instances = field_info_instances(NULL, $bundle_type);
..
Comments
Comment #1
ilo commentedChanged to support as suggested by webchick
Comment #2
yched commentedHm, field_info_fields() was modified as part of #560780: Add Image Field to image.module, which I couldn't review properly.
The signature is buggy at least. We talk about object (or entity) types, and bundles, but not about bundle types...
According to the function, it looks like this should in fact read $obj_type : 'node', 'user', 'comment'...
Comment #3
ilo commentedAccording to the doc line, the usage is intented to get the list of fields from a bundle:
$bundle_type (optional) The bundle type on which to filter the list of fields. In the case of nodes, this is the node type.
Don't know if this helps or not :)
Comment #4
yched commentedDoh, that's in fact a clash with #470242: Namespacing for bundle names. That patch changed the field_info_instances() signature to take ($obj_type, $bundle) instead of just ($bundle) previously, and that was not mirrored in #560780: Add Image Field to image.module.
So, in the current state of the function, the first arg of field_info_field() works as a $obj_type. No code currently calls field_info_fields() with that 1st arg, though.
The whole function needs refactoring though.
Comment #5
decipheredSubscribe. Just hit this particular road block.
Comment #6
arithmetric commentedI ran into this bug too and took a stab at a fix for it.
As yched said, it seems like when the arguments of field_info_instances() were changed to include $obj_type, this change was not carried over to field_info_fields().
My patch adds $obj_type as an optional argument for field_info_fields(). This allows field_info_fields() to return the fields of an object type (i.e. fields on all nodes) or of a bundle type (i.e. fields on article-type nodes).
The patch also updates the only two uses of field_info_fields() within Drupal core: in file_get_file_reference_count() and file_get_file_references().
Comment #7
dries commentedThis looks good to me, but would be nice to get sign off from yched. Marking it RTBC.
Comment #8
yched commentedWell, IMO those 'limit by object type, or bundle, or field type, or several of the above' features don't belong in field_info_fields() to begin with.
At some point during #560780: Add Image Field to image.module I suggested that some helper function used to filter fields by field types might be merged with field_info_fields(), but adding more criteria is bloat. We're reading from a cached array of field definitions, not writing a pseudo query builder.
The current implementation has several flaws / holes, so if we go that route, we'd need a battery of tests to make sure we caught all combinations - I don't really volunteer for writing those ;-)
So I'd rather go back to where field_info_fields() was before the imagefield patch: just get the list of all active fields on the site. It's easy enough for field_info_fields() callers to filter the results by whatever criteria makes sense for them.
In the end, File field / image field only need a subset of the conditions (limit to a specific field, limit to a specific field type) in *one* function, file_get_file_reference_count(), so I moved that logic back to there.
Comment #10
yched commentedTurns out field_info_fields(some conditions) was called in two places in file.module, dunno why I missed the 2nd on my initial search.
That patch fixes both places.
Comment #11
yched commentedMarked #637870: field_info_fields() does not work as documented and #637342: Missing param in field_info_fields() as duplicates.
Comment #12
matt2000 commented---scribe
sub
Comment #13
matt2000 commentedMoving solution candidate from duplicate #637870: field_info_fields() does not work as documented.
Maybe this is not the right way to go, but I'd at least like to know why, since this is the only proposed solution that actually provides the documented functionality, as opposed to removing functionality.
#6 might be better, now that I understand that bundles aren't actually supposed to exists across entities. My patch is admittedly a lot of extra weight for a purely theoretical usage.
#10 has the advantage of getting rid of ugly code like theFunction(NULL, NULL, $stuff) which is a win.
Perhaps a best-of-both-worlds would be something that supports the form
field_info_fields(array('bundle_type' => 'article', 'entity_type' => 'node'))?So I'd be happy to see any of these in, but I don't like the idea of decreasing the usefulness of a tool if we don't need to.
Comment #14
yched commentedfrom my #8:
if we go that route, we'd need a battery of tests to make sure we caught all combinations - I don't really volunteer for writing those ;-)
Makes no sense, a bundle doesn't appear in several entity types. They might have the same name, but that doesn't make them the same bundle.
I still stand by the optinion that field_info_fields() should be what it originally was. I'm sorry that I let this rot until post freeze and it appears we're removing features, but that 'filtering' feature just doesn't belong in there.
I'm on crack. Are you, too?
Comment #15
amitaibuI agree that an info function shouldn't do filtering, although it would be nice to have a follow up patch to add such a utility function.
Minor - Do we really need to support the field name? I personally don't like ambiguous arguments that can be either array or a string.
Why are we checking
empty($field_type)- is there a use case for that?I'm on crack. Are you, too?
Comment #16
yched commentedre #15
I don't either, but that's how filefield / imagefield were coded, changing that would be a different issue for which quicksketch's opinion is needed.
file_field_delete_file() calls file_get_file_reference_count() without a $field_type param. That's because you want to react to a file being deleted in all fields that refer to it, be it image or file field.
Comment #17
damien tournoud commentedWe are actually in needs work. As yched noted, this makes very little sense:
Bundle names are not global, they depend on the object/entity type.
Comment #18
damien tournoud commentedI agree that #10 is the way to go. The only minor issue I have with it is the API inconsistency (not introduced by this patch), between the $field argument of file_get_file_reference_count() (a string or an array) and file_get_file_references() (an array). We really need to fix those things, but this is not in the scope of this patch.
RTBCing #10.
Comment #19
dries commentedCommitted #10. Thanks all.
Comment #22
yched commented#21 is spam. Funny.