Implements hook_field_update_instance().
function image_field_update_instance($instance, $prior_instance) {
// Only act on image fields.
$field = field_read_field($instance['field_name']);
if ($field['type'] != 'image') {
return;
}
// The value of a managed_file element can be an array if the #extended
// property is set to TRUE.
$fid_new = $instance['settings']['default_image'];
if (isset($fid_new['fids'])) {
$fid_new = $fid_new['fids'];
}
$fid_old = $prior_instance['settings']['default_image'];
if (isset($fid_old['fids'])) {
$fid_old = $fid_old['fids'];
}
// If the old and new files do not match, update the default accordingly.
$file_new = $fid_new ? file_load($fid_new[0]) : FALSE;
if ($fid_new != $fid_old) {
// Save the new file, if present.
if ($file_new) {
$file_new->status = FILE_STATUS_PERMANENT;
$file_new
->save();
file_usage()
->add($file_new, 'image', 'default_image', $instance['uuid']);
}
// Delete the old file, if present.
if ($fid_old && ($file_old = file_load($fid_old[0]))) {
file_usage()
->delete($file_old, 'image', 'default_image', $instance['uuid']);
}
}
// If the upload destination changed, then move the file.
if ($file_new && file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme']) {
$directory = $field['settings']['uri_scheme'] . '://default_images/';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
file_move($file_new, $directory . $file_new->filename);
}
}