You are here

function _imagefield_widget_settings_default_validate in ImageField 6.3

Element specific validation for imagefield default value.

Validated in a separate function from imagefield_field() to get access to the $form_state variable.

1 string reference to '_imagefield_widget_settings_default_validate'
imagefield_widget_settings_form in ./imagefield_widget.inc
Implementation of CCK's hook_widget_settings($op = 'form').

File

./imagefield_widget.inc, line 138
ImageField widget hooks and callbacks.

Code

function _imagefield_widget_settings_default_validate($element, &$form_state) {

  // Skip this validation if there isn't a default file uploaded at all or if
  // validation has already failed because of another error.
  if (!is_uploaded_file($_FILES['files']['tmp_name']['default_image_upload']) || form_set_error()) {
    return;
  }

  // Verify the destination exists.
  $destination = file_directory_path() . '/imagefield_default_images';
  if (!field_file_check_directory($destination, FILE_CREATE_DIRECTORY)) {
    form_set_error('default_image', t('The default image could not be uploaded. The destination %destination does not exist or is not writable by the server.', array(
      '%destination' => dirname($destination),
    )));
    return;
  }
  $validators = array(
    'file_validate_is_image' => array(),
  );

  // We save the upload here because we can't know the correct path until the file is saved.
  if (!($file = file_save_upload('default_image_upload', $validators, $destination))) {

    // No upload to save we hope... or file_save_upload() reported an error on its own.
    return;
  }

  // Remove the old default image if any.
  $old_default = $form_state['values']['default_image'];
  if (!empty($old_default['fid'])) {

    // Set a flag to not count this instance in hook_file_references().
    $old_default['imagefield_type_name'] = $form_state['values']['type_name'];
    field_file_delete($old_default);
  }

  // Make the file permanent and store it in the form.
  file_set_status($file, FILE_STATUS_PERMANENT);
  $file->timestamp = time();
  $form_state['values']['default_image'] = (array) $file;
}