You are here

function _brightcove_upload_form in Brightcove Video Connect 7.6

Same name and namespace in other branches
  1. 7.7 brightcove.video.inc \_brightcove_upload_form()
  2. 7.3 brightcove.module \_brightcove_upload_form()
  3. 7.4 brightcove.module \_brightcove_upload_form()
  4. 7.5 brightcove.module \_brightcove_upload_form()
2 calls to _brightcove_upload_form()
brightcove_media_upload_form in brightcove_media/brightcove_media.module
Upload form for brightcove media.
brightcove_video_form in ./brightcove.video.inc
Brightcove video entity form.

File

./brightcove.video.inc, line 116
Brightcove video related functions.

Code

function _brightcove_upload_form(&$form_state, $video_entity, $video_id = NULL) {
  $client = $video_entity->client;
  $form = [];
  $form['#entity'] = $video_entity;
  $form['#entity_type'] = 'brightcove_video';
  $form['#bundle'] = $video_entity->type;
  if (empty($video_id)) {
    $form['file_upload'] = [
      '#type' => 'file',
      '#title' => t('Video') . ' <span class="form-required" title="' . t('This field is required.') . '">*</span>',
      '#size' => 40,
    ];
    $form['poster'] = [
      '#type' => 'file',
      '#title' => t('Poster image'),
      '#size' => 40,
    ];
    $form['thumbnail'] = [
      '#type' => 'file',
      '#title' => t('Thumbnail image'),
      '#size' => 40,
    ];
    $form['captions'] = [
      '#type' => 'container',
      '#prefix' => '<div id="caption-container">',
      '#suffix' => '</div>',
    ];
    $captions = isset($form_state['caption_num']) ? $form_state['caption_num'] : 0;
    $form_state['caption_num'] = $captions;
    for ($i = 0; $i < $captions; $i++) {
      $form['captions']["caption_{$i}"] = [
        '#type' => 'fieldset',
        '#title' => t('Caption'),
      ];
      $form['captions']["caption_{$i}"]["caption_{$i}_label"] = [
        '#type' => 'textfield',
        '#title' => t('Label'),
        '#required' => TRUE,
      ];
      $form['captions']["caption_{$i}"]["caption_{$i}_file"] = [
        '#type' => 'file',
        '#title' => t('WebVTT caption file'),
        '#description' => t('.vtt extension is required'),
        '#size' => 40,
      ];
      $form['captions']["caption_{$i}"]["caption_{$i}_srclang"] = [
        '#type' => 'textfield',
        '#title' => t('Source language code'),
        '#required' => TRUE,
        '#description' => t("Example: en"),
      ];
      $form['captions']["caption_{$i}"]["caption_{$i}_kind"] = [
        '#type' => 'select',
        '#title' => t('Kind'),
        '#options' => [
          'captions' => t('Captions'),
          'subtitles' => t('Subtitles'),
          'descriptions' => t('Descriptions'),
          'chapters' => t('Chapters'),
          'metadata' => t('Metadata'),
        ],
      ];
      $form['captions']["caption_{$i}"]["caption_{$i}_default"] = [
        '#type' => 'checkbox',
        '#title' => t("Default"),
      ];
    }
    $form['captions']['add_caption'] = [
      '#type' => 'submit',
      '#value' => t('Add caption'),
      '#submit' => [
        '_brightcove_video_form_add_caption_submit',
      ],
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => '_brightcove_video_form_add_caption',
        'wrapper' => 'caption-container',
      ],
    ];
    if (variable_get('brightcove_3rdparty', FALSE)) {
      $form['captions']['3rdparty'] = [
        '#type' => 'container',
        'default' => [
          '#markup' => t('Note: automatic caption generation is available as a 3rd party integration on the !link.', [
            '!link' => l(t('Brightcove Video Connect page'), 'https://www.drupal.org/project/brightcove'),
          ]),
        ],
      ];
    }
  }
  $form['client'] = [
    '#type' => 'hidden',
    '#value' => $client->bcid,
  ];
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
    '#default_value' => !empty($form_state['values']['title']) ? $form_state['values']['title'] : '',
  ];
  $form['short'] = [
    '#type' => 'textarea',
    '#rows' => 2,
    '#title' => t('Short description'),
    '#default_value' => !empty($form_state['values']['short']) ? $form_state['values']['short'] : '',
  ];
  $form['long'] = [
    '#type' => 'textarea',
    '#rows' => 2,
    '#title' => t('Long description'),
    '#default_value' => !empty($form_state['values']['long']) ? $form_state['values']['long'] : '',
  ];
  $form['reference_id'] = [
    '#type' => 'textfield',
    '#title' => t('Reference ID'),
    '#default_value' => !empty($form_state['values']['reference_id']) ? $form_state['values']['reference_id'] : '',
    '#description' => t('Leave empty to generate ID automatically.'),
  ];
  $form['tags'] = [
    '#type' => 'textfield',
    '#title' => t('Tags'),
    '#description' => t('Separate tags with a comma; no tag > 128 characters'),
    '#default_value' => !empty($form_state['values']['tags']) ? $form_state['values']['tags'] : '',
  ];
  $form['state'] = [
    '#type' => 'select',
    '#title' => t('State'),
    '#options' => [
      'ACTIVE' => t('Active'),
      'INACTIVE' => t('Inactive'),
    ],
    '#default_value' => !empty($form_state['values']['state']) ? $form_state['values']['state'] : TRUE,
  ];
  $form['custom_fields'] = [
    '#type' => 'fieldset',
    '#title' => t('Custom fields'),
    '#collapsible' => TRUE,
  ];
  try {

    /** @var \Brightcove\API\CMS $cms */
    list($cms, ) = brightcove_create_classes($client);

    /** @var \Brightcove\Object\CustomFields $fields */
    $fields = $cms
      ->getVideoFields();
    $has_required = FALSE;
    $types = [
      'string' => 'textfield',
      'enum' => 'select',
    ];
    $has_custom_fields = FALSE;
    foreach ($fields
      ->getCustomFields() as $field) {

      /** @var \Brightcove\Object\CustomField $field */
      $has_custom_fields = TRUE;
      $has_required = $has_required || $field
        ->isRequired();
      $form['custom_fields']["custom_field_{$field->getId()}"] = [
        '#title' => $field
          ->getDisplayName(),
        '#description' => $field
          ->getDescription(),
        '#required' => $field
          ->isRequired(),
        '#type' => $types[$field
          ->getType()],
        '#default_value' => isset($form_state['values']["custom_field_{$field->getId()}"]) ? $form_state['values']["custom_field_{$field->getId()}"] : NULL,
      ];
      if ($field
        ->getType() === 'enum') {
        $enum_values = drupal_map_assoc($field
          ->getEnumValues());

        // Show elements in custom fields in alphabetical order like in Video Cloud Studio.
        ksort($enum_values);
        $form['custom_fields']["custom_field_{$field->getId()}"]['#options'] = [
          NULL => t('- no value set -'),
        ] + $enum_values;
      }
    }
    $form['custom_fields']['#collapsed'] = !$has_required;
    $form['custom_fields']['#access'] = $has_custom_fields;
  } catch (\Brightcove\API\Exception\APIException $ex) {
    $form['custom_fields']['#access'] = FALSE;
  } catch (\Brightcove\API\Exception\AuthenticationException $ex) {
    return [
      'authfail' => [
        '#type' => 'markup',
        '#markup' => t('Failed to authenticate with Brightcove. Go to !settings-link to check the settings of the selected client.', [
          '!settings-link' => l(t('Edit @client', [
            '@client' => $client->label,
          ]), "admin/config/media/brightcove/client/manage/{$client->bcid}"),
        ]),
      ],
    ];
  }
  $form['schedule'] = [
    '#type' => 'fieldset',
    '#title' => t('Availability'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#attached' => [
      'js' => [
        drupal_get_path('module', 'brightcove') . '/js/schedule.js',
      ],
      'css' => [
        drupal_get_path('module', 'brightcove') . '/styles/schedule.css',
      ],
    ],
  ];
  $form['schedule']['start_date'] = [
    '#type' => 'radios',
    '#title' => t('Starting'),
    '#default_value' => isset($form_state['values']['start_date']) ? $form_state['values']['start_date'] : 'immediately',
    '#options' => [
      'immediately' => t('Immediately'),
      'date_set' => t('Starting from'),
    ],
  ];
  $form['schedule']['start_availability_date'] = [
    '#type' => 'date_popup',
    '#date_format' => 'Y-m-d H:i',
    '#date_timezone' => date_default_timezone(),
    '#date_label_position' => 'hidden',
    '#default_value' => !empty($form_state['values']['start_availability_date']) ? $form_state['values']['start_availability_date'] : format_date(REQUEST_TIME, 'custom', 'Y-m-d') . ' 00:00:00',
    '#prefix' => '<div id="brightcove-start-availability-date">',
    '#suffix' => '</div>',
    '#pre_render' => [
      '_brightcove_schedule_start_end_date_pre_render',
    ],
    '#states' => [
      'visible' => [
        'input[name="start_date"]' => [
          'value' => 'date_set',
        ],
      ],
    ],
  ];
  $form['schedule']['end_date'] = [
    '#type' => 'radios',
    '#title' => t('Ending'),
    '#default_value' => isset($form_state['values']['end_date']) ? $form_state['values']['end_date'] : 'no_end_date',
    '#options' => [
      'no_end_date' => t('No end date'),
      'date_set' => t('Until'),
    ],
  ];
  $form['schedule']['end_availability_date'] = [
    '#type' => 'date_popup',
    '#date_format' => 'Y-m-d H:i',
    '#date_timezone' => date_default_timezone(),
    '#date_label_position' => 'hidden',
    '#default_value' => !empty($form_state['values']['end_availability_date']) ? $form_state['values']['end_availability_date'] : format_date(REQUEST_TIME, 'custom', 'Y-m-d') . ' 23:59:59',
    '#prefix' => '<div id="brightcove-end-availability-date">',
    '#suffix' => '</div>',
    '#pre_render' => [
      '_brightcove_schedule_start_end_date_pre_render',
    ],
    '#states' => [
      'visible' => [
        'input[name="end_date"]' => [
          'value' => 'date_set',
        ],
      ],
    ],
  ];
  $form['advanced'] = [
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Advanced'),
  ];
  $form['advanced']['linktext'] = [
    '#type' => 'textfield',
    '#title' => t('Related link text'),
    '#default_value' => !empty($form_state['values']['linktext']) ? $form_state['values']['linktext'] : '',
  ];
  $form['advanced']['linkurl'] = [
    '#type' => 'textfield',
    '#title' => t('Related link url'),
    '#default_value' => !empty($form_state['values']['linkurl']) ? $form_state['values']['linkurl'] : '',
  ];
  $form['advanced']['economics'] = [
    '#type' => 'select',
    '#title' => t('Economic model'),
    '#options' => [
      BRIGHTCOVE_ECONOMICS_FREE => t('No advertising'),
      BRIGHTCOVE_ECONOMICS_AD_SUPPORTED => t('Advertising'),
    ],
    '#description' => t('If set to "Advertising", ads may be shown when viewers watch this video'),
    '#default_value' => !empty($form_state['values']['economics']) ? $form_state['values']['economics'] : BRIGHTCOVE_ECONOMICS_FREE,
  ];
  if (empty($video_id)) {
    $form['advanced']['profile'] = [
      '#type' => 'select',
      '#title' => t('Video encoding profile'),
      '#options' => drupal_map_assoc(brightcove_standard_profile_list()),
    ];
  }
  $form['additional_fields'] = [
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Additional fields'),
  ];

  // Attach additional fields.
  // Edit form vs upload form.
  $entity = brightcove_video_load_by_video_id($video_id, $client);
  field_attach_form('brightcove_video', $entity, $form['additional_fields'], $form_state);
  return $form;
}