You are here

public function Wistia::html in Video Filter 8

HTML code of the video player.

Overrides VideoFilterBase::html

File

src/Plugin/VideoFilter/Wistia.php, line 27

Class

Wistia
Provides Wistia codec for Video Filter.

Namespace

Drupal\video_filter\Plugin\VideoFilter

Code

public function html($video) {
  $video_code = $video['codec']['matches'][7];
  $matches = $video['codec']['matches'];
  $embed_type = $matches[4] == 'projects' || $matches[6] == 'playlists' ? 'playlists' : 'iframe';

  // Get embed code via oEmbed.
  $endpoint = 'http://fast.wistia.com/oembed?url=http://fast.wistia.com/embed/' . $embed_type . '/' . $video_code . '&width=' . $video['width'] . '&height=' . $video['height'];
  $request = \Drupal::httpClient()
    ->get($endpoint, [
    'headers' => [
      'Accept' => 'application/json',
    ],
  ]);
  if ($request
    ->getStatusCode() == 200) {
    $response = json_decode($request
      ->getBody());
  }
  $html = !empty($response->html) ? $response->html : '';

  // See if the video source is already an iframe src.
  $pattern = '@https?://fast.wistia.(com|net)/embed/(iframe|playlists)/[a-zA-Z0-9]+\\?+.+@';
  $matches = [];
  if (preg_match($pattern, $video['source'], $matches)) {

    // Replace the oEmbed iframe src with that provided in the token, in order
    // to support embed builder URLs.
    $pattern = '@https?://fast.wistia.(com|net)/embed/(iframe|playlists)/[a-zA-Z0-9]+\\?[^"]+@';
    $replacement = $matches[0];
    $html = preg_replace($pattern, $replacement, $html);
  }
  return $html;
}