You are here

function content_field_instance_update in Content Construction Kit (CCK) 6

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

Update an existing field instance.

Parameters

$field: An array of properties to update the field with, input either in the field => widget format used by the content module or as an array of form values.

3 calls to content_field_instance_update()
ContentCrudTestCase::updateField in tests/content.crud.test
Updates a field instance. Also makes all future calls to functions which take an optional field use the updated one as the default.
content_admin_display_overview_form_submit in includes/content.admin.inc
Submit handler for the display overview form.
_content_admin_field_submit in includes/content.admin.inc
Save a field's settings after editing.

File

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

Code

function content_field_instance_update($field) {
  include_once './' . drupal_get_path('module', 'content') . '/includes/content.admin.inc';

  // Get the previous value from the table.
  $previous = content_field_instance_read(array(
    'field_name' => $field['field_name'],
    'type_name' => $field['type_name'],
  ));
  $prev_field = array_pop($previous);
  $field = content_field_instance_expand($field);

  // Create a complete field array by merging the previous and current values,
  // letting the current values overwrite the previous ones.
  $widget = array_merge($prev_field['widget'], $field['widget']);
  $field = array_merge($prev_field, $field);
  $field['widget'] = $widget;

  // Make sure we know what module to invoke for field info.
  if (empty($field['module']) && !empty($field['type'])) {
    $field_types = _content_field_types();
    $field['module'] = $field_types[$field['type']]['module'];
  }

  // The storage type may need to be updated.
  $field['db_storage'] = content_storage_type($field);

  // Changes in field values may affect columns, or column
  // information may have changed, get a fresh copy.
  $field['columns'] = (array) module_invoke($field['module'], 'field_settings', 'database columns', $field);

  // If the database storage has changed, update the field and previous instances.
  $prior_instances = content_field_instance_read(array(
    'field_name' => $field['field_name'],
  ));
  if ($prev_field['db_storage'] == CONTENT_DB_STORAGE_PER_CONTENT_TYPE && count($prior_instances) > 1) {

    // Update the field's data storage.
    $field['db_storage'] = CONTENT_DB_STORAGE_PER_FIELD;

    // Update the schema for prior instances to adapt to the change in db storage.
    foreach ($prior_instances as $instance) {
      if ($instance['type_name'] != $field['type_name']) {
        $new_instance = $instance;
        $new_instance['db_storage'] = CONTENT_DB_STORAGE_PER_FIELD;

        // Invoke hook_content_fieldapi().
        module_invoke_all('content_fieldapi', 'update instance', $new_instance);
        content_alter_schema($instance, $new_instance);
      }
    }
  }

  // Invoke hook_content_fieldapi().
  module_invoke_all('content_fieldapi', 'update instance', $field);

  // Update the field and the instance with the latest values.
  _content_field_write($field, 'update');
  _content_field_instance_write($field, 'update');
  content_alter_schema($prev_field, $field);
  content_clear_type_cache(TRUE);
  return $field;
}