diff --git a/modules/media_internet/media_internet.pages.inc b/modules/media_internet/media_internet.pages.inc index 45af0b9..3398acc 100644 --- a/modules/media_internet/media_internet.pages.inc +++ b/modules/media_internet/media_internet.pages.inc @@ -21,6 +21,79 @@ function media_internet_add($form, &$form_state = array(), $options = array()) { } } +/** + * Allow stream wrappers to have their chance at validation. + * + * Any module that implements hook_media_parse will have an + * opportunity to validate this. + * + * @see media_parse_to_uri() + */ +function media_internet_add_validate($form, &$form_state) { + // We only want to validate the embed code after the first step. + if ($form['#step'] != 1) { + return; + } + + // 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. +} + function media_internet_add_upload_step_upload($form, &$form_state, array $options = array()) { $form['embed_code'] = array( '#type' => 'textfield',