You are here

function brightcove_field_browser_video_validate in Brightcove Video Connect 7.6

Same name and namespace in other branches
  1. 7.7 brightcove.module \brightcove_field_browser_video_validate()
  2. 7.3 brightcove_field/brightcove_field.module \brightcove_field_browser_video_validate()
  3. 7.4 brightcove_field/brightcove_field.module \brightcove_field_browser_video_validate()
  4. 7.5 brightcove_field/brightcove_field.module \brightcove_field_browser_video_validate()

Validate callback for the video field.

2 string references to 'brightcove_field_browser_video_validate'
brightcove_field_browser_process in ./brightcove.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_video_widget_form in ./brightcove_field.video.inc
Helper function to return the video widget form.

File

./brightcove.module, line 925
Brightcove module is an integration layer between any modules using Brightcove API. It makes all necessary checks for the API and makes settings available to the user.

Code

function brightcove_field_browser_video_validate($element, &$form_state) {
  $id = '';
  $value = $element['#value'];
  if (!empty($value)) {
    $id = brightcove_parse_id($value);
    $client_parents = $element['#parents'];
    array_pop($client_parents);
    array_push($client_parents, 'bcid');
    $bcid = drupal_array_get_nested_value($form_state['values'], $client_parents);
    $client = brightcove_client_load_or_default($bcid);

    // Assign ID to the value.
    // 231289 [id:72431493001]
    if (is_numeric($id) && empty($form_state['triggering_element']['#disable_video_validation'])) {

      // Matched ID, check if the video exists.
      $video = brightcove_load_video($id, $client);

      // Check value in session variable for newly uploaded video.
      if (empty($video)) {
        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.', [
          '%name' => t($element['#title']),
        ]));
      }
    }
    else {

      // Didn't match ID, try looking up the video text at BC.
      $result = [];
      brightcove_try(function () use (&$result, $client, $value) {

        /** @var \Brightcove\API\CMS $cms */
        list($cms, ) = brightcove_create_classes($client);
        $result = $cms
          ->listVideos($value, '-created_at');
      }, function () use (&$element) {
        form_error($element, t('There was a problem accessing Brightcove. Please try again'));
      });
      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]"', [
          '%title',
          $value,
          '%name' => t($element['#title']),
        ]));
      }
      elseif (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.', [
          '%name' => t($element['#title']),
        ]));
      }
      else {
        $id = $result[0]
          ->getId();
      }
    }
  }
  form_set_value($element, $id, $form_state);
}