1043c5a62aa0eaa192a50e86153ce8f5d5081149
[yaffs-website] / web / modules / contrib / video_embed_field / src / Plugin / video_embed_field / Provider / YouTubePlaylist.php
1 <?php
2
3 namespace Drupal\video_embed_field\Plugin\video_embed_field\Provider;
4
5 use Drupal\video_embed_field\ProviderPluginBase;
6
7 /**
8  * A YouTube playlist video provider.
9  *
10  * @VideoEmbedProvider(
11  *   id = "youtube_playlist",
12  *   title = @Translation("YouTube Playlist")
13  * )
14  */
15 class YouTubePlaylist extends ProviderPluginBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function renderEmbedCode($width, $height, $autoplay) {
21     return [
22       '#type' => 'video_embed_iframe',
23       '#provider' => 'youtube_playlist',
24       '#url' => 'https://www.youtube.com/embed/videoseries',
25       '#query' => [
26         'list' => $this->getVideoId(),
27       ],
28       '#attributes' => [
29         'width' => $width,
30         'height' => $height,
31         'frameborder' => '0',
32         'allowfullscreen' => 'allowfullscreen',
33       ],
34     ];
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function getRemoteThumbnailUrl() {
41     return sprintf('http://img.youtube.com/vi/%s/hqdefault.jpg', static::getUrlComponent($this->getInput(), 'video_id'));
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function getIdFromInput($input) {
48     return static::getUrlComponent($input, 'id');
49   }
50
51   /**
52    * Get a component from the URL.
53    *
54    * @param string $input
55    *   The input URL.
56    * @param string $component
57    *   The component from the regex to get.
58    *
59    * @return string
60    *   The value of the match in the regex.
61    */
62   protected static function getUrlComponent($input, $component) {
63     preg_match('/^https?:\/\/(?:www\.)?youtube\.com\/watch\?(?=.*v=(?<video_id>[0-9A-Za-z_-]*))(?=.*list=(?<id>[A-Za-z0-9_-]*))/', $input, $matches);
64     return isset($matches[$component]) ? $matches[$component] : FALSE;
65   }
66
67 }