video_embed_facebook.module 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @file Add a handler for facebook videos to Video Embed Field.
  4. * @see video_embed_field.api.php for more documentation
  5. */
  6. /**
  7. * Implements hook_video_embed_handler_info().
  8. *
  9. * This function is used to tell video_embed_field which functions will be used to handle
  10. * different operations, along with a bit of metadata.
  11. * @return an associative array with the data
  12. * @see video_embed_field.api.php for specific details on the data to return.
  13. */
  14. function video_embed_facebook_video_embed_handler_info() {
  15. $handlers = array();
  16. //the key here should be unique to our handler, normally the name of the service will suffice
  17. $handlers['facebook'] = array(
  18. 'title' => 'Facebook Video', //The title is the name to show to users
  19. //function is a function to take the url and return the embed code
  20. 'function' => 'video_embed_facebook_handle_video',
  21. //thumbnail_function is optional and takes the url and returns the thumbnail url
  22. 'thumbnail_function' => 'video_embed_facebook_handle_thumbnail',
  23. //data_function is optional and returns an array of extra data for the given video url
  24. //because facebook requires oath to get this data, we'll leave it out for now
  25. //'data_function' => 'video_embed_facebook_handle_data',
  26. //form is the configure form to embed into video_embed styles - this is where all settings should go
  27. 'form' => 'video_embed_facebook_form',
  28. //domains is how video embed determines which handler to use, its an array of domains to match
  29. //urls against. Don't include the scheme (like http://) or www.
  30. 'domains' => array(
  31. 'facebook.com',
  32. ),
  33. //defaults are the defaults to provide to your form (as defined in your form callback)
  34. 'defaults' => array(
  35. 'width' => 640,
  36. 'height' => 360,
  37. 'allowfullscreen' => TRUE,
  38. ),
  39. );
  40. return $handlers;
  41. }
  42. /**
  43. * Our configuration form (the callback for the form key in info)
  44. * Provide a form to configure out video settings
  45. * @param $defaults - default/current values for your provider, the currently saved settings
  46. * with empty values filled with the defaults provided in info hook
  47. * @return a form as defined by forms api
  48. */
  49. function video_embed_facebook_form($defaults) {
  50. $form = array();
  51. //form element for the width of the player - note we're using the default from defaults
  52. $form['height'] = array(
  53. '#type' => 'textfield',
  54. '#title' => t('Player Width'),
  55. '#description' => t('The width of the player.'),
  56. '#default_value' => $defaults['height'],
  57. );
  58. //element for width
  59. $form['width'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Player Width'),
  62. '#description' => t('The width of the player.'),
  63. '#default_value' => $defaults['width'],
  64. );
  65. //allow configuration of fullscreen
  66. $form['allowfullscreen'] = array(
  67. '#type' => 'checkbox',
  68. '#title' => t('Allow Fullscreen'),
  69. '#desecription' => t('This will allow the video to be fullscreened.'),
  70. '#default_value' => $defaults['allowfullscreen'],
  71. );
  72. return $form;
  73. }
  74. /**
  75. * This is the video handler (the 'function' key from handler_info)
  76. * @param $url - the full video url
  77. * @param $settings - an associative array of this handlers settings, from the settings form
  78. * @return - the embed code for the video
  79. */
  80. function video_embed_facebook_handle_video($url, $settings) {
  81. $id = _video_embed_facebook_get_video_id($url);
  82. if ($id) {
  83. //our embed code
  84. $embed = '<object>
  85. <param name="allowfullscreen" value="!fullscreen" />
  86. <param name="allowscriptaccess" value="always" />
  87. <param name="movie" value="https://www.facebook.com/v/!id" />
  88. <param name="wmode" value="opaque" />
  89. <embed src="https://www.facebook.com/v/!id" type="application/x-shockwave-flash" wmode="opaque"
  90. allowscriptaccess="always" allowfullscreen="!fullscreen" width="!width" height="!height">
  91. </embed>
  92. </object>';
  93. //use format_string to replace our placeholders with the values from the settings
  94. $embed = format_string($embed, array(
  95. '!id' => $id,
  96. '!fullscreen' => $settings['allowfullscreen'] ? 'true' : 'false',
  97. '!width' => $settings['width'],
  98. '!height' => $settings['height'],
  99. ));
  100. //we want to return a render array
  101. $video = array(
  102. '#markup' => $embed,
  103. );
  104. return $video;
  105. }
  106. // just return an empty string if there is no id, so we don't have broken embeds showing up
  107. return '';
  108. }
  109. /**
  110. * Retreive the thumbnail for the facebook video - note that based on a users permissions, this may be
  111. * the facebook unknown thumbnail: https://s-static.ak.facebook.com/rsrc.php/v1/y0/r/XsEg9L6Ie5_.jpg
  112. * @param $url - the url of the video as entered by the user
  113. * @return an array with the keys:
  114. * 'id' => an id for the video which is unique to your provider, used for naming the cached thumbnail file
  115. * 'url' => the url to retrieve the thumbnail from
  116. */
  117. function video_embed_facebook_handle_thumbnail($url) {
  118. $id = _video_embed_facebook_get_video_id($url);
  119. // We're using a part of the graphs api to return the thumbnail. This only works for some videos
  120. return array(
  121. 'id' => $id, //generally the id that the provider uses for the video
  122. 'url' => 'https://graph.facebook.com/' . $id . '/picture', //the url of the thumbnail
  123. );
  124. }
  125. /**
  126. * Helper function to take a facebook video url and return its id
  127. * @param $url - the full facebook video url
  128. * @return the id for the video
  129. */
  130. function _video_embed_facebook_get_video_id($url) {
  131. //parse_url is an easy way to break a url into its components
  132. $matches = array();
  133. preg_match('/(.*)?v=([^&#]*)/', $url, $matches);
  134. //if the v get parameter is set, return it
  135. if ($matches && !empty($matches[2])) {
  136. return $matches[2];
  137. }
  138. //otherwise return false.
  139. return FALSE;
  140. }