Version 1
[yaffs-website] / web / modules / contrib / video / src / Plugin / video / Provider / Facebook.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\video\Plugin\video\Provider\Facebook.
6  */
7
8 namespace Drupal\video\Plugin\video\Provider;
9
10 use Drupal\video\ProviderPluginBase;
11
12 /**
13  * @VideoEmbeddableProvider(
14  *   id = "facebook",
15  *   label = @Translation("Facebook"),
16  *   description = @Translation("Facebook Video Provider"),
17  *   regular_expressions = {
18  *     "@^https?://www\.facebook\.com/.*(/videos/(?<id>\d+))@i",
19  *     "@^https?://www\.facebook\.com/.*(video\.php\?v=(?<id>\d+))@i"
20  *   },
21  *   mimetype = "video/facebook",
22  *   stream_wrapper = "facebook"
23  * )
24  */
25
26 class Facebook extends ProviderPluginBase {
27   /**
28    * {@inheritdoc}
29    */
30   public function renderEmbedCode($settings) {
31     $file = $this->getVideoFile();
32     $data = $this->getVideoMetadata();
33     // @see https://developers.facebook.com/docs/plugins/embedded-video-player
34     return [
35       '#type' => 'html_tag',
36       '#tag' => 'div',
37       '0' => array(
38         '#type' => 'html_tag',
39         '#tag' => 'script',
40         '#value' => 'window.fbAsyncInit = function() {
41         FB.init({
42                 xfbml      : true,
43                 version    : \'v2.3\'
44         });
45         }; (function(d, s, id){
46                 var js, fjs = d.getElementsByTagName(s)[0];
47                 if (d.getElementById(id)) {return;}
48                 js = d.createElement(s); js.id = id;
49                 js.src = "//connect.facebook.net/en_US/sdk.js";
50                 fjs.parentNode.insertBefore(js, fjs);
51         }(document, \'script\', \'facebook-jssdk\'));',
52       ),
53       '1' => array(
54         '#type' => 'html_tag',
55         '#tag' => 'div',
56         '#attributes' => array(
57           'class' => 'fb-video',
58           'data-href' => sprintf('https://www.facebook.com/video.php?v=%s', $data['id']),
59           'data-width' => $settings['width'],
60           'data-autoplay' => $settings['autoplay'] ? 1 : 0
61         ),
62       ),
63     ];
64   }
65   
66   /**
67    * {@inheritdoc}
68    */
69   public function getRemoteThumbnailUrl() {
70     $data = $this->getVideoMetadata();
71     return 'https://graph.facebook.com/' . $data['id'] . '/picture';
72   }
73 }