Index: modules/field/field.crud.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/field/field.crud.inc,v retrieving revision 1.71 diff -u -p -r1.71 field.crud.inc --- modules/field/field.crud.inc 29 Sep 2010 01:37:02 -0000 1.71 +++ modules/field/field.crud.inc 23 Oct 2010 08:36:32 -0000 @@ -583,14 +583,10 @@ function field_read_fields($params = arr */ function field_delete_field($field_name) { // Delete all non-deleted instances. - $field = field_info_field($field_name); - if (isset($field['bundles'])) { - foreach ($field['bundles'] as $entity_type => $bundles) { - foreach ($bundles as $bundle) { - $instance = field_info_instance($entity_type, $field_name, $bundle); - field_delete_instance($instance); - } - } + $field = field_read_field($field_name, array('include_inactive' => TRUE)); + $instances = field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE)); + foreach ($instances as $instance) { + field_delete_instance($instance); } // Mark field data for deletion. @@ -931,7 +927,7 @@ function field_delete_instance($instance ->execute(); // Mark instance data for deletion. - $field = field_info_field($instance['field_name']); + $field = field_read_field($instance['field_name'], array('include_inactive' => TRUE)); module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance); // Clear the cache. @@ -1024,13 +1020,17 @@ function field_delete_instance($instance * The maximum number of field data records to purge before returning. */ function field_purge_batch($batch_size) { - // Retrieve all deleted field instances. We cannot use field_info_instances() - // because that function does not return deleted instances. - $instances = field_read_instances(array('deleted' => 1), array('include_deleted' => 1)); + // Retrieve all deleted field instances, regardless its active status. + // A field is inactive when the module providing with its field type has + // been disabled. + $instances = field_read_instances(array('deleted' => 1), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); foreach ($instances as $instance) { // field_purge_data() will need the field array. - $field = field_info_field_by_id($instance['field_id']); + $field = field_read_field($instance['field_name'], array('include_deleted' => TRUE, 'include_inactive' => TRUE)); + if (empty($field['storage']['active'])){ + continue; + } // Retrieve some entities. $query = new EntityFieldQuery(); $results = $query @@ -1056,10 +1056,13 @@ function field_purge_batch($batch_size) } // Retrieve all deleted fields. Any that have no bundles can be purged. - $fields = field_read_fields(array('deleted' => 1), array('include_deleted' => 1)); + $fields = field_read_fields(array('deleted' => 1), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); foreach ($fields as $field) { // field_read_fields() does not return $field['bundles'] which we need. - $field = field_info_field_by_id($field['id']); + $field = field_read_field($field['field_name'], array('include_deleted' => TRUE, 'include_inactive' => TRUE)); + if (empty($field['storage']['active'])){ + continue; + } if (!isset($field['bundles']) || count($field['bundles']) == 0) { field_purge_field($field); } Index: modules/field/modules/field_sql_storage/field_sql_storage.module =================================================================== RCS file: /cvs/drupal/drupal/modules/field/modules/field_sql_storage/field_sql_storage.module,v retrieving revision 1.56 diff -u -p -r1.56 field_sql_storage.module --- modules/field/modules/field_sql_storage/field_sql_storage.module 6 Oct 2010 13:57:47 -0000 1.56 +++ modules/field/modules/field_sql_storage/field_sql_storage.module 23 Oct 2010 08:36:32 -0000 @@ -652,7 +652,7 @@ function field_sql_storage_field_storage */ function field_sql_storage_field_storage_delete_instance($instance) { $etid = _field_sql_storage_etid($instance['entity_type']); - $field = field_info_field($instance['field_name']); + $field = field_read_field($instance['field_name'], array('include_inactive' => TRUE)); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_update($table_name) Index: modules/field/tests/field.test =================================================================== RCS file: /cvs/drupal/drupal/modules/field/tests/field.test,v retrieving revision 1.42 diff -u -p -r1.42 field.test --- modules/field/tests/field.test 28 Sep 2010 02:30:31 -0000 1.42 +++ modules/field/tests/field.test 23 Oct 2010 08:36:32 -0000 @@ -1972,11 +1972,13 @@ class FieldCrudTestCase extends FieldTes function testDeleteField() { // TODO: Also test deletion of the data stored in the field ? - // Create two fields (so we can test that only one is deleted). + // Create three fields (so we can test which is deleted). $this->field = array('field_name' => 'field_1', 'type' => 'test_field'); field_create_field($this->field); $this->another_field = array('field_name' => 'field_2', 'type' => 'test_field'); field_create_field($this->another_field); + $this->inactive_field = array('field_name' => 'field_inactive', 'type' => 'test_field'); + field_create_field($this->inactive_field); // Create instances for each. $this->instance_definition = array( @@ -1988,10 +1990,21 @@ class FieldCrudTestCase extends FieldTes ), ); field_create_instance($this->instance_definition); + $this->another_instance_definition = $this->instance_definition; $this->another_instance_definition['field_name'] = $this->another_field['field_name']; field_create_instance($this->another_instance_definition); + $this->inactive_instance_definition = $this->instance_definition; + $this->inactive_instance_definition['field_name'] = $this->inactive_field['field_name']; + field_create_instance($this->inactive_instance_definition); + + // Simulate an inactive field. + db_update('field_config') + ->fields(array('active' => 0)) + ->condition('field_name', $this->inactive_field['field_name']) + ->execute(); + // Test that the first field is not deleted, and then delete it. $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE)); $this->assertTrue(!empty($field) && empty($field['deleted']), t('A new field is not marked for deletion.')); @@ -2021,6 +2034,17 @@ class FieldCrudTestCase extends FieldTes $another_instance = field_read_instance('test_entity', $this->another_instance_definition['field_name'], $this->another_instance_definition['bundle']); $this->assertTrue(!empty($another_instance) && empty($another_instance['deleted']), t('An instance of a non-deleted field is not marked for deletion.')); + // Make sure that the inactive field is marked as deleted when it is + // specifically loaded. + field_delete_field($this->inactive_field['field_name']); + $field = field_read_field($this->inactive_field['field_name'], array('include_deleted' => TRUE, 'include_inactive' => TRUE)); + $this->assertTrue(!empty($field['deleted']), t('A deleted inactive field is marked for deletion.')); + + // Try to load the instance of the inactive field normally and make sure + // it does not show up. + $instance = field_read_instance('test_entity', $this->inactive_instance_definition['field_name'], $this->inactive_instance_definition['bundle']); + $this->assertTrue(empty($instance), t('An instance for a deleted inactive field is not loaded by default.')); + // Try to create a new field the same name as a deleted field and // write data into it. field_create_field($this->field); @@ -2832,6 +2856,12 @@ class FieldBulkDeleteTestCase extends Fi $id++; } } + + // Simulate an inactive field. + db_update('field_config') + ->fields(array('active' => 0)) + ->condition('field_name', 'bf_2') + ->execute(); } /**