You are here

function media_unsplash_add_validate in Media Unsplash 7

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_unsplash_add_validate'
media_unsplash_form_file_entity_add_upload_alter in ./media_unsplash.module
Implements hook_form_FORM_ID_alter().

File

./media_unsplash.module, line 406
Provides definition for unsplash media integration.

Code

function media_unsplash_add_validate($form, &$form_state) {
  $embed_code = $form_state['values']['unsplash_code'];

  // See @file_entity_add_upload_submit
  // Needed for skipping fields if is enabled and if Web tab is active.
  $form_state['triggering_element']['#id'] = 'edit-next';
  if (!empty($embed_code)) {
    try {
      $provider = new MediaUnsplashHandler($embed_code);
      $provider
        ->validate();
    } catch (MediaInternetNoHandlerException $e) {
      form_set_error('unsplash_code', $e
        ->getMessage());
      return;
    } catch (MediaInternetValidationException $e) {
      form_set_error('unsplash_code', $e
        ->getMessage());
      return;
    }
    $validators = $form['#validators'];
    $file = $provider
      ->getFileObject();
    if ($validators) {
      try {
        $file = $provider
          ->getFileObject();
      } catch (Exception $e) {
        form_set_error('unsplash_code', $e
          ->getMessage());
        return;
      }
      $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('unsplash_code', $message);
        return FALSE;
      }
    }
  }
  else {
    return FALSE;
  }
}