With Drupal's features its easy to deploy new or altered node-types, amongs others.

Adding fields to a node-type is a walk in the park. But what about removing/deleting fields?

The answer is, you cant. At least, not with exported feature code. But Drupal wouldn't be Drupal if there whas n't a fix for this. The "secret" is to use hook_update in a module's .install file. And since a feature is a module, why not put it there.

Hard removal of fields

This delete's a field completele, erasing all data.

  // Destroy field "field_some_name"
  $field_name = "field_some_name";
  if (field_info_field($field_name) !== NULL) {
    field_delete_field($field_name);
  }

Soft removal of fields

This only dispatches a field from a certain node-type.

  // Remove "field_some_name" from node-type "some_ctype"
  $instance = field_info_instance('node', 'field_some_name', 'some_ctype');
  field_delete_instance($instance, FALSE);

Delete fieldgroups

This deletes a specified fieldgroup from a certain node-type.

  // Remove "group_some_name" from node-type "some_ctype"
  $group = field_group_load_field_group('group_some_name', 'node', 'some_ctype', 'form');
  field_group_group_export_delete($group, FALSE);

Other entity types

What about entity types other than "node"? Looking at the example code above it might be clear that this can apply on other types too. Just swap the "node" defenition in "field_info_instance()" and "field_group_load_field_group()" for another.

This applies on Drupal7.

Add new comment