I have created a module that creates a custom entity (using hook_entity_info etc.), defines a field type (using hook_field_schema and hook_field_info), and then creates an instance of the field type with my custom entity (using field_create_field and then field_create_instance).
This all works great until I try to uninstall the module and am told "Drupal (Field type(s) in use".
Whilst this is true they won't be in use once the module is uninstalled!
Short of going into the DB and starting to delete rows out of the field_config and field_config_instance table how do I get round this problem?
Here I am creating the field instance in hook_enable:
function emtr_enable() {
field_cache_clear();
field_associate_fields('emtr');
if(field_info_field('emtr_denominations')) return;
$field = array(
'field_name' => 'emtr_denominations',
'type' => 'field_emtr_denominations',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'translatable' => FALSE,
);
field_create_field($field);
$instance = array(
'field_name' => 'emtr_denominations',
'entity_type' => 'emtr_safe_check',
'bundle' => 'emtr_safe_check',
'label' => 'Denominations',
'required' => false,
'widget' => array(
'type' => 'number'
),
'settings' => array(
'target_type' => 'emtr_safe_check',
'handler_settings' => array('target_bundles' => NULL),
),
'display' => array(
'default' => array('label' => 'inline', 'type' => 'number_integer'),
),
);
field_create_instance($instance);
}
And here is my uninstall hook:
function emtr_uninstall() {
if ($instance = field_info_instance('emtr_safe_check', 'emtr_denominations', 'emtr_safe_check')) {
field_delete_instance($instance);
}
if (field_info_field('emtr_denominations')) {
field_delete_field('emtr_denominations');
}
field_purge_batch(100);
}
And hook_field_schema:
/**
* Implements hook_field_schema
*/
function emtr_field_schema($field) {
$columns = array(
'denomination_value' => array(
'type' => 'int',
'not null' => FALSE,
),
'denomination_tid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
);
$indexes = array(
'denomination_tid' => array('denomination_tid'),
);
return array(
'columns' => $columns,
'indexes' => $indexes,
);
}
Just in case my hook_field_info and hook_entity_info are in a pastebin here.
field_delete_instance
in your custom uninstall hook, in symmetry to your call tofield_create_instance
? Show us your code if you want us to help you with that code.