3 namespace Drupal\video_embed_field\Plugin\video_embed_field\Provider;
5 use Drupal\video_embed_field\ProviderPluginBase;
8 * A YouTube provider plugin.
10 * @VideoEmbedProvider(
12 * title = @Translation("YouTube")
15 class YouTube extends ProviderPluginBase {
20 public function renderEmbedCode($width, $height, $autoplay) {
22 '#type' => 'video_embed_iframe',
23 '#provider' => 'youtube',
24 '#url' => sprintf('https://www.youtube.com/embed/%s', $this->getVideoId()),
26 'autoplay' => $autoplay,
27 'start' => $this->getTimeIndex(),
34 'allowfullscreen' => 'allowfullscreen',
37 if ($language = $this->getLanguagePreference()) {
38 $embed_code['#query']['cc_lang_pref'] = $language;
44 * Get the time index for when the given video starts.
47 * The time index where the video should start based on the URL.
49 protected function getTimeIndex() {
50 preg_match('/[&\?]t=(?<timeindex>\d+)/', $this->getInput(), $matches);
51 return isset($matches['timeindex']) ? $matches['timeindex'] : 0;
55 * Extract the language preference from the URL for use in closed captioning.
57 * @return string|FALSE
58 * The language preference if one exists or FALSE if one could not be found.
60 protected function getLanguagePreference() {
61 preg_match('/[&\?]hl=(?<language>[a-z\-]*)/', $this->getInput(), $matches);
62 return isset($matches['language']) ? $matches['language'] : FALSE;
68 public function getRemoteThumbnailUrl() {
69 $url = 'http://img.youtube.com/vi/%s/%s.jpg';
70 $high_resolution = sprintf($url, $this->getVideoId(), 'maxresdefault');
71 $backup = sprintf($url, $this->getVideoId(), 'mqdefault');
73 $this->httpClient->head($high_resolution);
74 return $high_resolution;
76 catch (\Exception $e) {
84 public static function getIdFromInput($input) {
85 preg_match('/^https?:\/\/(www\.)?((?!.*list=)youtube\.com\/watch\?.*v=|youtu\.be\/)(?<id>[0-9A-Za-z_-]*)/', $input, $matches);
86 return isset($matches['id']) ? $matches['id'] : FALSE;