video_embed_facebook.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. 'class' => '',
  27. ),
  28. );
  29. return $handlers;
  30. }
  31. /**
  32. * Defines the form elements for the Facebook videos configuration form.
  33. *
  34. * @param array $defaults
  35. * The form default values.
  36. *
  37. * @return array
  38. * The provider settings form array.
  39. */
  40. function video_embed_facebook_form($defaults) {
  41. $form = array();
  42. $form['width'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('Player Width'),
  45. '#description' => t('The width of the player.'),
  46. '#default_value' => $defaults['width'],
  47. );
  48. $form['height'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('Player Height'),
  51. '#description' => t('The height of the player.'),
  52. '#default_value' => $defaults['height'],
  53. );
  54. $form['class'] = array(
  55. '#type' => 'textfield',
  56. '#title' => t('Player CSS class'),
  57. '#description' => t('CSS class to add to the player'),
  58. '#default_value' => $defaults['class'],
  59. );
  60. return $form;
  61. }
  62. /**
  63. * Validates the form elements for the Facebook video configuration form.
  64. *
  65. * @param array $element
  66. * The form element to validate.
  67. * @param array $form_state
  68. * The form to validate state.
  69. * @param array $form
  70. * The form to validate structure.
  71. */
  72. function video_embed_field_handler_facebook_form_validate($element, &$form_state, $form) {
  73. video_embed_field_validate_dimensions($element);
  74. }
  75. /**
  76. * Handler for Facebook videos.
  77. *
  78. * @param string $url
  79. * The video URL.
  80. * @param array $settings
  81. * The settings array.
  82. *
  83. * @return string|bool
  84. * The video iframe, or FALSE in case the ID can't be retrieved from the URL.
  85. */
  86. function video_embed_facebook_handle_video($url, $settings) {
  87. $id = _video_embed_facebook_get_video_id($url);
  88. if ($id) {
  89. // Our embed code.
  90. $embed='<iframe class="@class" src="//www.facebook.com/video/embed?video_id=!id" width="@width" height="@height"></iframe> ';
  91. // Use format_string to replace our placeholders with the settings values.
  92. $embed = format_string($embed, array(
  93. '!id' => $id,
  94. '@width' => $settings['width'],
  95. '@height' => $settings['height'],
  96. '@class' => $settings['class'],
  97. ));
  98. $video = array(
  99. '#markup' => $embed,
  100. );
  101. return $video;
  102. }
  103. return FALSE;
  104. }
  105. /**
  106. * Gets the thumbnail url for Facebook videos.
  107. *
  108. * @param string $url
  109. * The video URL.
  110. *
  111. * @return array
  112. * The video thumbnail information.
  113. */
  114. function video_embed_facebook_handle_thumbnail($url) {
  115. $id = _video_embed_facebook_get_video_id($url);
  116. return array(
  117. 'id' => $id,
  118. 'url' => 'https://graph.facebook.com/' . $id . '/picture',
  119. );
  120. }
  121. /**
  122. * Helper function to get the Facebook video's id.
  123. *
  124. * @param string $url
  125. * The video URL.
  126. *
  127. * @return string|bool
  128. * The video ID, or FALSE in case the ID can't be retrieved from the URL.
  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=|video_id=|videos\/|videos\/v.\.\d+\/)(\d+).*/', $url, $matches);
  134. // If the v or video_id get parameters are set, return it.
  135. if ($matches && !empty($matches[1])) {
  136. return check_plain($matches[1]);
  137. }
  138. // Otherwise return false.
  139. return FALSE;
  140. }