X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fvideo_embed_field%2Fsrc%2FPlugin%2Fvideo_embed_field%2FProvider%2FYouTube.php;fp=web%2Fmodules%2Fcontrib%2Fvideo_embed_field%2Fsrc%2FPlugin%2Fvideo_embed_field%2FProvider%2FYouTube.php;h=aa11aeac98a65e24336e0ef21515d84cc4b95866;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/YouTube.php b/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/YouTube.php new file mode 100644 index 000000000..aa11aeac9 --- /dev/null +++ b/web/modules/contrib/video_embed_field/src/Plugin/video_embed_field/Provider/YouTube.php @@ -0,0 +1,89 @@ + 'video_embed_iframe', + '#provider' => 'youtube', + '#url' => sprintf('https://www.youtube.com/embed/%s', $this->getVideoId()), + '#query' => [ + 'autoplay' => $autoplay, + 'start' => $this->getTimeIndex(), + 'rel' => '0', + ], + '#attributes' => [ + 'width' => $width, + 'height' => $height, + 'frameborder' => '0', + 'allowfullscreen' => 'allowfullscreen', + ], + ]; + if ($language = $this->getLanguagePreference()) { + $embed_code['#query']['cc_lang_pref'] = $language; + } + return $embed_code; + } + + /** + * Get the time index for when the given video starts. + * + * @return int + * The time index where the video should start based on the URL. + */ + protected function getTimeIndex() { + preg_match('/[&\?]t=(?\d+)/', $this->getInput(), $matches); + return isset($matches['timeindex']) ? $matches['timeindex'] : 0; + } + + /** + * Extract the language preference from the URL for use in closed captioning. + * + * @return string|FALSE + * The language preference if one exists or FALSE if one could not be found. + */ + protected function getLanguagePreference() { + preg_match('/[&\?]hl=(?[a-z\-]*)/', $this->getInput(), $matches); + return isset($matches['language']) ? $matches['language'] : FALSE; + } + + /** + * {@inheritdoc} + */ + public function getRemoteThumbnailUrl() { + $url = 'http://img.youtube.com/vi/%s/%s.jpg'; + $high_resolution = sprintf($url, $this->getVideoId(), 'maxresdefault'); + $backup = sprintf($url, $this->getVideoId(), 'mqdefault'); + try { + $this->httpClient->head($high_resolution); + return $high_resolution; + } + catch (\Exception $e) { + return $backup; + } + } + + /** + * {@inheritdoc} + */ + public static function getIdFromInput($input) { + preg_match('/^https?:\/\/(www\.)?((?!.*list=)youtube\.com\/watch\?.*v=|youtu\.be\/)(?[0-9A-Za-z_-]*)/', $input, $matches); + return isset($matches['id']) ? $matches['id'] : FALSE; + } + +}