You are here

function _filefield_widget_prepare_form_values in FileField 5.2

2 calls to _filefield_widget_prepare_form_values()
filefield_js in ./filefield.module
Menu callback for JavaScript-based uploads.
filefield_widget in ./filefield.module
Implementation of hook_widget().

File

./filefield.module, line 393
Defines a file field type.

Code

function _filefield_widget_prepare_form_values($node, $field, &$items) {
  $fieldname = $field['field_name'];

  // @todo split this into its own function. determine if we can make it a form element.
  if (!count($_POST)) {
    filefield_clear_session();
  }

  // Attach new files
  if ($file = file_check_upload($fieldname . '_upload')) {
    $file = (array) $file;

    // test allowed extensions. We do this when the file is uploaded, rather than waiting for the
    // field itseld to reach op==validate.
    $last_ext = array_pop(explode('.', $file['filename']));
    $valid = TRUE;

    // only check extensions if there extensions to check.
    // @todo: trim & strtolower file_extenstions with a formapi validate callback.
    if (strlen(trim($field['widget']['file_extensions']))) {
      $allowed_extensions = array_unique(explode(' ', strtolower(trim($field['widget']['file_extensions']))));
      $ext = strtolower(array_pop(explode('.', $file['filename'])));
      if (!in_array($ext, $allowed_extensions)) {
        $valid = FALSE;
        form_set_error($field['field_name'] . '_upload', t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions', array(
          '%ext' => $last_ext,
          '%allowed_extensions' => $field['widget']['file_extensions'],
        )));
      }
    }

    // let extended validation from other module happen so we get all error messages.
    // if you implement hook_filefield_file() return FALSE to stop the upload.
    if (!$valid || in_array(FALSE, module_invoke_all('filefield', 'file_validate', $node, $field, $file))) {
      return FALSE;
    }

    // let modules massage act on the file.
    foreach (module_implements('filefield') as $module) {
      $function = $module . '_filefield';
      $function('file_prepare', $node, $field, $file);
    }
    $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['file_path']));
    if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
      if (strpos($filepath, file_directory_path()) !== FALSE) {
        $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/');
      }
      $filepath = 'system/files/' . $filepath;
    }

    // prepare file array.
    $file['fid'] = 'upload';
    $file['preview'] = $filepath;

    // if this is a single value filefield mark any other files for deletion.
    if (!$field['multiple']) {
      if (is_array($items)) {
        foreach ($items as $delta => $session_file) {
          $items[$delta]['delete'] = TRUE;
        }
      }

      // Remove old temporary file from session.
      filefield_clear_field_session($fieldname);
    }
    $file_id = count($items) + count($_SESSION['filefield'][$fieldname]);
    $_SESSION['filefield'][$fieldname][$file_id] = $file;
  }

  // Load files from preview state. before committing actions.
  if (!empty($_SESSION['filefield'][$fieldname])) {
    foreach ($_SESSION['filefield'][$fieldname] as $delta => $file) {
      $items[] = $file;
    }
  }
}