You are here

public static function FieldHelper::deleteField in Helper 7

Delete a field.

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

Parameters

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

bool $purge:

Throws

FieldException

3 calls to FieldHelper::deleteField()
FieldHelper::deleteInstance in lib/FieldHelper.php
Delete a field instance.
_helper_field_delete_field in ./helper.field.inc
A clone of field_delete_field() that works for inactive fields.
_helper_purge_inactive_deleted_fields in ./helper.field.inc

File

lib/FieldHelper.php, line 304

Class

FieldHelper

Code

public static function deleteField(array $field, $purge = TRUE) {
  $field = static::readFieldById($field['id']);
  if (empty($field)) {
    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 field ID {$field['id']}.");
  }
  if ($instances = field_read_instances(array(
    'field_id' => $field['id'],
  ), array(
    'include_deleted' => TRUE,
    'include_inactive' => TRUE,
  ))) {
    foreach ($instances as $instance) {
      static::deleteInstance($instance, $purge, FALSE);
    }
  }
  if (empty($field['deleted'])) {

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

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

    // Clear the cache.
    field_cache_clear(TRUE);
    module_invoke_all('field_delete_field', $field);
    watchdog('helper', "Marked field ID {$field['id']} for deletion.");
  }
  if ($purge) {
    static::purgeField($field);
  }
}