70c7f6beb58cea00090af568982d83bdfe2e48ad
[yaffs-website] / web / modules / contrib / video_embed_field / src / Plugin / video_embed_field / Provider / Vimeo.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 Vimeo provider plugin.
9  *
10  * @VideoEmbedProvider(
11  *   id = "vimeo",
12  *   title = @Translation("Vimeo")
13  * )
14  */
15 class Vimeo extends ProviderPluginBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function renderEmbedCode($width, $height, $autoplay) {
21     $iframe = [
22       '#type' => 'video_embed_iframe',
23       '#provider' => 'vimeo',
24       '#url' => sprintf('https://player.vimeo.com/video/%s', $this->getVideoId()),
25       '#query' => [
26         'autoplay' => $autoplay,
27       ],
28       '#attributes' => [
29         'width' => $width,
30         'height' => $height,
31         'frameborder' => '0',
32         'allowfullscreen' => 'allowfullscreen',
33       ],
34     ];
35     if ($time_index = $this->getTimeIndex()) {
36       $iframe['#fragment'] = sprintf('t=%s', $time_index);
37     }
38     return $iframe;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getRemoteThumbnailUrl() {
45     return $this->oEmbedData()->thumbnail_url;
46   }
47
48   /**
49    * Get the vimeo oembed data.
50    *
51    * @return array
52    *   An array of data from the oembed endpoint.
53    */
54   protected function oEmbedData() {
55     return json_decode(file_get_contents('http://vimeo.com/api/oembed.json?url=' . $this->getInput()));
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function getIdFromInput($input) {
62     preg_match('/^https?:\/\/(www\.)?vimeo.com\/(channels\/[a-zA-Z0-9]*\/)?(?<id>[0-9]*)(\/[a-zA-Z0-9]+)?(\#t=(\d+)s)?$/', $input, $matches);
63     return isset($matches['id']) ? $matches['id'] : FALSE;
64   }
65
66   /**
67    * Get the time index from the URL.
68    *
69    * @return string|FALSE
70    *   A time index parameter to pass to the frame or FALSE if none is found.
71    */
72   protected function getTimeIndex() {
73     preg_match('/\#t=(?<time_index>(\d+)s)$/', $this->input, $matches);
74     return isset($matches['time_index']) ? $matches['time_index'] : FALSE;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getName() {
81     return $this->oEmbedData()->title;
82   }
83
84 }