You are here

function content_field_instance_update in Content Construction Kit (CCK) 6.2

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 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.

$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:

// Update several fields at a time.
foreach ($fields as $field) {
  content_field_instance_update($field, FALSE);
}

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

See also

content_clear_type_cache()

menu_rebuild()

6 calls to content_field_instance_update()
ContentCrudBasicTest::testBasic in tests/content.crud.test
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_display_overview_form_submit in includes/content.admin.inc
Submit handler for the display overview form.
content_field_basic_form_submit in includes/content.admin.inc
Create a new field for a content type.
content_field_edit_form_submit in includes/content.admin.inc
Save a field's settings after editing.

... See full list

File

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

Code

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

  // Ensure the field description is in the 'expanded' form.
  $field = content_field_instance_expand($field);

  // 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);

  // 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);
  if ($rebuild) {
    content_clear_type_cache(TRUE);

    // The label is in the menu tree, so we need a menu rebuild
    // if the label changes.
    if ($prev_field['widget']['label'] != $field['widget']['label']) {
      menu_rebuild();
    }
  }
  return $field;
}