You are here

function brightcove_brightcove_video_view in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 brightcove.module \brightcove_brightcove_video_view()
  2. 3.x brightcove.module \brightcove_brightcove_video_view()

Implements hook_ENTITY_TYPE_view().

File

./brightcove.module, line 192
Brightcove module.

Code

function brightcove_brightcove_video_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('brightcove_video_player')) {

    /** @var \Drupal\brightcove\Entity\BrightcoveVideo $video_entity */
    $video_entity = $entity;

    // Only display the video player if we have all the info needed for it.
    if ($video_id = $video_entity
      ->getVideoId()) {

      /** @var \Drupal\brightcove\Entity\BrightcoveApiClient $api_client */
      $api_client = BrightcoveAPIClient::load($video_entity
        ->getApiClient());

      /** @var \Drupal\brightcove\Entity\BrightcovePlayer $player */
      if ($player = $video_entity
        ->getPlayer()) {
        $player = BrightcovePlayer::load($player);
      }
      if (!$player) {
        $player = BrightcovePlayer::loadByPlayerId($api_client
          ->getDefaultPlayer());
      }
      $build['brightcove_video_player'] = [
        '#theme' => [
          'brightcove_video_player',
        ],
        '#video_id' => $video_id,
        '#account' => $api_client
          ->getAccountId(),
        '#player' => BrightcoveUtil::getDefaultPlayer($video_entity),
        '#height' => !empty($player) ? $player
          ->getHeight() : NULL,
        '#width' => !empty($player) ? $player
          ->getWidth() : NULL,
        '#data_usage' => 'cms:drupal:' . \DRUPAL::VERSION . ':' . system_get_info('module', 'brightcove')['version'] . ':javascript',
      ];
    }
  }
  if ($display
    ->getComponent('brightcove_custom_fields')) {

    /** @var \Drupal\brightcove\Entity\BrightcoveVideo $video_entity */
    $video_entity = $entity;
    $custom_fields = BrightcoveCustomField::loadMultipleByApiClient($video_entity
      ->getApiClient());

    // Show custom fields in the extra field, if it exists and has a value.
    $custom_field_values = $video_entity
      ->getCustomFieldValues();
    $build['brightcove_custom_fields'] = [];
    foreach ($custom_fields as $custom_field) {
      $custom_field_id = $custom_field
        ->getCustomFieldId();
      if (empty($custom_field_values[$custom_field_id])) {
        continue;
      }
      $build['brightcove_custom_fields'][$custom_field_id] = [
        '#type' => 'item',
        '#title' => $custom_field
          ->getName(),
        '#markup' => $custom_field_values[$custom_field_id],
      ];
    }
  }
}