You are here

function _brightcove_field_video_widget_form in Brightcove Video Connect 7.6

Same name and namespace in other branches
  1. 7.7 brightcove_field.video.inc \_brightcove_field_video_widget_form()
  2. 7.3 brightcove_field/brightcove_field.video.inc \_brightcove_field_video_widget_form()
  3. 7.4 brightcove_field/brightcove_field.video.inc \_brightcove_field_video_widget_form()
  4. 7.5 brightcove_field/brightcove_field.video.inc \_brightcove_field_video_widget_form()

Helper function to return the video widget form.

1 call to _brightcove_field_video_widget_form()
brightcove_field_widget_form in ./brightcove.module
Implements hook_field_widget_form().

File

./brightcove_field.video.inc, line 11
Holds all of the video field related methods.

Code

function _brightcove_field_video_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, &$element) {
  $entity_type = $element['#entity_type'];
  $entity_info = entity_get_info($entity_type);
  $eid = isset($form['#entity']->{$entity_info['entity keys']['id']}) ? $form['#entity']->{$entity_info['entity keys']['id']} : NULL;
  $bundle = $element['#bundle'];

  // It needs to be able to work with field collection.
  $parent_delta = array_pop($element['#field_parents']);

  // We need this mixed variable in access checking.
  $entity_id_or_bundle = !empty($eid) ? $eid : $bundle;

  // Select the client.
  $element['#tree'] = TRUE;
  $wrapper_id = _brightcove_field_get_wrapper_name($field['field_name'], $delta);

  // Playlists already determine the client.
  if ($entity_type == 'brightcove_playlist') {

    // Editing a playlist.
    if (!empty($form['#entity']->client) && $form['#entity']->client instanceof Entity) {
      $bcid = $form['#entity']->client->bcid;
    }
    else {
      $bcid = isset($form_state['values']['bcid']) ? $form_state['values']['bcid'] : $form_state['input']['bcid'];
    }
    $element['bcid'] = [
      '#type' => 'value',
      '#value' => $bcid,
    ];
  }
  else {
    module_load_include('inc', 'brightcove', 'brightcove.client');
    $element['bcid'] = brightcove_client_select_element();
    switch ($element['bcid']['#type']) {
      case 'select':
        $element['bcid']['#ajax'] = [
          'wrapper' => $wrapper_id,
          'callback' => 'brightcove_field_video_client_ajax_callback',
        ];
        $bcid = $element['bcid']['#default_value'];
        if (isset($form_state['values'][$field['field_name']][$langcode][$delta]['bcid'])) {
          $bcid = $form_state['values'][$field['field_name']][$langcode][$delta]['bcid'];
        }
        elseif (isset($form_state['input'][$field['field_name']][$langcode][$delta]['bcid'])) {
          $bcid = $form_state['input'][$field['field_name']][$langcode][$delta]['bcid'];
        }
        elseif (isset($items[$delta]['bcid'])) {
          $bcid = $items[$delta]['bcid'];
          if ($bcid === "0" && !empty($items[$delta]['brightcove_id'])) {
            $bcid = variable_get('brightcove_client_default');
          }
        }
        $element['bcid']['#default_value'] = $bcid;
        break;
      case 'value':
        $bcid = $element['bcid']['#value'];
        break;
      default:
        return;
    }
  }
  $element['previous_client'] = [
    '#type' => 'value',
    '#value' => $bcid,
  ];
  if (empty($brightcove_field_settings[$element['#field_name']])) {
    $brightcove_field_settings[$element['#field_name']] = [
      'brightcove_field' => [
        $element['#field_name'] => [
          'entity_type' => $entity_type,
          'field_name' => $element['#field_name'],
          'entity_id' => is_null($eid) ? '0' : $eid,
        ],
      ],
    ];
  }
  if (isset($form_state['values'][$field['field_name']][$langcode][$delta]['previous_client']) && $form_state['values'][$field['field_name']][$langcode][$delta]['previous_client'] !== $bcid) {
    $form_state['input'][$field['field_name']][$langcode][$delta]['brightcove_id'] = '';
    $form_state['values'][$field['field_name']][$langcode][$delta]['brightcove_id'] = '';
  }

  // Since the actions buttons depend on the client selection,
  // we wrap them to update them via AJAX.
  $element['#prefix'] = "<div id=\"{$wrapper_id}\">";
  $element['#suffix'] = '</div>';
  $element['actions'] = [
    '#type' => 'container',
    '#weight' => 99,
  ];
  $client = brightcove_client_load($bcid);
  if (!empty($client)) {
    $element['brightcove_id'] = [
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]['brightcove_id']) ? $items[$delta]['brightcove_id'] : NULL,
      '#title' => $element['#title'],
      '#maxlength' => 256,
      '#value_callback' => 'brightcove_field_video_browser_value',
      '#autocomplete_path' => 'brightcove/autocomplete/videos/' . $bcid,
      '#attributes' => [
        'rel' => [
          $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
        ],
        'class' => [
          'brightcove-video-field',
          $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
        ],
        'data-field-name' => $element['#field_name'],
      ],
      '#element_validate' => [
        'brightcove_field_browser_video_validate',
      ],
      '#field_name' => $element['#field_name'],
      '#description' => $element['#description'],
      '#attached' => [
        'library' => [
          [
            'system',
            'ui.dialog',
          ],
        ],
        'css' => [
          drupal_get_path('module', 'brightcove') . '/styles/brightcove.css',
        ],
        'js' => [
          drupal_get_path('module', 'brightcove') . '/js/brightcove.js',
          [
            'data' => $brightcove_field_settings[$element['#field_name']],
            'type' => 'setting',
          ],
        ],
      ],
    ];
    $player_list = [];
    brightcove_try(function () use (&$player_list, $client) {
      $player_list = brightcove_player_list($client->bcid);
    });

    // Only show player selection on other entities than 'brightcove_playlist'.
    // A video can have a player but a playlist determines the player for the
    // videos in the list.
    if (empty($form_state['values']['field_brightcove_video'][LANGUAGE_NONE][$delta]['player'])) {
      $selected = isset($items[$delta]['player']) ? $items[$delta]['player'] : brightcove_get_default_player($client->bcid);
    }
    else {
      $selected = isset($form_state['values']['field_brightcove_video'][LANGUAGE_NONE][$delta]['player']) ? $form_state['values']['field_brightcove_video'][LANGUAGE_NONE][$delta]['player'] : key($player_list);
    }
    if ($element['#entity_type'] != 'brightcove_playlist') {
      $element['player'] = [
        '#type' => 'select',
        '#title' => t('Player'),
        '#options' => $player_list,
        '#default_value' => $selected,
        '#ajax' => [
          'callback' => 'ajax_dependent_dropdown',
          'wrapper' => 'embed-replace-' . $delta,
        ],
      ];
      $element['embed'] = [
        '#type' => 'select',
        '#title' => t('Child player'),
        '#prefix' => '<div id="embed-replace-' . $delta . '">',
        '#suffix' => '</div>',
        '#options' => brightcove_embed_list($selected, $client),
        '#default_value' => isset($items[$delta]['embed']) ? $items[$delta]['embed'] : 'default',
      ];
    }
    $element['player_customization'] = [
      '#type' => 'fieldset',
      '#title' => t('NEW CHILD PLAYER'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('See the <a href="https://support.brightcove.com/guide-embed-apis">Embed APIs Guide</a> to decide when child players (Embed APIs) should be used.<br />NOTE: Any unsaved settings on this page will be lost when creating a new child player.'),
    ];
    $element['player_customization']['link_to_config'] = [
      '#prefix' => '<div id="link-replace-' . $delta . '"><br />',
      '#suffix' => '</div>',
      '#markup' => l('Create child player', '/admin/config/media/brightcove/players/' . $bcid . '/' . $selected . '/add-embed', [
        'query' => $eid ? [
          'destination' => "{$entity_type}/{$eid}/edit",
        ] : [],
        'attributes' => [
          'class' => 'button',
        ],
      ]),
    ];
    $element['actions']['remove'] = [
      '#type' => 'button',
      '#attributes' => [
        'class' => [
          'brightcove-field-remove-button',
        ],
        'rel' => $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
        'data-entity-type' => $entity_type,
        'data-field-name' => $element['#field_name'],
        'data-entity-id' => is_null($eid) ? '0' : $eid,
      ],
      '#default_value' => t('Remove'),
      '#value_callback' => 'brightcove_field_button_value_callback',
      '#name' => $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
    ];
    if (!isset($element['#default_value'])) {
      $element['actions']['remove']['#attributes']['disabled'] = 'disabled';
    }
    if (brightcove_field_browse_access('browse videos', $entity_type, $field['field_name'], $entity_id_or_bundle, $client)) {

      // Button to browse videos.
      $element['actions']['browse'] = [
        '#type' => 'button',
        '#attributes' => [
          'class' => [
            'brightcove-field-browse-video-button',
          ],
          'rel' => $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
          'data-entity-type' => $entity_type,
          'data-bundle' => $element['#bundle'],
          'data-field-name' => $element['#field_name'],
          'data-entity-id' => is_null($eid) ? '0' : $eid,
          'data-client-id' => $bcid,
        ],
        '#executes_submit_callback' => FALSE,
        '#limit_validation_errors' => [],
        '#default_value' => t('Browse'),
        '#value_callback' => 'brightcove_field_button_value_callback',
        '#ajax' => [
          'callback' => 'ajax_browse_video_dialog_callback',
        ],
        '#name' => $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
        '#disable_field_validation' => TRUE,
      ];
    }
    if (brightcove_field_browse_access('upload videos', $entity_type, $field['field_name'], $entity_id_or_bundle, $client)) {
      $element['actions']['upload'] = [
        '#type' => 'button',
        '#attributes' => [
          'class' => [
            'brightcove-field-upload-button',
          ],
          'rel' => $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
          'data-entity-type' => $entity_type,
          'data-bundle' => $element['#bundle'],
          'data-field-name' => $element['#field_name'],
          'data-entity-id' => is_null($eid) ? '0' : $eid,
          'data-client-id' => $bcid,
        ],
        '#limit_validation_errors' => [],
        '#default_value' => t('Upload'),
        '#value_callback' => 'brightcove_field_button_value_callback',
        '#ajax' => [
          'callback' => 'ajax_upload_video_dialog_callback',
        ],
        '#name' => $element['#field_name'] . '-' . $parent_delta . '-' . $delta,
        '#disable_field_validation' => TRUE,
      ];
    }
  }
}