You are here

function brightcove_field_browser_validate in Brightcove Video Connect 7.2

Validate callback for the field.

2 string references to 'brightcove_field_browser_validate'
brightcove_field_browser_process in brightcove_field/brightcove_field.module
Brightcove field form that returns the actual field to the user. Parts of this and subsequent JS taken from Nodereference Explorer. Thanks!
brightcove_field_field_widget_form in brightcove_field/brightcove_field.module
Implements hook_field_widget_form().

File

brightcove_field/brightcove_field.module, line 264
Brightcove field module provides a Content Construction Kit module to developers, allowing them to browse videos in their Brightcove Studio and upload them.

Code

function brightcove_field_browser_validate($element, &$form_state) {
  $id = '';
  $field_name = $element['#field_name'];
  $field = field_info_field($field_name);
  $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();
      $result = NULL;
      try {
        $result = $bc ? $bc
          ->find('find_videos_by_text', array(
          'text' => $value,
        )) : array();
      } 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);
}