video_embed_facebook.module 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @file
  4. * Adds a handler for Facebook videos to Video Embed Field.
  5. *
  6. * @see video_embed_field.api.php for more documentation
  7. */
  8. /**
  9. * Implements hook_video_embed_handler_info().
  10. */
  11. function video_embed_facebook_video_embed_handler_info() {
  12. $handlers = array();
  13. $handlers['facebook'] = array(
  14. 'title' => 'Facebook Video',
  15. 'function' => 'video_embed_facebook_handle_video',
  16. 'thumbnail_function' => 'video_embed_facebook_handle_thumbnail',
  17. 'thumbnail_default' => drupal_get_path('module', 'video_embed_facebook') . '/img/facebook.jpg',
  18. 'form' => 'video_embed_facebook_form',
  19. 'form_validate' => 'video_embed_field_handler_youtube_form_validate',
  20. 'domains' => array(
  21. 'facebook.com',
  22. ),
  23. 'defaults' => array(
  24. 'width' => 640,
  25. 'height' => 360,
  26. ),
  27. );
  28. return $handlers;
  29. }
  30. /**
  31. * Defines the form elements for the Facebook videos configuration form.
  32. *
  33. * @param array $defaults
  34. * The form default values.
  35. *
  36. * @return array
  37. * The provider settings form array.
  38. */
  39. function video_embed_facebook_form($defaults) {
  40. $form = array();
  41. $form['width'] = array(
  42. '#type' => 'textfield',
  43. '#title' => t('Player Width'),
  44. '#description' => t('The width of the player.'),
  45. '#default_value' => $defaults['width'],
  46. );
  47. $form['height'] = array(
  48. '#type' => 'textfield',
  49. '#title' => t('Player Height'),
  50. '#description' => t('The height of the player.'),
  51. '#default_value' => $defaults['height'],
  52. );
  53. return $form;
  54. }
  55. /**
  56. * Validates the form elements for the Facebook video configuration form.
  57. *
  58. * @param array $element
  59. * The form element to validate.
  60. * @param array $form_state
  61. * The form to validate state.
  62. * @param array $form
  63. * The form to validate structure.
  64. */
  65. function video_embed_field_handler_facebook_form_validate($element, &$form_state, $form) {
  66. video_embed_field_validate_dimensions($element);
  67. }
  68. /**
  69. * Handler for Facebook videos.
  70. *
  71. * @param string $url
  72. * The video URL.
  73. * @param array $settings
  74. * The settings array.
  75. *
  76. * @return string|bool
  77. * The video iframe, or FALSE in case the ID can't be retrieved from the URL.
  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='<iframe src="//www.facebook.com/video/embed?video_id=!id" width="!width" height="!height"></iframe> ';
  84. // Use format_string to replace our placeholders with the settings values.
  85. $embed = format_string($embed, array(
  86. '!id' => $id,
  87. '!width' => $settings['width'],
  88. '!height' => $settings['height'],
  89. ));
  90. $video = array(
  91. '#markup' => $embed,
  92. );
  93. return $video;
  94. }
  95. return FALSE;
  96. }
  97. /**
  98. * Gets the thumbnail url for Facebook videos.
  99. *
  100. * @param string $url
  101. * The video URL.
  102. *
  103. * @return array
  104. * The video thumbnail information.
  105. */
  106. function video_embed_facebook_handle_thumbnail($url) {
  107. $id = _video_embed_facebook_get_video_id($url);
  108. return array(
  109. 'id' => $id,
  110. 'url' => 'https://graph.facebook.com/' . $id . '/picture',
  111. );
  112. }
  113. /**
  114. * Helper function to get the Facebook video's id.
  115. *
  116. * @param string $url
  117. * The video URL.
  118. *
  119. * @return string|bool
  120. * The video ID, or FALSE in case the ID can't be retrieved from the URL.
  121. */
  122. function _video_embed_facebook_get_video_id($url) {
  123. // Parse_url is an easy way to break a url into its components.
  124. $matches = array();
  125. preg_match('/(.*)?[v|video_id]=([^&#]*)/', $url, $matches);
  126. // If the v or video_id get parameters are set, return it.
  127. if ($matches && !empty($matches[2])) {
  128. return $matches[2];
  129. }
  130. // Otherwise return false.
  131. return FALSE;
  132. }