4

Why would field_info_instances() return an empty set in hook_uninstall() when it returns the expected results in the main module file?

I've used the exact same code to delete the fields for a set of bundles in both the .install file and the main .module file:

  $bundles = array('property', 'open_house', 'agent', 'office');
  // Delete fields
  foreach ($bundles as $bundle) {
    $instances = field_info_instances('content', $bundle);
    if (!empty($instances)) {
      foreach ($instances as $instance) {
        dpm($instance);
        //field_delete_instance($instance);
      }
    }
  }

The following code (pulled from _field_info_collate_fields() in field.info.inc) outputs the fields in a list:

  $definitions = array(
    'field_ids' => field_read_fields(array(), array('include_deleted' => 1)), 
    'instances' => field_read_instances(),
  );

as shown here:

 ... (Array, 2 elements)

     field_ids (Array, 149 elements)
     instances (Array, 8 elements)

It was suggested that I need to use drupal_load() and/or drupal_load_include() to load the field module or field.info.inc to use the field_info_instances() function. I also tried using drupal_load() to load my module, but the following code also does not work:

  $bundles = array('property', 'open_house', 'agent', 'office');
  drupal_load('module', 'field');
  module_load_include('inc', 'field.module', 'field.info');

  // Delete fields
  foreach ($bundles as $bundle) {
    $instances = field_info_instances('content', $bundle);
    dpm($instances);
    if (!empty($instances)) {
      foreach ($instances as $instance) {
        field_delete_instance($instance);
      }
    }
  }

Does anyone know why field_info_instances() would be unable to read the information about the field instances?

UPDATE:

When I run the following code: dpm(field_read_instances(null, array('include_inactive' => TRUE))); my fields are displayed. Why are they inactive, is it because the module is being uninstalled and is disabled? What should I do to work around this?

  • What are your field types? A field type module can't control its own fields, it gets really messy (this is why we split relation_endpoints off the main relation module for example). – user49 Oct 10 '11 at 0:43
  • 1
    Field types are text, image, address etc. I'm just making instances of those. I found out that this has been fixed in 7.8. #943772 by jpsoto, yched, catch, bojanz, pillarsdotnet, steinmb: Fixed field_delete_field() and others fail for inactive fields. Thanks chx. – Arosboro Oct 10 '11 at 13:34
0

I found out that this has been fixed in 7.8. #943772 by jpsoto, yched, catch, bojanz, pillarsdotnet, steinmb: Fixed field_delete_field() and others fail for inactive fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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