In node_example_uninstall(), each field defined by the module is looped over and deleted as follows:

  foreach (array_keys(node_example_installed_fields()) as $field) {
    field_delete_field($field);
  }

Then, each field instance is looped over and deleted:

$instances = field_info_instances('node', 'node_example');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }

However, field_delete_field deletes not only a field (as its name might suggest) but marks "a field and its instances and data for deletion." ( http://api.drupal.org/api/function/field_delete_field/7 )
Therefore, any instances of fields defined by this module will already have been marked for deletion by field_delete_field(), and there's nothing for field_delete_instance() to do.

The exception to this would be if the node_example node type had an instance of a field defined by another module (e.g. if the user had added a field via the "Manage Fields" page at admin/structure/types/manage/node_example/fields ). In which case the instance of that field should be deleted before the example node type itself is deleted.
If this is the purpose then I think a comment to this effect would be helpful, since at the moment the code reads as if the field_delete_instance() call is necessary to delete the instances defined within the node_example module itself (which it is not).

CommentFileSizeAuthor
#2 node_example_746266_uninstall.patch1.27 KBtanoshimi

Comments

cyberswat’s picture

subscribing

tanoshimi’s picture

Status: Active » Needs review
StatusFileSize
new1.27 KB

Thinking about this some more, I believe that the code to uninstall the node_example node type is correct, but it's the comments that are wrong.

The reason for calling field_delete_field is to delete both the fields defined by the module (i.e color, quantity, and image) and also any instances of those fields - attached to either the node_example content type, or any other bundles.

However, we also need to delete instances of any other fields that have been attached to the node_example type prior to deleting it. Seeing as this node type has a body field, a failure to do this would lead to an orphaned instance of body (and possibly other fields, such as comment) being left behind in field_config_instance after the node_example node type had been deleted.

I can't think why you'd ever want to delete a node type without deleting all field instances attached to it first, but this doesn't appear to be automatically done by node_type_delete(). So, if you want to ensure you don't get orphaned fields, you should call field_delete_instance() for all fields attached to a node type before calling node_type_delete(), as this module does.

Attached patch hopefully clarifies the comments explaining each step in the uninstall.

cyberswat’s picture

Status: Needs review » Reviewed & tested by the community

@tanoshimi Thanks for following up with this and clarifying the comments.

rfay’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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