2

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.

4
  • Are you sure you are calling field_delete_instance in your custom uninstall hook, in symmetry to your call to field_create_instance? Show us your code if you want us to help you with that code.
    – Mołot
    Commented Feb 13, 2014 at 16:26
  • I was not using field_delete_instance in my uninstall hook but have added it in and manually removed the module and fields, and then reinstalled the module however the problem persists.
    – Felix Eve
    Commented Feb 13, 2014 at 16:35
  • 1
    This is pretty much by design - the best solution would be to separate your entity and field into two separate modules so they can be disabled independently. You can add/remove the field instance in the entity module's install/uninstall hooks, so when you disable that module the instance is removed, and you can then disable the field module
    – Clive
    Commented Feb 13, 2014 at 17:02
  • What a good idea - cheers Clive :)
    – Felix Eve
    Commented Feb 14, 2014 at 8:47

1 Answer 1

1

So as Clive suggested I solved this by creating a helper module called emtr_fields.

The emtr_fields module creates the entity and has the field schema.

Then the main emtr module creates the field and it's instance.

To remove all of this again first the emtr module has to be disable AND uninstalled (it has to be uninstalled as the field and it's instance is deleted in hook_uninstall), and then the emtr_field module can be disabled and uninstalled.

I also found some documentation on this on drupal.org here:

Module cannot be disabled because Drupal claim "Field type(s) in use"

Moving field(s) definitions into a separate module of a installed module.

Field types should be defined by one module and implemented by a separate module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.