You are here

function media_add_upload in D7 Media 7

Form callback for adding media via an upload form. @todo: should use the AJAX uploader

1 call to media_add_upload()
media_add_upload_multiple in includes/media.pages.inc
1 string reference to 'media_add_upload'
media_media_browser_plugin_view in ./media.media.inc
Implements hook_media_browser_plugin_view().

File

includes/media.pages.inc, line 152
Common pages for the Media module.

Code

function media_add_upload($form, &$form_state, $params = array()) {

  // Set up file upload validators.
  $validators = array();

  // Validate file extensions. If there are no file extensions in $params and
  // there are no Media defaults, there is no file extension validation.
  if (!empty($params['file_extensions'])) {
    $validators['file_validate_extensions'] = array(
      $params['file_extensions'],
    );
  }
  elseif ($tmp = media_variable_get('file_extensions')) {
    $validators['file_validate_extensions'] = array(
      $tmp,
    );
  }

  // Validate file size but do not allow anything higher than file_upload_max_size().
  $max_filesize = file_upload_max_size();
  if (!empty($params['max_filesize']) && $params['max_filesize'] < $max_filesize) {
    $validators['file_validate_size'] = array(
      parse_size($params['max_filesize']),
    );
  }
  elseif (($tmp = media_variable_get('max_filesize')) && $tmp < $max_filesize) {
    $validators['file_validate_size'] = array(
      parse_size($tmp),
    );
  }
  else {
    $validators['file_validate_size'] = array(
      $max_filesize,
    );
  }

  // Add image validators.
  $params += array(
    'min_resolution' => 0,
    'max_resolution' => 0,
  );
  if ($params['min_resolution'] || $params['max_resolution']) {
    $validators['file_validate_image_resolution'] = array(
      $params['max_resolution'],
      $params['min_resolution'],
    );
  }
  $form['#validators'] = $validators;
  $form['upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload a new file'),
    '#description' => theme('file_upload_help', array(
      'description' => '',
      'upload_validators' => $validators,
    )),
    '#upload_validators' => $validators,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}