You are here

function media_youtube_add_validate in Media: YouTube 7.2

Allow stream wrappers to have their chance at validation.

Any module that implements hook_media_parse will have an opportunity to validate this.

See also

media_parse_to_uri()

File

./media_youtube.module, line 254
Provides a stream wrapper and formatters appropriate for accessing and displaying YouTube videos.

Code

function media_youtube_add_validate($form, &$form_state) {
  if ($form_state['values']['op'] == t('Apply')) {
    return;
  }
  $uri = $form_state['values']['submitted-video'];
  try {
    $file = file_uri_to_object($uri, TRUE);
  } catch (Exception $e) {
    form_set_error('url', $e
      ->getMessage());
    return;
  }
  if (!$file->uri) {
    form_set_error('url', t('Please select a video.'));
    return;
  }
  $validators = isset($form['#validators']) ? $form['#validators'] : array();
  if ($validators) {

    // Check for errors. @see media_add_upload_validate calls file_save_upload().
    // this code is ripped from file_save_upload because we just want the validation part.
    // Call the validation functions specified by this function's caller.
    $errors = file_validate($file, $validators);
    if (!empty($errors)) {
      $message = t('%uri could not be added.', array(
        '%uri' => $uri,
      ));
      if (count($errors) > 1) {
        $message .= theme('item_list', array(
          'items' => $errors,
        ));
      }
      else {
        $message .= ' ' . array_pop($errors);
      }
      form_set_error('url', $message);
      return FALSE;
    }
  }

  // @TODO: Validate that if we have no $uri that this is a valid file to
  // save. For instance, we may only be interested in images, and it would
  // be helpful to let the user know they passed the HTML page containing
  // the image accidentally. That would also save us from saving the file
  // in the submit step.
  // This is kinda a hack of the same.
  // This should use the file_validate routines that the upload form users.
  // We need to fix the media_parse_to_file routine to allow for a validation.
}