You are here

function media_update_presubmit in Media Update 7.2

Same name and namespace in other branches
  1. 7 media_update.module \media_update_presubmit()

Provides submit handling based on hook_media_update_params.

Checks to ensure that an update is actually occurring then performs a form submission based on our validation params.

1 string reference to 'media_update_presubmit'
media_update_form_media_edit_alter in ./media_update.module
Implements hook_form_FORM_ID_alter().

File

./media_update.module, line 170
Defines the module media_update.

Code

function media_update_presubmit($form, &$form_state) {
  if (isset($form_state['update_field']) && $form_state['file_updated']) {
    $field = $form_state['update_field'];
    if (isset($form_state['values'][$field])) {
      $original_file = $form_state['file'];

      // We have to update the fid of the file object for file-based uploads,
      // but this entity isn't an object in other scenarios.
      if (isset($form_state['values'][$field]) && is_object($form_state['values'][$field]) && $form_state['update_type'] == 'file') {
        $form_state['values'][$field]->fid = $original_file->fid;
        $file_name_changed = $original_file->filename == $form_state['values'][$field]->filename;

        // See if we need to remove the original file.
        $remove_original_file = !$file_name_changed && variable_get(MEDIA_UPDATE_REPLACE_SAME, 0) || $file_name_changed && variable_get(MEDIA_UPDATE_REPLACE_DIFFERENT, 0);
        if ($remove_original_file) {
          file_unmanaged_delete($original_file->uri);
        }
      }

      // @todo: Find a better way to handle this.
      // We're doing this because both the edit form and the add form pass an
      // argument. The edit form passes a file object while the add form needs
      // a params array. The array holds little of value to our submit, but
      // the incorrect object type causes chaos.
      $arg0 = $form_state['build_info']['args'][0];
      $form_state['build_info']['args'][0] = array();

      // Call the original add submit handler.
      $form_state['update_submit']($form, $form_state);
      _media_update_original_file(NULL, TRUE);

      // Potentially 2 file saves happen per file upload, but we want to ensure
      // that any upload logic occurs as well as any update logic.
      $form_state['file'] = file_load($original_file->fid);

      // We're done with our submit handler, so reset the args element.
      $form_state['build_info']['args'][0] = $arg0;

      // Raise an event so that other modules know that a file has been replaced.
      module_invoke_all('media_update_updated', $original_file);
    }
  }
}