You are here

function _imagefield_widget_prepare_form_values in ImageField 5

Same name and namespace in other branches
  1. 5.2 imagefield.module \_imagefield_widget_prepare_form_values()
1 call to _imagefield_widget_prepare_form_values()
imagefield_widget in ./imagefield.module
Implementation of hook_widget().

File

./imagefield.module, line 386
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, &$node_field) {
  $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;
    if (strpos($file['filemime'], 'image') !== FALSE) {
      $file = _imagefield_scale_image($file, $field['widget']['max_resolution']);

      // Create the filepath for the image preview
      $filepath = file_create_filename($file['filename'], file_create_path($field['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($node_field)) {
          foreach ($node_field as $delta => $session_file) {
            $node_field[$delta]['flags']['delete'] = TRUE;
          }
        }
        imagefield_clear_field_session($fieldname);
      }

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

  // 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) {
      $node_field[] = $file;
    }
  }
}