function field_update_instance

Updates an instance of a field.

Parameters

$instance: An associative array representing an instance structure. The required keys and values are:

  • entity_type: The type of the entity the field is attached to.
  • bundle: The bundle this field belongs to.
  • field_name: The name of an existing field.

Read-only ID properties are assigned automatically. Any other properties properties specified in $instance overwrite the existing values for the instance.

Throws

Drupal\field\FieldException

See also

field_create_instance()

Related topics

29 calls to field_update_instance()
DisplayOverview::submit in drupal/core/modules/field_ui/lib/Drupal/field_ui/DisplayOverview.php
Overrides Drupal\field_ui\OverviewBase::submit().
EntityFieldTest::testComputedProperties in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php
Tests getting processed property values via a computed property.
FieldAttachOtherTest::testFieldAttachPrepareViewMultiple in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Tests the 'multiple entity' behavior of field_attach_prepare_view().
FieldAttachOtherTest::testFieldAttachView in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php
Test field_attach_view() and field_attach_prepare_view().
FieldAttachStorageTest::testFieldAttachSaveLoad in drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php
Check field values insert, update and load.

... See full list

File

drupal/core/modules/field/field.crud.inc, line 543
Field CRUD API, handling field and field instance creation and deletion.

Code

function field_update_instance($instance) {

  // Check that the specified field exists.
  $field = field_read_field($instance['field_name']);
  if (empty($field)) {
    throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array(
      '@field' => $instance['field_name'],
    )));
  }

  // Check that the field instance exists (even if it is inactive, since we
  // want to be able to replace inactive widgets with new ones).
  $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'], array(
    'include_inactive' => TRUE,
  ));
  if (empty($prior_instance)) {
    throw new FieldException(t("Attempt to update an instance of field @field on bundle @bundle that doesn't exist.", array(
      '@field' => $instance['field_name'],
      '@bundle' => $instance['bundle'],
    )));
  }
  $instance['id'] = $prior_instance['id'];
  $instance['field_id'] = $prior_instance['field_id'];
  _field_write_instance($instance, TRUE);

  // Clear caches.
  field_cache_clear();
  module_invoke_all('field_update_instance', $instance, $prior_instance);
}