Updates an instance of a field.
mixed $instance: Either the \Drupal\field\Plugin\Core\Entity\FieldInstance to update, or an associative array representing an instance structure. If the latter, the required keys and values are:
The other array elements represent properties of the instance, and all properties must be specified or their default values will be used (except internal-use properties, which are assigned automatically). To avoid losing the previously stored properties of the instance when making a change, first load the instance with field_info_instance(), then override the values you want to override, and finally save using this function. Example:
// Fetch an instance info array.
$instance_info = field_info_instance($entity_type, $field_name, $bundle_name);
// Change a single property in the instance definition.
$instance_info['definition']['required'] = TRUE;
// Write the changed definition back.
field_update_instance($instance_info['definition']);
as of Drupal 8.0. Use $instance->save().
function field_update_instance($instance) {
// Module developers can still pass in an array of properties.
if (is_array($instance)) {
$instance_loaded = entity_load('field_instance', $instance['entity_type'] . '.' . $instance['bundle'] . '.' . $instance['field_name']);
if (empty($instance_loaded)) {
throw new FieldException('Attempt to update a non-existent instance.');
}
// Merge incoming values.
foreach ($instance as $key => $value) {
$instance_loaded[$key] = $value;
}
$instance = $instance_loaded;
}
$instance
->save();
}