function file_managed_file_submit

Form submission handler for upload / remove buttons of managed_file elements.

See also

file_managed_file_process()

2 string references to 'file_managed_file_submit'
file_managed_file_process in drupal/core/modules/file/file.module
Render API callback: Expands the managed_file element type.
_image_field_required_fields_validate in drupal/core/modules/image/image.field.inc
Validate callback for alt and title field, if the user wants them required.

File

drupal/core/modules/file/file.module, line 1113
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_managed_file_submit($form, &$form_state) {

  // Determine whether it was the upload or the 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 = NestedArray::getValue($form, $parents);

  // No action is needed here for the upload button, because all file uploads on
  // the form are processed by file_managed_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') {
    $fids = array_keys($element['#files']);

    // Get files that will be removed.
    if ($element['#multiple']) {
      $remove_fids = array();
      foreach (element_children($element) as $name) {
        if (strpos($name, 'file_') === 0 && $element[$name]['selected']['#value']) {
          $remove_fids[] = (int) substr($name, 5);
        }
      }
      $fids = array_diff($fids, $remove_fids);
    }
    else {

      // If we deal with single upload element remove the file and set
      // element's value to empty array (file could not be removed from
      // element if we don't do that).
      $remove_fids = $fids;
      $fids = array();
    }
    foreach ($remove_fids as $fid) {

      // If it's a temporary file we can safely remove it immediately, otherwise
      // it's up to the implementing module to remove usages of files to have them
      // removed.
      if ($element['#files'][$fid] && $element['#files'][$fid]->status == 0) {
        file_delete($element['#files'][$fid]->fid);
      }
    }

    // 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 managed_file element is part of a field widget.
    // $form_state['input'] must be updated so that file_managed_file_value()
    // has correct information during the rebuild.
    form_set_value($element['fids'], implode(' ', $fids), $form_state);
    NestedArray::setValue($form_state['input'], $element['fids']['#parents'], implode(' ', $fids));
  }

  // 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;
}