You are here

function _brightcove_get_player_field in Brightcove Video Connect 8.2

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

Gets a player field for the given entity.

Parameters

\Drupal\brightcove\BrightcoveVideoPlaylistCMSEntityInterface $entity:

Return value

array|null Renderable array for the player field.

2 calls to _brightcove_get_player_field()
brightcove_brightcove_playlist_view in ./brightcove.module
Implements hook_ENTITY_TYPE_view().
brightcove_brightcove_video_view in ./brightcove.module
Implements hook_ENTITY_TYPE_view().

File

./brightcove.module, line 240
Brightcove module.

Code

function _brightcove_get_player_field(BrightcoveVideoPlaylistCMSEntityInterface $entity) {
  $player_field = NULL;

  // Only display the player if we have all the info needed for it.
  $id = $entity
    ->getBrightcoveId();
  if (!empty($id)) {

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

    /** @var \Drupal\brightcove\Entity\BrightcovePlayer $player */
    if ($player = $entity
      ->getPlayer()) {
      $player = BrightcovePlayer::load($player);
    }
    if (!$player) {
      $player = BrightcovePlayer::loadByPlayerId($api_client
        ->getDefaultPlayer());
    }
    if (version_compare($player
      ->getVersion(), '6') >= 0) {
      $player_field = [
        '#theme' => [
          $player
            ->isResponsive() ? 'brightcove_player_responsive' : 'brightcove_player_fixed',
        ],
        '#account' => $api_client
          ->getAccountId(),
        '#data_usage' => 'cms:drupal:' . \DRUPAL::VERSION . ':' . system_get_info('module', 'brightcove')['version'] . ':javascript',
        '#id' => $id,
        '#is_playlist' => $player
          ->isPlaylist(),
        '#player' => BrightcoveUtil::getDefaultPlayer($entity),
        '#type' => $entity instanceof BrightcovePlaylistInterface ? 'playlist' : 'video',
      ];
      if ($player
        ->isResponsive()) {
        $player_field += [
          '#max_width' => !empty($player) ? $player
            ->getWidth() : 100,
          '#units' => !empty($player) ? $player
            ->getUnits() : '%',
        ];
      }
      else {
        $player_field += [
          '#height' => !empty($player) ? $player
            ->getHeight() : NULL,
          '#width' => !empty($player) ? $player
            ->getWidth() : NULL,
          '#units' => !empty($player) ? $player
            ->getUnits() : 'px',
        ];
      }
    }
    elseif (\Drupal::currentUser()
      ->hasPermission('administer brightcove configuration')) {

      // Show message for administrators.
      $player_field = [
        '#type' => 'item',
        '#markup' => t('Players below version 6.x are no longer supported. Please <a href=":studio_url" target="_blank">upgrade the player</a> in the Brightcove Studio or <a href=":edit_form">choose a different player</a> for this video.', [
          ':studio_url' => 'https://studio.brightcove.com/products/videocloud/players/players/{$player->getPlayerId()}?new_play_edit=false',
          ':edit_form' => Url::fromRoute('entity.brightcove_video.edit_form', [
            'brightcove_video' => $entity
              ->id(),
          ], [
            'fragment' => 'edit-player',
          ])
            ->toString(),
        ]),
        '#wrapper_attributes' => [
          'class' => [
            'messages',
            'messages--warning',
          ],
        ],
      ];
    }
    else {

      // Show message for users.
      $player_field = [
        '#type' => 'item',
        '#markup' => t('Unsupported player, cannot display video.'),
        '#wrapper_attributes' => [
          'class' => [
            'messages',
            'messages--warning',
          ],
        ],
      ];
      \Drupal::logger('brightcove')
        ->warning(strtr('Players below version 6.x are no longer supported. Please <a href=":studio_url">upgrade the player</a> in the Brightcove Studio or <a href=":edit_form">choose a different player</a> for the related video.', [
        ':studio_url' => 'https://studio.brightcove.com/products/videocloud/players/players/{$player->getPlayerId()}?new_play_edit=false',
        ':edit_form' => Url::fromRoute('entity.brightcove_video.edit_form', [
          'brightcove_video' => $entity
            ->id(),
        ], [
          'fragment' => 'edit-player',
        ])
          ->toString(),
      ]));
    }
  }
  return $player_field;
}