You are here

function _imagefield_widget_prepare_form_values in ImageField 5.2

Same name and namespace in other branches
  1. 5 imagefield.module \_imagefield_widget_prepare_form_values()
2 calls to _imagefield_widget_prepare_form_values()
imagefield_js in ./imagefield.module
Menu-callback for JavaScript-based uploads.
imagefield_widget in ./imagefield.module
Implementation of hook_widget().

File

./imagefield.module, line 586
Defines an image field type. imagefield uses content.module to store the fid, and the drupal files table to store the actual file data.

Code

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

  // clean up the session if we weren't posted.
  if (!count($_POST)) {
    imagefield_clear_session();
  }

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

    // Validation must happen immediately after the image is uploaded so we
    // can discard the file if it turns out not to be a valid mime type
    $valid_image = _imagefield_widget_upload_validate($node, $field, $items, $file);

    //$valid_image = true;
    if ($valid_image) {
      $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);

      // Allow tokenized paths if available
      if (function_exists('token_replace')) {
        global $user;
        $widget_image_path = token_replace($field['widget']['image_path'], 'user', $user);
      }
      else {
        $widget_image_path = $field['widget']['image_path'];
      }
      imagefield_check_directory($widget_image_path);

      // Create the filepath for the image preview
      $filepath = file_create_filename($file['filename'], file_create_path($widget_image_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;
      }
      $file['fid'] = 'upload';
      $file['preview'] = $filepath;

      // If a single field, mark any other images for deletion and delete files in session
      if (!$field['multiple']) {
        if (is_array($items)) {
          foreach ($items as $delta => $session_file) {
            $items[$delta]['flags']['hidden'] = true;
            $items[$delta]['flags']['delete'] = true;
          }
        }
        imagefield_clear_field_session($fieldname);
      }

      // Add the file to the session
      $file_id = count($items) + count($_SESSION['imagefield'][$fieldname]);
      $file['sessionid'] = $file_id;
      $_SESSION['imagefield'][$fieldname][$file_id] = $file;
    }
    else {

      // Delete the invalid file
      file_delete($file['filepath']);

      // If a single field and a valid file is in the session, mark existing image for deletion
      if (!$field['multiple']) {
        if (!empty($_SESSION['imagefield'][$fieldname]) && !empty($items)) {
          foreach ($items as $delta => $session_file) {
            $items[$delta]['flags']['hidden'] = true;
            $items[$delta]['flags']['delete'] = true;
          }
        }
      }
    }
  }

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