You are here

function imagefield_crop_field_update_field in Imagefield Crop 7.2

Implements hook_field_update_field().

File

./imagefield_crop.module, line 101
Functionality and Drupal hook implementations for the Imagefield Crop module.

Code

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

  // The value of a managed_file element can be an array if #extended == TRUE.
  $fid_new = is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image'];
  $fid_old = is_array($prior_field['settings']['default_image']) ? $prior_field['settings']['default_image']['fid'] : $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_save($file_new);
      file_usage_add($file_new, 'imagefield_crop', 'default_image', $field['id']);
    }

    // Is there an old file?
    if ($fid_old && ($file_old = file_load($fid_old))) {
      file_usage_delete($file_old, 'imagefield_crop', 'default_image', $field['id']);
    }
  }

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