You are here

function media_internet_form_file_entity_add_upload_alter in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 modules/media_internet/media_internet.module \media_internet_form_file_entity_add_upload_alter()
  2. 7.3 modules/media_internet/media_internet.module \media_internet_form_file_entity_add_upload_alter()

Implements hook_form_FORM_ID_alter().

File

modules/media_internet/media_internet.module, line 169

Code

function media_internet_form_file_entity_add_upload_alter(&$form, &$form_state, $form_id) {
  $step = $form['#step'];
  $options = $form['#options'];

  // Swap the upload field for an embed field when on the first step of the web
  // tab.
  if ($form_id == 'media_internet_add_upload' && $step == 1) {
    unset($form['upload']);
    $form['embed_code'] = array(
      '#type' => 'textfield',
      '#title' => t('File URL'),
      '#description' => t('Enter a URL to a file.'),
      '#attributes' => array(
        'class' => array(
          'media-add-from-url',
        ),
      ),
      // There is no standard specifying a maximum length for a URL. Internet
      // Explorer supports up to 2083 (http://support.microsoft.com/kb/208427)
      // so we assume publicly available media URLs are within this limit.
      '#maxlength' => 2083,
      '#required' => TRUE,
      '#default_value' => isset($form_state['storage']['embed_code']) ? $form_state['storage']['embed_code'] : NULL,
    );

    // Create an array to hold potential Internet media providers.
    $providers = array();

    // Determine if there are any visible providers.
    foreach (media_internet_get_providers() as $key => $provider) {
      if (empty($provider['hidden']) || $provider['hidden'] != TRUE) {
        $providers[] = check_plain($provider['title']);
      }
    }
    $form['#providers'] = $providers;

    // Notify the user of any available providers.
    if ($providers) {

      // If any providers are enabled it is assumed that some kind of embed is supported.
      $form['embed_code']['#title'] = t('File URL or media resource');
      $form['embed_code']['#description'] = t('Enter a URL to a file or media resource. Many media providers also support identifying media via the embed code used to embed the media into external websites.');
      $form['embed_code']['#description'] = theme('media_internet_embed_help', array(
        'description' => $form['embed_code']['#description'],
        'supported_providers' => implode(', ', $providers),
      ));
    }
    $form['#validators'] = array();
    if (!empty($options['types'])) {
      $form['#validators']['media_file_validate_types'] = array(
        $options['types'],
      );
    }

    // Add validation and submission handlers to the form and ensure that they
    // run first.
    array_unshift($form['#validate'], 'media_internet_add_validate');
    array_unshift($form['#submit'], 'media_internet_add_submit');
  }
}