You are here

function media_file_submit in D7 Media 7.2

Same name and namespace in other branches
  1. 7.4 media.module \media_file_submit()
  2. 7.3 media.module \media_file_submit()

Form submission handler for attach / remove buttons of media elements.

See also

media_element_process()

1 string reference to 'media_file_submit'
media_element_process in ./media.module
Process callback for the media form element.

File

./media.module, line 980
Media API

Code

function media_file_submit($form, &$form_state) {

  // Determine whether it was the attach or remove button that was clicked, and
  // set $element to the managed_file element that contains that button.
  $parents = $form_state['triggering_element']['#array_parents'];
  $button_key = array_pop($parents);
  $element = drupal_array_get_nested_value($form, $parents);

  // No action is needed here for the attach button, because all media
  // attachments on the form are processed by media_file_value() regardless of
  // which button was clicked. Action is needed here for the remove button,
  // because we only remove a file in response to its remove button being
  // clicked.
  if ($button_key == 'remove_button') {

    // If it's a temporary file we can safely remove it immediately, otherwise
    // it's up to the implementing module to clean up files that are in use.
    if ($element['#file'] && $element['#file']->status == 0) {
      file_delete($element['#file']);
    }

    // Update both $form_state['values'] and $form_state['input'] to reflect
    // that the file has been removed, so that the form is rebuilt correctly.
    // $form_state['values'] must be updated in case additional submit handlers
    // run, and for form building functions that run during the rebuild, such as
    // when the media element is part of a field widget.
    // $form_state['input'] must be updated so that media_file_value() has
    // correct information during the rebuild.
    $values_element = $element['#extended'] ? $element['fid'] : $element;
    form_set_value($values_element, NULL, $form_state);
    drupal_array_set_nested_value($form_state['input'], $values_element['#parents'], NULL);
  }

  // Set the form to rebuild so that $form is correctly updated in response to
  // processing the file removal. Since this function did not change $form_state
  // if the upload button was clicked, a rebuild isn't necessary in that
  // situation and setting $form_state['redirect'] to FALSE would suffice.
  // However, we choose to always rebuild, to keep the form processing workflow
  // consistent between the two buttons.
  $form_state['rebuild'] = TRUE;
}