You are here

function media_internet_add_validate in D7 Media 7.4

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

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()

1 string reference to 'media_internet_add_validate'
media_internet_form_file_entity_add_upload_alter in modules/media_internet/media_internet.module
Implements hook_form_FORM_ID_alter().

File

modules/media_internet/media_internet.module, line 233

Code

function media_internet_add_validate($form, &$form_state) {

  // Supporting providers can now claim this input. It might be a URL, but it
  // might be an embed code as well.
  $embed_code = $form_state['values']['embed_code'];
  try {
    $provider = media_internet_get_provider($embed_code);
    $provider
      ->validate();
  } catch (MediaInternetNoHandlerException $e) {
    form_set_error('embed_code', $e
      ->getMessage());
    return;
  } catch (MediaInternetValidationException $e) {
    form_set_error('embed_code', $e
      ->getMessage());
    return;
  }
  $validators = $form['#validators'];
  $file = $provider
    ->getFileObject();
  if ($validators) {
    try {
      $file = $provider
        ->getFileObject();
    } catch (Exception $e) {
      form_set_error('embed_code', $e
        ->getMessage());
      return;
    }

    // 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('%url could not be added.', array(
        '%url' => $embed_code,
      ));
      if (count($errors) > 1) {
        $message .= theme('item_list', array(
          'items' => $errors,
        ));
      }
      else {
        $message .= ' ' . array_pop($errors);
      }
      form_set_error('embed_code', $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.
}