You are here

function _brightcove_edit_form_callback in Brightcove Video Connect 7.6

Same name and namespace in other branches
  1. 7.7 brightcove.module \_brightcove_edit_form_callback()
1 call to _brightcove_edit_form_callback()
ajax_edit_video_dialog_close_callback in ./brightcove.module

File

./brightcove.module, line 3075
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_edit_form_callback($form, $form_state) {
  brightcove_load_lib();
  $video_entity = $form['#entity'];
  $client = $video_entity->client;
  $video_id = $video_entity->video_id;
  if (empty($video_entity->video) || !$video_entity->video instanceof \Brightcove\Object\Video\Video) {
    $video_entity->video = brightcove_load_video($video_id, $client);
  }
  $video = $video_entity->video;
  $custom_field_definitions = [];
  brightcove_try(function () use (&$custom_field_definitions, $client) {

    /** @var \Brightcove\API\CMS $cms */
    list($cms, ) = brightcove_create_classes($client);
    $custom_field_definitions = $cms
      ->getVideoFields()
      ->getCustomFields();
  });
  $video
    ->setName($form_state['values']['title']);
  $video
    ->setDescription($form_state['values']['short']);
  $video
    ->setLongDescription($form_state['values']['long']);
  $video
    ->setReferenceId($form_state['values']['reference_id']);
  $video
    ->setTags(array_filter(array_map('trim', explode(',', $form_state['values']['tags']))));
  $video
    ->setState($form_state['values']['state']);
  $custom_fields = [];
  foreach ($custom_field_definitions as $def) {
    $custom_fields[$def
      ->getId()] = isset($form_state['values']["custom_field_{$def->getId()}"]) ? $form_state['values']["custom_field_{$def->getId()}"] : NULL;
  }
  if ($custom_fields) {
    $video
      ->setCustomFields($custom_fields);
  }
  $schedule = new \Brightcove\Object\Video\Schedule();
  $schedule_set = FALSE;
  if ($form_state['values']['start_date'] !== 'immediately') {
    $datetime = new DateTime($form_state['values']['start_availability_date']);
    $schedule
      ->setStartsAt($datetime
      ->format(DateTime::ISO8601));
    $schedule_set = TRUE;
  }
  if ($form_state['values']['end_date'] !== 'no_end_date') {
    $datetime = new DateTime($form_state['values']['end_availability_date']);
    $schedule
      ->setEndsAt($datetime
      ->format(DateTime::ISO8601));
    $schedule_set = TRUE;
  }
  if ($schedule_set) {
    $video
      ->setSchedule($schedule);
  }
  $link = new \Brightcove\Object\Video\Link();
  $link
    ->setText($form_state['values']['linktext']);
  $link
    ->setUrl($form_state['values']['linkurl']);
  $video
    ->setLink($link);
  $video
    ->setEconomics($form_state['values']['economics']);
  field_attach_submit('brightcove_video', $video_entity, $form['additional_fields'], $form_state);
  $video_entity
    ->save();
  return $video_id;
}