video_embed_brightcove.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Add a handler for brightcove videos to Video Embed Field.
  5. * @see video_embed_field.api.php for more documentation.
  6. */
  7. /**
  8. * Implements hook_video_embed_handler_info().
  9. */
  10. function video_embed_brightcove_video_embed_handler_info() {
  11. $handlers = array();
  12. $handlers['brightcove'] = array(
  13. 'title' => 'Brightcove Video',
  14. 'function' => 'video_embed_brightcove_handle_video',
  15. 'thumbnail_default' => drupal_get_path('module', 'video_embed_brightcove') . '/img/brightcove.jpg',
  16. 'form' => 'video_embed_brightcove_form',
  17. 'form_validate' => 'video_embed_field_handler_brightcove_form_validate',
  18. 'domains' => array(
  19. 'brightcove.com',
  20. 'link.brightcove.com',
  21. ),
  22. 'defaults' => array(
  23. 'width' => 640,
  24. 'height' => 360,
  25. 'class' => '',
  26. ),
  27. );
  28. return $handlers;
  29. }
  30. /**
  31. * Form to configure out video settings.
  32. *
  33. * @param array $defaults
  34. * Values for your provider.
  35. *
  36. * @return array
  37. * A form as defined by form API.
  38. */
  39. function video_embed_brightcove_form($defaults) {
  40. $form = array();
  41. // Element for the width of the player.
  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. // Element for the height of the player.
  49. $form['height'] = array(
  50. '#type' => 'textfield',
  51. '#title' => t('Player Height'),
  52. '#description' => t('The height of the player.'),
  53. '#default_value' => $defaults['height'],
  54. );
  55. $form['class'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('Player CSS class'),
  58. '#description' => t('CSS class to add to the player'),
  59. '#default_value' => $defaults['class'],
  60. );
  61. return $form;
  62. }
  63. /**
  64. * Validates the form elements for the Brightcove configuration form.
  65. */
  66. function video_embed_field_handler_brightcove_form_validate($element, &$form_state, $form) {
  67. video_embed_field_validate_dimensions($element);
  68. }
  69. /**
  70. * The video handler.
  71. *
  72. * @param string $url
  73. * The full video url.
  74. * @param array $settings
  75. * Handlers settings from the settings form.
  76. *
  77. * @return array|string
  78. * The embed code for the video.
  79. */
  80. function video_embed_brightcove_handle_video($url, $settings) {
  81. $parameters = _video_embed_brightcove_get_video_properties($url);
  82. if (isset($parameters['id']) && isset($parameters['key'])) {
  83. // Embed code.
  84. $embed = '<object class="@class" id="myExperience" class="BrightcoveExperience">
  85. <param name="bgcolor" value="#FFFFFF" />
  86. <param name="width" value="@width" />
  87. <param name="height" value="@height" />
  88. <param name="playerID" value="!id" />
  89. <param name="playerKey" value="!key" />
  90. <param name="isVid" value="true" />
  91. <param name="isUI" value="true" />
  92. <param name="dynamicStreaming" value="true" />
  93. <param name="@videoPlayer" value="!videoplayer" />
  94. </object>';
  95. // Replace our placeholders with the values from the settings.
  96. $embed = format_string($embed, array(
  97. '!id' => $parameters['id'],
  98. '!key' => $parameters['key'],
  99. '@width' => $settings['width'],
  100. '@height' => $settings['height'],
  101. '@class' => $settings['class'],
  102. '!videoplayer' => $parameters['player'],
  103. ));
  104. $video = array(
  105. '#markup' => $embed,
  106. '#suffix' => '<script type="text/javascript">brightcove.createExperiences();</script>',
  107. '#attached' => array(
  108. 'js' => array(
  109. '//admin.brightcove.com/js/BrightcoveExperiences.js' => array(
  110. 'type' => 'external',
  111. ),
  112. ),
  113. ),
  114. );
  115. return $video;
  116. }
  117. return '';
  118. }
  119. /**
  120. * Helper function to take a brightcove video url and return its id.
  121. *
  122. * @param string $url
  123. * The full brightcove video url.
  124. *
  125. * @return array
  126. * The video properties.
  127. */
  128. function _video_embed_brightcove_get_video_properties($url) {
  129. // Easy way to break a url into its components.
  130. $components = array(
  131. 'id' => array(
  132. 'start' => 'bcpid',
  133. 'finish' => '\?bckey',
  134. ),
  135. 'key' => array(
  136. 'start' => 'bckey=',
  137. 'finish' => '&bctid',
  138. ),
  139. 'player' => array(
  140. 'start' => 'bctid=',
  141. 'finish' => '',
  142. ),
  143. );
  144. $matches = array();
  145. $return = array();
  146. foreach ($components as $key => $component) {
  147. $string = "/(.*){$component['start']}(.*){$component['finish']}/";
  148. preg_match($string, $url, $matches);
  149. if ($matches && !empty($matches[2])) {
  150. $return[$key] = check_plain($matches[2]);
  151. }
  152. }
  153. return $return;
  154. }