You are here

function content_field_instance_delete in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 5 content_crud.inc \content_field_instance_delete()
  2. 6.3 includes/content.crud.inc \content_field_instance_delete()
  3. 6 includes/content.crud.inc \content_field_instance_delete()

Delete an existing field instance.

Parameters

$field_name: The field name to delete.

$type_name: The content type where the field instance is going to be deleted.

$rebuild: TRUE to clear content type caches and rebuild menu (default). FALSE allows the caller to process several fields at a time quickly, but then the caller is reponsible to clear content type caches and rebuild menu as soon as all fields have been processed. For example:

// Delete several fields at a time.
foreach ($fields as $field) {
  content_field_instance_delete($field['field_name'], $type_name, FALSE);
}

// Clear caches and rebuild menu.
content_clear_type_cache(TRUE);
menu_rebuild();

See also

content_clear_type_cache()

menu_rebuild()

4 calls to content_field_instance_delete()
ContentCrudTestCase::deleteField in tests/content.crud.test
Deletes an instance of a field.
content_field_remove_form_submit in includes/content.admin.inc
Remove a field from a content type.
content_module_delete in includes/content.crud.inc
Delete all data related to a module.
content_type_delete in includes/content.crud.inc
Make changes needed when a content type is deleted.

File

includes/content.crud.inc, line 545
Create/Read/Update/Delete functions for CCK-defined object types.

Code

function content_field_instance_delete($field_name, $type_name, $rebuild = TRUE) {
  include_once './' . drupal_get_path('module', 'content') . '/includes/content.admin.inc';

  // Get the previous field value.
  $field = array_pop(content_field_instance_read(array(
    'field_name' => $field_name,
    'type_name' => $type_name,
  )));

  // Invoke hook_content_fieldapi().
  module_invoke_all('content_fieldapi', 'delete instance', $field);
  db_query("DELETE FROM {" . content_instance_tablename() . "} WHERE field_name = '%s' AND type_name = '%s'", $field['field_name'], $field['type_name']);

  // If no instances remain, delete the field entirely.
  $instances = content_field_instance_read(array(
    'field_name' => $field_name,
  ));
  if (sizeof($instances) < 1) {
    db_query("DELETE FROM {" . content_field_tablename() . "} WHERE field_name = '%s'", $field['field_name']);
    content_alter_schema($field, array());
  }
  elseif (sizeof($instances) == 1 && !$field['multiple']) {

    // Multiple-valued fields are always stored per-field-type.
    $instance = $instances[0];
    $new_instance = $instance;
    $new_instance['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
    _content_field_write($new_instance, 'update');
    content_alter_schema($instance, $new_instance);
  }

  // If the deleted instance was the last field for the content type,
  // we drop the per-type table. We also consider possibly inactive fields.
  if (!content_field_instance_read(array(
    'type_name' => $field['type_name'],
  ), TRUE)) {
    $base_tablename = _content_tablename($field['type_name'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
    if (db_table_exists($base_tablename)) {
      db_drop_table($ret, $base_tablename);
    }
  }
  if ($rebuild) {
    content_clear_type_cache(TRUE);
    menu_rebuild();
  }
  return $field;
}