video_embed_brightcove.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. ),
  26. );
  27. return $handlers;
  28. }
  29. /**
  30. * Form to configure out video settings.
  31. *
  32. * @param array $defaults
  33. * Values for your provider.
  34. *
  35. * @return array
  36. * A form as defined by form API.
  37. */
  38. function video_embed_brightcove_form($defaults) {
  39. $form = array();
  40. // Element for the width of the player.
  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. // Element for the height of the player.
  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. return $form;
  55. }
  56. /**
  57. * Validates the form elements for the Brightcove configuration form.
  58. */
  59. function video_embed_field_handler_brightcove_form_validate($element, &$form_state, $form) {
  60. video_embed_field_validate_dimensions($element);
  61. }
  62. /**
  63. * The video handler.
  64. *
  65. * @param string $url
  66. * The full video url.
  67. * @param array $settings
  68. * Handlers settings from the settings form.
  69. *
  70. * @return array|string
  71. * The embed code for the video.
  72. */
  73. function video_embed_brightcove_handle_video($url, $settings) {
  74. $parameters = _video_embed_brightcove_get_video_properties($url);
  75. if (isset($parameters['id']) && isset($parameters['key'])) {
  76. // Embed code.
  77. $embed = '<object id="myExperience" class="BrightcoveExperience">
  78. <param name="bgcolor" value="#FFFFFF" />
  79. <param name="width" value="@width" />
  80. <param name="height" value="@height" />
  81. <param name="playerID" value="!id" />
  82. <param name="playerKey" value="!key" />
  83. <param name="isVid" value="true" />
  84. <param name="isUI" value="true" />
  85. <param name="dynamicStreaming" value="true" />
  86. <param name="@videoPlayer" value="!videoplayer" />
  87. </object>';
  88. // Replace our placeholders with the values from the settings.
  89. $embed = format_string($embed, array(
  90. '!id' => $parameters['id'],
  91. '!key' => $parameters['key'],
  92. '@width' => $settings['width'],
  93. '@height' => $settings['height'],
  94. '!videoplayer' => $parameters['player'],
  95. ));
  96. $video = array(
  97. '#markup' => $embed,
  98. '#suffix' => '<script type="text/javascript">brightcove.createExperiences();</script>',
  99. '#attached' => array(
  100. 'js' => array(
  101. '//admin.brightcove.com/js/BrightcoveExperiences.js' => array(
  102. 'type' => 'external',
  103. ),
  104. ),
  105. ),
  106. );
  107. return $video;
  108. }
  109. return '';
  110. }
  111. /**
  112. * Helper function to take a brightcove video url and return its id.
  113. *
  114. * @param string $url
  115. * The full brightcove video url.
  116. *
  117. * @return array
  118. * The video properties.
  119. */
  120. function _video_embed_brightcove_get_video_properties($url) {
  121. // Easy way to break a url into its components.
  122. $components = array(
  123. 'id' => array(
  124. 'start' => 'bcpid',
  125. 'finish' => '\?bckey',
  126. ),
  127. 'key' => array(
  128. 'start' => 'bckey=',
  129. 'finish' => '&bctid',
  130. ),
  131. 'player' => array(
  132. 'start' => 'bctid=',
  133. 'finish' => '',
  134. ),
  135. );
  136. $matches = array();
  137. $return = array();
  138. foreach ($components as $key => $component) {
  139. $string = "/(.*){$component['start']}(.*){$component['finish']}/";
  140. preg_match($string, $url, $matches);
  141. if ($matches && !empty($matches[2])) {
  142. $return[$key] = $matches[2];
  143. }
  144. }
  145. return $return;
  146. }