5 * Contains \Drupal\video\Plugin\video\Provider\YouTube.
8 namespace Drupal\video\Plugin\video\Provider;
10 use Drupal\video\ProviderPluginBase;
11 use GuzzleHttp\Exception\ClientException;
14 * @VideoEmbeddableProvider(
16 * label = @Translation("YouTube"),
17 * description = @Translation("YouTube Video Provider"),
18 * regular_expressions = {
19 * "@(?:(?<protocol>http|https):)?//(?:www\.)?youtube(?<cookie>-nocookie)?\.com/embed/(?<id>[a-z0-9_-]+)@i",
20 * "@(?:(?<protocol>http|https):)?//(?:www\.)?youtube(?<cookie>-nocookie)?\.com/v/(?<id>[a-z0-9_-]+)@i",
21 * "@(?:(?<protocol>http|https):)?//(?:www\.)?youtube(?<cookie>-nocookie)?\.com/watch(\?|\?.*\&)v=(?<id>[a-z0-9_-]+)@i",
22 * "@(?:(?<protocol>http|https):)?//youtu(?<cookie>-nocookie)?\.be/(?<id>[a-z0-9_-]+)@i"
24 * mimetype = "video/youtube",
25 * stream_wrapper = "youtube"
28 class YouTube extends ProviderPluginBase {
33 public function renderEmbedCode($settings) {
34 $file = $this->getVideoFile();
35 $data = $this->getVideoMetadata();
37 '#type' => 'html_tag',
40 'width' => $settings['width'],
41 'height' => $settings['height'],
43 'allowfullscreen' => 'allowfullscreen',
44 'src' => sprintf('https://www.youtube.com/embed/%s?autoplay=%d&start=%d', $data['id'], $settings['autoplay'], NULL),
52 public function getRemoteThumbnailUrl() {
53 $data = $this->getVideoMetadata();
56 // Sometimes the video has not every version of thumbnails. Guzzle throws
57 // exception at that time. Now catch it, and try download another size of
60 'http://img.youtube.com/vi/' . $data['id'] . "/maxresdefault.jpg",
61 'http://img.youtube.com/vi/' . $data['id'] . "/hqdefault.jpg",
62 'http://img.youtube.com/vi/' . $data['id'] . "/default.jpg",
65 foreach ($img_urls as $url) {
67 $this->httpClient->request('GET', $url);
69 catch (ClientException $e) {
71 if ($e->getCode() != 404) {