You are here

function file_entity_edit_submit in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7 file_entity.pages.inc \file_entity_edit_submit()
  2. 7.2 file_entity.pages.inc \file_entity_edit_submit()

Form submission handler for the 'Save' button for file_entity_edit().

1 string reference to 'file_entity_edit_submit'
file_entity_edit in ./file_entity.pages.inc
Page callback: Form constructor for the file edit form.

File

./file_entity.pages.inc, line 982
Supports file operations including View, Edit, and Delete.

Code

function file_entity_edit_submit($form, &$form_state) {
  $file = $form_state['file'];
  $orphaned_uri = '';

  // Check if a replacement file has been uploaded.
  if (!empty($form_state['values']['replace_upload'])) {
    $replacement = $form_state['values']['replace_upload'];

    // Existing image metadata is stored in $file->height and $file->width.
    // Loop through the replacement metadata and update existing values.
    if (!empty($replacement->metadata)) {
      foreach ($replacement->metadata as $metadata_key => $metadata_value) {
        if (isset($file->{$metadata_key})) {
          $file->{$metadata_key} = $metadata_value;
        }
      }
    }

    // Move file from temp to permanent home.
    if (!empty($form_state['values']['replace_keep_original_filename']) && $form_state['values']['replace_keep_original_filename']) {
      $destination_uri = rtrim($file->uri, drupal_basename($file->uri)) . drupal_basename($file->uri);
    }
    else {
      $destination_uri = rtrim($file->uri, drupal_basename($file->uri)) . drupal_basename($replacement->uri);
    }
    $replace_mode = $destination_uri == $file->uri ? FILE_EXISTS_REPLACE : FILE_EXISTS_RENAME;
    if ($new_file_uri = file_unmanaged_copy($replacement->uri, $destination_uri, $replace_mode)) {

      // @todo Add watchdog() about replaced file here?
      // Remove temporary file.
      file_delete($replacement);

      // Update if the uri target has changed.
      if ($new_file_uri != $file->uri) {

        // Store the original file uri to delete if save is successful.
        $orphaned_uri = $file->uri;

        // Update file entity uri.
        $file->uri = $new_file_uri;
      }
    }
  }

  // Run entity form submit handling and save the file.
  entity_form_submit_build_entity('file', $file, $form, $form_state);

  // A user might assign the associated user by entering a user name in the file
  // edit form, which we then need to translate to a user ID.
  if (isset($file->name)) {

    // The use of isset() is mandatory in the context of user IDs, because
    // user ID 0 denotes the anonymous user.
    if ($user = user_load_by_name($file->name)) {
      $file->uid = $user->uid;
    }
    else {

      // Anonymous user.
      $file->uid = 0;
    }
  }
  elseif ($file->uid) {
    $user = user_load($file->uid);
    $file->name = $user->name;
  }
  if (file_uri_scheme($file->uri) != $form_state['values']['scheme']) {
    $file_destination = $form_state['values']['scheme'] . '://' . file_uri_target($file->uri);
    $file_destination = file_stream_wrapper_uri_normalize($file_destination);
    $file_destination_dirname = drupal_dirname($file_destination);

    // Create the directory in case it doesn't exist.
    file_prepare_directory($file_destination_dirname, FILE_CREATE_DIRECTORY);
    if ($moved_file = file_move($file, $file_destination, FILE_EXISTS_RENAME)) {

      // Only re-assign the file object if file_move() did not fail.
      $file = $moved_file;
    }
  }
  file_save($file);
  $args = array(
    '@type' => file_entity_type_get_name($file),
    '%title' => entity_label('file', $file),
  );
  watchdog('file', '@type: updated %title.', $args);
  drupal_set_message(t('@type %title has been updated.', $args));

  // Clean up orphaned file.
  if (!empty($orphaned_uri)) {
    file_unmanaged_delete($orphaned_uri);
    $args['@orphaned'] = file_uri_target($orphaned_uri);
    watchdog('file', '@type: deleted orphaned file @orphaned for %title.', $args);
    drupal_set_message(t('The replaced @type @orphaned has been deleted.', $args));
  }
  $form_state['redirect'] = 'file/' . $file->fid;

  // Clear the page and block caches.
  cache_clear_all();
}