video_embed_facebook.module 5.7 KB

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