View source
<?php
namespace Drupal\video_embed_brightcove\Plugin\video_embed_field\Provider;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\video_embed_field\ProviderPluginBase;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Brightcove extends ProviderPluginBase {
protected $configFactory;
public function __construct($configuration, $plugin_id, $plugin_definition, ClientInterface $http_client, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $http_client);
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('http_client'), $container
->get('config.factory'));
}
public function renderEmbedCode($width, $height, $autoplay) {
if ($autoplay) {
$config = $this->configFactory
->get('video_embed_brightcove.settings');
$player_name = $config
->get('autoplay_player');
}
if (empty($player_name)) {
$player_name = $this
->getPlayerName();
}
return [
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => [
'width' => $width,
'height' => $height,
'frameborder' => '0',
'allowfullscreen' => 'allowfullscreen',
'src' => sprintf('https://players.brightcove.net/%d/%s_default/index.html?videoId=%d', $this
->getPlayerId(), $player_name, $this
->getVideoId(), $autoplay),
],
];
}
public function getRemoteThumbnailUrl() {
$config = $this->configFactory
->get('video_embed_brightcove.settings');
$client_id = $config
->get('client_id');
$client_secret = $config
->get('client_secret');
if (empty($client_id) || empty($client_secret)) {
throw new \Exception('Brightcove API credentials are not set.');
}
$auth_uri = 'https://oauth.brightcove.com/v4/access_token';
$auth_string = base64_encode($client_id . ':' . $client_secret);
$auth_options = [
'headers' => [
'Authorization' => 'Basic ' . $auth_string,
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => 'grant_type=client_credentials',
];
$auth = $this->httpClient
->request('POST', $auth_uri, $auth_options);
if ($auth
->getStatusCode() !== 200) {
throw new \Exception('Brightcove API authentication failed.');
}
$auth_json = json_decode($auth
->getBody()
->getContents());
if (!empty($auth_json->access_token)) {
$video_uri = 'https://cms.api.brightcove.com/v1/accounts/' . $this
->getPlayerId() . '/videos/' . $this
->getVideoId() . '/images';
$video_options = [
'headers' => [
'Authorization' => 'Bearer ' . $auth_json->access_token,
'Content-Type' => 'application/json',
],
];
$video = $this->httpClient
->request('GET', $video_uri, $video_options);
if ($video
->getStatusCode() !== 200) {
throw new \Exception('Brightcove API video request failed.');
}
$video_json = json_decode($video
->getBody()
->getContents());
if (!empty($video_json->poster->src)) {
return $video_json->poster->src;
}
}
throw new \Exception('Brightcove API video thumbnail unavailable.');
}
protected function getPlayerId() {
return static::getUrlMetadata($this
->getInput(), 'player');
}
protected function getPlayerName() {
return static::getUrlMetadata($this
->getInput(), 'player_name');
}
public static function getIdFromInput($input) {
return static::getUrlMetadata($input, 'id');
}
protected static function getUrlMetadata($input, $metadata) {
preg_match('/^https?:\\/\\/players\\.brightcove\\.net\\/(?<player>[0-9]*)\\/(?<player_name>[\\S]*)_default\\/index\\.html\\?videoId\\=(?<id>[0-9]*)?$/', $input, $matches);
return isset($matches[$metadata]) ? $matches[$metadata] : FALSE;
}
}