You are here

function _linkimagefield_widget_prepare_form_values in Link Image Field 5

1 call to _linkimagefield_widget_prepare_form_values()
linkimagefield_widget in ./linkimagefield.module
Implementation of hook_widget().

File

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

Code

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

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

  // Attach new files
  if ($file = file_check_upload($fieldname . '_upload')) {
    $file = (array) $file;
    if (strpos($file['filemime'], 'image') !== FALSE) {
      $file = _linkimagefield_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;
          }
        }
        linkimagefield_clear_field_session($fieldname);
      }

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

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