media_youtube.theme.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @file media_youtube/includes/themes/media_youtube.theme.inc
  4. *
  5. * Theme and preprocess functions for Media: YouTube.
  6. */
  7. /**
  8. * Preprocess function for theme('media_youtube_video').
  9. */
  10. function media_youtube_preprocess_media_youtube_video(&$variables) {
  11. // Build the URL for display.
  12. $uri = $variables['uri'];
  13. $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
  14. $parts = $wrapper->get_parameters();
  15. $variables['video_id'] = check_plain($parts['v']);
  16. $variables['query'] = array();
  17. // @see http://code.google.com/apis/youtube/player_parameters.html
  18. foreach (array('width', 'height', 'autoplay', 'related', 'hd', 'showsearch', 'modestbranding', 'showinfo', 'version', 'theme', 'fullscreen', 'wmode', 'chromeless') as $option) {
  19. // Set the option, either from the options array, or from the default value.
  20. $variables[$option] = isset($variables[$option]) ? $variables[$option] : (isset($variables['options'][$option]) ? $variables['options'][$option] : media_youtube_variable_get($option));
  21. }
  22. // We have to set fullscreen in the url query and as a parameter to the flash.
  23. $variables['fs'] = $variables['fullscreen'];
  24. $variables['fullscreen'] = $variables['fullscreen'] ? 'true' : 'false';
  25. $variables['wrapper_id'] = 'media_youtube_' . $variables['video_id'] . '_' . $variables['id'];
  26. // Pass the settings to replace the object tag with an iframe.
  27. $settings = array(
  28. 'media_youtube' => array(
  29. $variables['wrapper_id'] => array(
  30. 'width' => $variables['width'],
  31. 'height' => $variables['height'],
  32. 'video_id' => $variables['video_id'],
  33. 'fullscreen' => $variables['fullscreen'],
  34. 'id' => $variables['wrapper_id'] .'_iframe',
  35. ),
  36. ),
  37. );
  38. // Set the version of the youtube api player.
  39. if ($variables['version']) {
  40. $variables['query']['version'] = $variables['version'];
  41. // Note that the fs variable defaults to 1 with the AS3 player.
  42. if (!$variables['fs']) {
  43. $variables['query']['fs'] = 0;
  44. }
  45. }
  46. else if ($variables['fs']) {
  47. // Note that the fs variable defaults to 0 with the AS2 player.
  48. $variables['query']['fs'] = 1;
  49. }
  50. // These options default to 0.
  51. foreach (array('modestbranding', 'autoplay', 'hd') as $variable) {
  52. if ($variables[$variable]) {
  53. $variables['query'][$variable] = 1;
  54. }
  55. }
  56. // These options default to 1.
  57. foreach (array('showsearch', 'showinfo') as $variable) {
  58. if (!$variables[$variable]) {
  59. $variables['query'][$variable] = 0;
  60. }
  61. }
  62. if (!$variables['related']) {
  63. $variables['query']['rel'] = 0;
  64. }
  65. if ($variables['theme'] != 'dark') {
  66. $variables['query']['theme'] = $variables['theme'];
  67. }
  68. // Ensure that we pass the required query variables to the Iframe settings.
  69. if (!empty($variables['query'])) {
  70. $settings['media_youtube'][$variables['wrapper_id']]['options'] = $variables['query'];
  71. }
  72. drupal_add_js($settings, 'setting');
  73. drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/media_youtube.js');
  74. drupal_add_css(drupal_get_path('module', 'media_youtube') . '/css/media_youtube.css');
  75. drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/flash_detect_min.js');
  76. // The chromeless player requires a different url.
  77. if ($variables['chromeless']) {
  78. $variables['url_api'] = 'apiplayer';
  79. $variables['query']['video_id'] = $variables['video_id'];
  80. }
  81. else {
  82. $variables['url_api'] = 'v/' . $variables['video_id'];
  83. }
  84. $variables['url'] = url('http://www.youtube.com/' . $variables['url_api'], array('query' => $variables['query'], 'external' => TRUE, 'https' => TRUE));
  85. // For users with JavaScript, these object and embed tags will be replaced
  86. // by an iframe, so that we can support users without Flash.
  87. $variables['output'] = <<<OUTPUT
  88. <object width="{$variables['width']}" height="{$variables['height']}">
  89. <param name="movie" value="{$variables['url']}"></param>
  90. <param name="allowFullScreen" value="{$variables['fullscreen']}"></param>
  91. <param name="wmode" value="{$variables['wmode']}" />
  92. <embed src="{$variables['url']}" type="application/x-shockwave-flash" width="{$variables['width']}" height="{$variables['height']}" allowfullscreen="{$variables['fullscreen']}" wmode="{$variables['wmode']}"></embed>
  93. </object>
  94. OUTPUT;
  95. }
  96. function theme_media_youtube_field_formatter_styles($variables) {
  97. $element = $variables['element'];
  98. $style = $variables['style'];
  99. $variables['file'] = $element['#item'];
  100. $variables['uri'] = $variables['file']['uri'];
  101. $variables['style_name'] = $style['name'];
  102. return theme('media_youtube_embed', $variables);
  103. }
  104. /**
  105. * Preview for Styles UI.
  106. */
  107. function theme_media_youtube_preview_style($variables) {
  108. $variables['uri'] = media_youtube_variable_get('preview_uri');
  109. $variables['field_type'] = 'file';
  110. $variables['object'] = file_uri_to_object($variables['uri']);
  111. return theme('styles', $variables);
  112. }
  113. /**
  114. * NOTE: Deprecated with Styles version 2.
  115. */
  116. function theme_media_youtube_styles($variables) {
  117. $style = $variables['style'];
  118. $variables['file'] = $variables['object'];
  119. $variables['uri'] = $variables['object']->uri;
  120. $variables['style_name'] = $style['name'];
  121. return theme('media_youtube_embed', $variables);
  122. }
  123. /**
  124. * @todo: get this working
  125. *
  126. * This code is for embedding videos in WYSIYWG areas, not currently working.
  127. * NOTE: Deprecated with Styles version 2.
  128. */
  129. function theme_media_youtube_embed($variables) {
  130. $preset_name = $variables['preset_name'];
  131. $preset = styles_containers_available_styles('file', 'media_youtube', $preset_name);
  132. $output = '';
  133. if (!empty($preset)) {
  134. // Build the URL for display.
  135. $uri = $variables['uri'];
  136. $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
  137. $parts = $wrapper->get_parameters();
  138. $fullscreen_value = $autoplay = 'false';
  139. $in_browser = $thumbnail = FALSE;
  140. foreach ($preset['effects'] as $effect) {
  141. switch ($effect['name']) {
  142. case 'autoplay':
  143. $autoplay = $effect['data']['autoplay'] ? 'true' : 'false';
  144. break;
  145. case 'resize':
  146. $width = $effect['data']['width'];
  147. $height = $effect['data']['height'];
  148. break;
  149. case 'fullscreen':
  150. $fullscreen_value = $effect['data']['fullscreen'] ? 'true' : 'false';
  151. break;
  152. case 'thumbnail':
  153. $thumbnail = $effect['data']['thumbnail'];
  154. }
  155. }
  156. if (isset($variables['object']->override)) {
  157. $override = $variables['object']->override;
  158. if (isset($override['width'])) {
  159. $width = $override['width'];
  160. }
  161. if (isset($override['height'])) {
  162. $height = $override['height'];
  163. }
  164. if (isset($override['wysiwyg'])) {
  165. $thumbnail = TRUE;
  166. }
  167. if (isset($override['browser']) && $override['browser']) {
  168. $in_browser = TRUE;
  169. $thumbnail = TRUE;
  170. }
  171. }
  172. $width = isset($width) ? $width : media_youtube_variable_get('width');
  173. $height = isset($height) ? $height : media_youtube_variable_get('height');
  174. $video_id = check_plain($parts['v']);
  175. if ($thumbnail) {
  176. // @todo Clean this up.
  177. $image_variables = array(
  178. 'path' => $wrapper->getOriginalThumbnailPath(),
  179. 'alt' => $variables['alt'],
  180. 'title' => $variables['title'],
  181. 'getsize' => FALSE,
  182. );
  183. if (isset($preset['image_style'])) {
  184. $image_variables['path'] = $wrapper->getLocalThumbnailPath();
  185. $image_variables['style_name'] = $preset['image_style'];
  186. $output = theme('image_style', $image_variables);
  187. }
  188. else {
  189. // We need to add this style attribute here so that it doesn't get lost
  190. // If you resize a video in a node, save it, edit it, but don't adjust
  191. // the sizing of the video while editing, the size will revert to the
  192. // default. Adding the specific size here retains the original resizing
  193. $WYSIWYG = isset($variables['object']->override['style']) ? $variables['object']->override['style'] : '';
  194. $image_variables['attributes'] = array('width' => $width, 'height' => $height, 'style' => $WYSIWYG);
  195. $output = theme('image', $image_variables);
  196. }
  197. if ($in_browser) {
  198. // Add an overlay that says 'YouTube' to media library browser thumbnails.
  199. $output .= '<span />';
  200. }
  201. }
  202. else {
  203. $output = theme('media_youtube_video', array('uri' => $uri, 'width' => $width, 'height' => $height, 'autoplay' => ($autoplay == 'true' ? TRUE : NULL), 'fullscreen' => ($fullscreen_value == 'true' ? TRUE : NULL)));
  204. }
  205. }
  206. return $output;
  207. }