You are here

protected function UrlToVideoFilterService::convertYouTubeUrlToEmbedCode in URL to Video Filter 2.0.x

Same name and namespace in other branches
  1. 8 src/Service/UrlToVideoFilterService.php \Drupal\url_to_video_filter\Service\UrlToVideoFilterService::convertYouTubeUrlToEmbedCode()

Converts YouTube URL to HTML placeholders.

Note that ths function only embeds placeholders into the HTML. The actual content, whether that be a clickable thumbnail, or the embedded video itself, is injected into the HTML with JavaScript, replacing the placeholders this function embeds.

Converts for URLs of the following patterns:

  • youtube.com/watch?v=##########
  • youtube.com/embed/###########
  • youtu.be/###########

Parameters

string $url: The YouTube URL to convert.

Return value

string HTML containing the placeholder for the YouTube video for the given URL.

1 call to UrlToVideoFilterService::convertYouTubeUrlToEmbedCode()
UrlToVideoFilterService::convertYouTubeUrls in src/Service/UrlToVideoFilterService.php
Converts URLs to embedded YouTube videos.

File

src/Service/UrlToVideoFilterService.php, line 100

Class

UrlToVideoFilterService
Service class for the URL To Video Filter module.

Namespace

Drupal\url_to_video_filter\Service

Code

protected function convertYouTubeUrlToEmbedCode($url) {
  $embed_code = '';
  if (strpos($url, 'youtube.com/watch')) {
    $video_key = preg_replace('/(http)?(s)?(:\\/\\/)?(www\\.)?youtube\\.com\\/watch\\?v=/', '', $url);
  }
  elseif (strpos($url, 'youtube.com/embed')) {
    $video_key = preg_replace('/(http)?(s)?(:\\/\\/)?(www\\.)?youtube\\.com\\/embed\\//', '', $url);
  }
  elseif (strpos($url, 'youtu.be')) {
    $video_key = preg_replace('/(http)?(s)?(:\\/\\/)?(www\\.)?youtu.be\\//', '', $url);
  }
  $embed_code .= '<span class="url-to-video-container youtube-container no-js">';
  $embed_code .= '<span class="youtube-player url-to-video-player loader" data-youtube-id="' . $video_key . '"></span>';
  $embed_code .= '</span>';
  return $embed_code;
}