You are here

public static function FieldHelper::deleteInstance in Helper 7

Delete a field instance.

This is a clone of field_delete_instance() that works for inactive fields.

Parameters

array $instance: The field instance definition. This may be deleted or inactive.

bool $purge:

bool $field_cleanup:

Throws

FieldException

4 calls to FieldHelper::deleteInstance()
FieldChangeHelper::changeInstanceField in lib/FieldChangeHelper.php
FieldHelper::deleteField in lib/FieldHelper.php
Delete a field.
_helper_field_delete_instance in ./helper.field.inc
A clone of field_delete_instance() that works for inactive fields.
_helper_purge_inactive_deleted_fields in ./helper.field.inc

File

lib/FieldHelper.php, line 201

Class

FieldHelper

Code

public static function deleteInstance(array $instance, $purge = TRUE, $field_cleanup = TRUE) {
  $field = static::readFieldById($instance['field_id']);
  $instance = static::readInstanceById($instance['id']);
  if (empty($instance)) {
    throw new FieldException();
  }
  if (!module_exists($field['storage']['module'])) {
    throw new FieldException("The {$field['storage']['module']} module needs to be enabled in order to delete an instance ID {$instance['id']} from field ID {$field['id']}.");
  }
  if (empty($instance['deleted'])) {

    // Mark the field instance for deletion.
    db_update('field_config_instance')
      ->fields(array(
      'deleted' => 1,
    ))
      ->condition('field_name', $instance['field_name'])
      ->condition('entity_type', $instance['entity_type'])
      ->condition('bundle', $instance['bundle'])
      ->execute();

    // Clear the cache.
    field_cache_clear();

    // Mark instance data for deletion.
    module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance);

    // Let modules react to the deletion of the instance.
    module_invoke_all('field_delete_instance', $instance);
    watchdog('helper', "Marked field instance ID {$instance['id']} for deletion.");
  }
  if ($purge) {
    static::purgeInstanceData($instance);
    static::purgeInstance($instance);
  }
  if ($field_cleanup && !field_read_instances(array(
    'field_id' => $field['id'],
  ), array(
    'include_deleted' => TRUE,
    'include_inactive' => TRUE,
  ))) {
    static::deleteField($field);
  }
}