As it is implemented right now, field_info_field() will return an array (rather than FALSE only if the field in question is 'active'. The documentation does not reflect this.

(field_info_field uses _field_info_collate_fields, which in turn calls field_read_fields without 'include_inactive' => TRUE in the second argument.)

This should also be documented in the documentation of _field_info_collate_fields.

CommentFileSizeAuthor
#15 998048-field-doc.patch690 bytesjhodgdon

Comments

jhodgdon’s picture

Status: Active » Postponed (maintainer needs more info)

Ummm... What are you saying needs to be documented? I'm confused.

sven.lauer’s picture

Sorry for not being clear:

If you give file_info_field the name of a field that is inactive (but not deleted), the function will return nothing.

As of now, the documentation only states "$field_name can only refer to a non-deleted field." which to me suggests that it could refer to a non-deleted, but inactive field.

So, I guess the sentence should be changed to "$field_name can only refer to a non-deleted, active field."

It might be that the original intent was to return a field array also for inactive fields (for then, the function can be used to check if a certain field exists before attempting to create it). If that is the case, the implementation needs to be changed.

jhodgdon’s picture

Title: Documentation problem with field_info_field » field_info_field only works for active fields.
Component: documentation » field system
Status: Postponed (maintainer needs more info) » Active

Thanks...

So, I guess the sentence should be changed to "$field_name can only refer to a non-deleted, active field."

That sounds like the right change to the documentation. At this point, I don't think we will change the function behavior. But let's see if we can get one of the Field maintainers to comment.

rfay’s picture

subscribe.

I'm no expert at this, but am certainly a hater of inactive fields :-) This issue probably goes more deeply into the heart of the "Drupal doesn't really know what to do with inactive fields" territory than just a docs change on this function.

yched’s picture

That's right, field_info_field() only returns active, non-deleted fields - that's the intended behavior.

sven.lauer’s picture

Okay, then it is only the documentation that needs to be updated.

However, given this intended behavior, field_delete_field only deletes instances of fields that are active, not those of fields that are inactive.

Calling field_delete_field on an inactive field will mean that the field itself will be marked for deletion, but not its instances. Also, unless I am very much mistaken, the data for the field will also not get marked for deletion. This is so because field_delete_field uses field_info_field to figure out what the storage engine is, and what bundles have instances. Since, on an inactive module, field_info_field will return void, the isset tests in field_delete_field will evaluate to FALSE.

So if the field to be deleted is active, field_delete_field will mark the field for deletion, as well as all its instances and data. If the field is inactive, only the field will be marked for deletion. This seems like inconsistent behavior to me, and should at least be documented.

Given the current behavior, deleting an inactive field will always result in orphaned instances/data---is that really what is intended? For one thing, it makes field_delete_field pretty useless in uninstall hooks (where the fields will always be inactive, because the module will be deactivated first). Maybe I am missing something, or am trying to use field_delete_field wrongly.

jhodgdon’s picture

What does "inactive" mean for a field??? I thought probably it meant it had no instances, but maybe not. Maybe this should also be documented somewhere?

rfay’s picture

IMO this is a perpetual problem with fields, because no adequate solution to inactive fields has ever been chosen. See D6 issues #863226: UI element to delete or re-activate inactive field instances and #649298-48: Features install/revert does not remove additional fields from content type already stored in the db

jhodgdon’s picture

What does "inactive" mean then?

And what about #6?

sven.lauer’s picture

Hm, I do understand why a function like the current field_info_field can be useful for certain things, but it seems to be a really bad idea to use it in something like field_delete_field ... essentially that means that there is no way (except directly going into the database) for a module to delete its fields on uninstall. Or at least, to do so without orphaning instances and data. That is not too bad (==does not directly break) if the field in question does not have its own table, but if it does, re-activating the module will throw an error for trying to create a table that already exists.

As it stands, at least one core module (comments) uses field_delete_field and so orphans it's instances on uninstall.

yched’s picture

Fields are inactive if the module that defines the field type is disabled, or if the module that defines their storage backend is disabled.

Disabled fields have always been a massive pain to deal with, because, the module being disabled, there's really not much we can do with them.

I agree field_delete_field() is currently not really bulletproof when called on an inactive field. Care to open a separate issue ?

sven.lauer’s picture

I was about to open a separate issue when I found #943772: field_delete_field() and others fail for inactive fields.

There is a patch, but no activity since October 23 ...

jhodgdon’s picture

OK. Barring that other issue making changes to field_info_field(), it sounds like we should at least fix the doc for that function so that it reflects what the function does, which is operate only on active, non-deleted fields. As suggested by Sven earlier:

$field_name can only refer to a non-deleted, active field.

Should we go ahead and make that doc change?

sven.lauer’s picture

I doubt that the other issue will make a change to field_info_field() ... the point of this function really seems to be to only return fields that "you can do something with". The patch in this issue basically avoids using field_info_field() for the deletion stuff, which is as it should be, in my view.

Maybe we should also add a reference to field_read_fields() as a means to also get deleted and/or inactive fields?

jhodgdon’s picture

Title: field_info_field only works for active fields. » field_info_field only works for active fields - document that
Status: Active » Needs review
StatusFileSize
new690 bytes

OK. Here's a proposed doc patch for this issue.

yched’s picture

Status: Needs review » Reviewed & tested by the community

Works for me. Thanks !

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)

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