You are here

public static function FieldHelper::purgeField in Helper 7

1 call to FieldHelper::purgeField()
FieldHelper::deleteField in lib/FieldHelper.php
Delete a field.

File

lib/FieldHelper.php, line 340

Class

FieldHelper

Code

public static function purgeField(array $field) {
  $field = static::readFieldById($field['id']);
  if (empty($field['deleted'])) {
    throw new FieldException("Field not yet marked as deleted.");
  }
  if (!module_exists($field['storage']['module'])) {
    throw new FieldException("The {$field['storage']['module']} module needs to be enabled in order to delete field ID {$field['id']}.");
  }
  $instances = field_read_instances(array(
    'field_id' => $field['id'],
  ), array(
    'include_deleted' => TRUE,
    'include_inactive' => TRUE,
  ));
  if (count($instances) > 0) {
    throw new FieldException(t('Attempt to purge a field @field_name that still has instances.', array(
      '@field_name' => $field['field_name'],
    )));
  }
  db_delete('field_config')
    ->condition('id', $field['id'])
    ->execute();

  // Notify the storage engine.
  module_invoke($field['storage']['module'], 'field_storage_purge_field', $field);

  // Clear the cache.
  field_info_cache_clear();

  // Invoke external hooks after the cache is cleared for API consistency.
  module_invoke_all('field_purge_field', $field);
  watchdog('helper', "Field ID {$field['id']} completely removed.");
}