media_youtube.formatters.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * Implements hook_file_formatter_info().
  7. */
  8. function media_youtube_file_formatter_info() {
  9. $formatters['media_youtube_video'] = array(
  10. 'label' => t('YouTube Video'),
  11. 'file types' => array('video'),
  12. 'default settings' => array(),
  13. 'view callback' => 'media_youtube_file_formatter_video_view',
  14. 'settings callback' => 'media_youtube_file_formatter_video_settings',
  15. );
  16. foreach (array('width', 'height', 'autoplay', 'related', 'hd', 'showsearch', 'modestbranding', 'showinfo', 'version', 'theme', 'fullscreen', 'wmode', 'chromeless') as $setting) {
  17. $formatters['media_youtube_video']['default settings'][$setting] = media_youtube_variable_get($setting);
  18. }
  19. $formatters['media_youtube_image'] = array(
  20. 'label' => t('YouTube Preview Image'),
  21. 'file types' => array('video'),
  22. 'default settings' => array(
  23. 'image_style' => '',
  24. ),
  25. 'view callback' => 'media_youtube_file_formatter_image_view',
  26. 'settings callback' => 'media_youtube_file_formatter_image_settings',
  27. );
  28. return $formatters;
  29. }
  30. /**
  31. * Implements hook_file_formatter_FORMATTER_view().
  32. */
  33. function media_youtube_file_formatter_video_view($file, $display, $langcode) {
  34. $scheme = file_uri_scheme($file->uri);
  35. // WYSIWYG does not yet support video inside a running editor instance.
  36. if ($scheme == 'youtube' && empty($file->override['wysiwyg'])) {
  37. $element = array(
  38. '#theme' => 'media_youtube_video',
  39. '#uri' => $file->uri,
  40. '#options' => array(),
  41. );
  42. foreach (array('width', 'height', 'autoplay', 'related', 'hd', 'showsearch', 'modestbranding', 'showinfo', 'version', 'theme', 'fullscreen', 'wmode', 'chromeless') as $setting) {
  43. $element['#options'][$setting] = isset($file->override[$setting]) ? $file->override[$setting] : $display['settings'][$setting];
  44. }
  45. return $element;
  46. }
  47. }
  48. /**
  49. * Implements hook_file_formatter_FORMATTER_settings().
  50. */
  51. function media_youtube_file_formatter_video_settings($form, &$form_state, $settings) {
  52. $element = array();
  53. $options = array(
  54. 0 => t('AS2'),
  55. 3 => t('AS3'),
  56. );
  57. $element['version'] = array(
  58. '#title' => t('YouTube player version'),
  59. '#type' => 'select',
  60. '#options' => $options,
  61. '#default_value' => $settings['version'],
  62. );
  63. $options = array(
  64. 'dark' => t('dark'),
  65. 'light' => t('light'),
  66. );
  67. $element['theme'] = array(
  68. '#title' => t('YouTube player theme'),
  69. '#type' => 'select',
  70. '#options' => $options,
  71. '#default_value' => $settings['theme'],
  72. );
  73. $element['width'] = array(
  74. '#title' => t('Width'),
  75. '#type' => 'textfield',
  76. '#default_value' => $settings['width'],
  77. );
  78. $element['height'] = array(
  79. '#title' => t('Height'),
  80. '#type' => 'textfield',
  81. '#default_value' => $settings['height'],
  82. );
  83. $element['fullscreen'] = array(
  84. '#title' => t('Allow the video to be played in full screen mode'),
  85. '#type' => 'checkbox',
  86. '#default_value' => $settings['fullscreen'],
  87. );
  88. $element['autoplay'] = array(
  89. '#title' => t('Autoplay'),
  90. '#type' => 'checkbox',
  91. '#default_value' => $settings['autoplay'],
  92. );
  93. $element['related'] = array(
  94. '#title' => t('Show suggested videos when the video finishes'),
  95. '#type' => 'checkbox',
  96. '#default_value' => $settings['related'],
  97. );
  98. $element['hd'] = array(
  99. '#title' => t('Display the high quality version of the video when available'),
  100. '#type' => 'checkbox',
  101. '#default_value' => $settings['hd'],
  102. );
  103. $element['showsearch'] = array(
  104. '#title' => t('Allow users to search from the video'),
  105. '#type' => 'checkbox',
  106. '#default_value' => $settings['showsearch'],
  107. );
  108. $element['modestbranding'] = array(
  109. '#title' => t('Use modest YouTube branding (requires AS3 version player)'),
  110. '#type' => 'checkbox',
  111. '#default_value' => $settings['modestbranding'],
  112. );
  113. $element['showinfo'] = array(
  114. '#title' => t('Display video title'),
  115. '#type' => 'checkbox',
  116. '#default_value' => $settings['showinfo'],
  117. );
  118. $element['chromeless'] = array(
  119. '#title' => t('Use chromeless player'),
  120. '#type' => 'checkbox',
  121. '#default_value' => $settings['chromeless'],
  122. );
  123. return $element;
  124. }
  125. /**
  126. * Implements hook_file_formatter_FORMATTER_view().
  127. */
  128. function media_youtube_file_formatter_image_view($file, $display, $langcode) {
  129. $scheme = file_uri_scheme($file->uri);
  130. if ($scheme == 'youtube') {
  131. $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
  132. $image_style = $display['settings']['image_style'];
  133. $valid_image_styles = image_style_options(FALSE);
  134. if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
  135. $element = array(
  136. '#theme' => 'image',
  137. '#path' => $wrapper->getOriginalThumbnailPath(),
  138. );
  139. }
  140. else {
  141. $element = array(
  142. '#theme' => 'image_style',
  143. '#style_name' => $image_style,
  144. '#path' => $wrapper->getLocalThumbnailPath(),
  145. );
  146. }
  147. return $element;
  148. }
  149. }
  150. /**
  151. * Implements hook_file_formatter_FORMATTER_settings().
  152. */
  153. function media_youtube_file_formatter_image_settings($form, &$form_state, $settings) {
  154. $element = array();
  155. $element['image_style'] = array(
  156. '#title' => t('Image style'),
  157. '#type' => 'select',
  158. '#options' => image_style_options(FALSE),
  159. '#default_value' => $settings['image_style'],
  160. '#empty_option' => t('None (original image)'),
  161. );
  162. return $element;
  163. }
  164. /**
  165. * Implements hook_file_default_displays().
  166. */
  167. function media_youtube_file_default_displays() {
  168. $default_displays = array();
  169. // Default settings for displaying as a video.
  170. $default_video_settings = array(
  171. 'media_large' => array(
  172. 'width' => 480,
  173. 'height' => 360,
  174. 'autoplay' => FALSE,
  175. ),
  176. 'media_original' => array(
  177. 'width' => 640,
  178. 'height' => 480,
  179. 'autoplay' => media_youtube_variable_get('autoplay'),
  180. ),
  181. );
  182. foreach ($default_video_settings as $view_mode => $settings) {
  183. $display_name = 'video__' . $view_mode . '__media_youtube_video';
  184. $default_displays[$display_name] = (object) array(
  185. 'api_version' => 1,
  186. 'name' => $display_name,
  187. 'status' => 1,
  188. 'weight' => 1,
  189. 'settings' => $settings,
  190. );
  191. }
  192. // Default settings for displaying a video preview image. We enable preview
  193. // images even for view modes that also play video, for use inside a running
  194. // WYSIWYG editor. The higher weight ensures that the video display is used
  195. // where possible.
  196. $default_image_styles = array(
  197. 'media_preview' => 'square_thumbnail',
  198. 'media_large' => 'large',
  199. 'media_original' => ''
  200. );
  201. foreach ($default_image_styles as $view_mode => $image_style) {
  202. $display_name = 'video__' . $view_mode . '__media_youtube_image';
  203. $default_displays[$display_name] = (object) array(
  204. 'api_version' => 1,
  205. 'name' => $display_name,
  206. 'status' => 1,
  207. 'weight' => 2,
  208. 'settings' => array('image_style' => $image_style),
  209. );
  210. }
  211. return $default_displays;
  212. }