You are here

function brightcove_cck_browser_validate in Brightcove Video Connect 6.2

Same name and namespace in other branches
  1. 6 brightcove_cck/brightcove_cck.module \brightcove_cck_browser_validate()

Validate callback for the field.

1 string reference to 'brightcove_cck_browser_validate'
brightcove_cck_browser_process in brightcove_cck/brightcove_cck.module
Brightcove CCK field form that returns the actual field to the user. Parts of this and subsequent JS taken from Nodereference Explorer. Thanks!

File

brightcove_cck/brightcove_cck.module, line 212
Brightcove CCK module provides a Content Construction Kit module to developers, allowing them to browse videos in their Brightcove Studio and upload them.

Code

function brightcove_cck_browser_validate($element, &$form_state) {
  $id = '';
  $field_name = $element['#field_name'];
  $type_name = $element['#type_name'];
  $field = content_fields($field_name, $type_name);
  $field_key = $element['#columns'][0];
  $value = $element['#value'];
  if (!empty($value)) {

    // Assign ID to the value.
    // 231289 [id:72431493001]
    $id = brightcove_parse_id($value);
    if (is_numeric($id)) {

      // Matched ID, check if the video exists.
      $video = brightcove_video_load($id);
      if (is_null(brightcove_video_cache_get($id)) && $video->id != $id) {
        form_error($element, t('%name: Found no valid video with that name. Please note that it might take several minutes after the video has been uploaded in Brightcove Studio to appear in the API.', array(
          '%name' => t($field['widget']['label']),
        )));
      }
    }
    else {

      // Didn't match ID, try looking up the video text at BC.
      $bc = _brightcove_initialize();
      try {
        $result = $bc
          ->find('find_videos_by_text', array(
          'text' => $value,
        ));
      } catch (Exception $error) {
        form_error($element, t('There was a problem accessing Brightcove. Please try again'));
        watchdog('brightcove', 'Validating element with Brightcove failed', array(), WATCHDOG_ERROR);
      }
      if (count($result) > 1) {

        // This title is ambiguous.
        form_error($element, t('%name: Video title %title matched more than one video. In case of doubt, use text "title [id:ID_OF_THE_VIDEO]"', array(
          '%title',
          $value,
          '%name' => t($field['widget']['label']),
        )));
      }
      else {
        if (count($result) == 0) {

          // No video found.
          form_error($element, t('%name: Found no valid video with that name. Please note that it might take several minutes after the video has been uploaded in Brightcove Studio to appear in the API.', array(
            '%name' => t($field['widget']['label']),
          )));
        }
        else {
          $id = $result[0]->id;
        }
      }
    }
  }
  form_set_value($element, $id, $form_state);
}