You are here

function content_field_instance_delete in Content Construction Kit (CCK) 5

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

Delete an existing field instance.

Parameters

$properties: An array of properties to use in selecting a field instance. Valid keys:

  • 'type_name' - The name of the content type in which the instance exists.
  • 'field_name' - The name of the field whose instance is to be deleted.

Return value

The number of field instances deleted.

3 calls to content_field_instance_delete()
content_field_delete in ./content_crud.inc
Delete an existing field.
content_type_delete in ./content_crud.inc
Make changes needed when a content type is deleted.
_content_admin_field_remove_submit in ./content_admin.inc
Remove a field from a content type.

File

./content_crud.inc, line 314
Create/Read/Update/Delete functions for CCK-defined object types.

Code

function content_field_instance_delete($properties) {
  $number_deleted = db_query("DELETE FROM {node_field_instance} WHERE type_name = '%s' AND field_name = '%s'", $properties['type_name'], $properties['field_name']);
  $type = content_types($properties['type_name']);
  $field = $type['fields'][$properties['field_name']];
  $field_types = _content_field_types();
  $field_type = $field_types[$field['type']];
  $columns = module_invoke($field_type['module'], 'field_settings', 'database columns', $field);
  $instances = db_result(db_query("SELECT COUNT(*) FROM {node_field_instance} WHERE field_name = '%s'", $properties['field_name']));

  // If only one instance remains, we may need to change the database
  // representation for this field.
  if ($instances == 1) {
    if (!$field['multiple']) {

      // Multiple-valued fields are always stored per-content-type.
      if (is_array($columns) && count($columns)) {
        $new_field = $field;
        $new_field['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
        db_query("UPDATE {node_field} SET db_storage = %d WHERE field_name = '%s'", CONTENT_DB_STORAGE_PER_CONTENT_TYPE, $properties['field_name']);
        content_alter_db_field($field, $columns, $new_field, $columns);
      }
    }
  }
  elseif ($instances == 0) {
    if (is_array($columns) && count($columns)) {
      content_alter_db_field($field, $columns, array(), array());
    }
    db_query("DELETE FROM {node_field} WHERE field_name = '%s'", $properties['field_name']);
  }
  content_clear_type_cache();
  return $number_deleted;
}