function image_field_update_field

Implements hook_field_update_field().

File

drupal/core/modules/image/image.module, line 367
Exposes global functionality for creating image styles.

Code

function image_field_update_field($field, $prior_field, $has_data) {
  if ($field['type'] != 'image') {
    return;
  }

  // The value of a managed_file element can be an array if #extended == TRUE.
  $fid_new = isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image'];
  $fid_old = isset($prior_field['settings']['default_image']['fids']) ? $prior_field['settings']['default_image']['fids'] : $prior_field['settings']['default_image'];
  $file_new = $fid_new ? file_load($fid_new) : FALSE;
  if ($fid_new != $fid_old) {

    // Is there a new file?
    if ($file_new) {
      $file_new->status = FILE_STATUS_PERMANENT;
      $file_new
        ->save();
      file_usage()
        ->add($file_new, 'image', 'default_image', $field['uuid']);
    }

    // Is there an old file?
    if ($fid_old && ($file_old = file_load($fid_old[0]))) {
      file_usage()
        ->delete($file_old, 'image', 'default_image', $field['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);
  }
}