media_vimeo.formatters.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * Implements hook_file_formatter_info().
  7. */
  8. function media_vimeo_file_formatter_info() {
  9. $formatters['media_vimeo_video'] = array(
  10. 'label' => t('Vimeo Video'),
  11. 'file types' => array('video'),
  12. 'default settings' => array(
  13. 'width' => media_vimeo_variable_get('width'),
  14. 'height' => media_vimeo_variable_get('height'),
  15. 'autoplay' => media_vimeo_variable_get('autoplay'),
  16. ),
  17. 'view callback' => 'media_vimeo_file_formatter_video_view',
  18. 'settings callback' => 'media_vimeo_file_formatter_video_settings',
  19. );
  20. $formatters['media_vimeo_image'] = array(
  21. 'label' => t('Vimeo Preview Image'),
  22. 'file types' => array('video'),
  23. 'default settings' => array(
  24. 'image_style' => '',
  25. ),
  26. 'view callback' => 'media_vimeo_file_formatter_image_view',
  27. 'settings callback' => 'media_vimeo_file_formatter_image_settings',
  28. );
  29. return $formatters;
  30. }
  31. /**
  32. * Implements hook_file_formatter_FORMATTER_view().
  33. */
  34. function media_vimeo_file_formatter_video_view($file, $display, $langcode) {
  35. $scheme = file_uri_scheme($file->uri);
  36. // WYSIWYG does not yet support video inside a running editor instance.
  37. if ($scheme == 'vimeo' && empty($file->override['wysiwyg'])) {
  38. $element = array(
  39. '#theme' => 'media_vimeo_video',
  40. '#uri' => $file->uri,
  41. );
  42. foreach (array('width', 'height', 'autoplay') as $setting) {
  43. $element['#' . $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_vimeo_file_formatter_video_settings($form, &$form_state, $settings) {
  52. $element = array();
  53. $element['width'] = array(
  54. '#title' => t('Width'),
  55. '#type' => 'textfield',
  56. '#default_value' => $settings['width'],
  57. );
  58. $element['height'] = array(
  59. '#title' => t('Height'),
  60. '#type' => 'textfield',
  61. '#default_value' => $settings['height'],
  62. );
  63. $element['autoplay'] = array(
  64. '#title' => t('Autoplay'),
  65. '#type' => 'checkbox',
  66. '#default_value' => $settings['autoplay'],
  67. );
  68. return $element;
  69. }
  70. /**
  71. * Implements hook_file_formatter_FORMATTER_view().
  72. */
  73. function media_vimeo_file_formatter_image_view($file, $display, $langcode) {
  74. $scheme = file_uri_scheme($file->uri);
  75. if ($scheme == 'vimeo') {
  76. $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
  77. $image_style = $display['settings']['image_style'];
  78. $valid_image_styles = image_style_options(FALSE);
  79. if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
  80. $element = array(
  81. '#theme' => 'image',
  82. '#path' => $wrapper->getOriginalThumbnailPath(),
  83. );
  84. }
  85. else {
  86. $element = array(
  87. '#theme' => 'image_style',
  88. '#style_name' => $image_style,
  89. '#path' => $wrapper->getLocalThumbnailPath(),
  90. );
  91. }
  92. return $element;
  93. }
  94. }
  95. /**
  96. * Implements hook_file_formatter_FORMATTER_settings().
  97. */
  98. function media_vimeo_file_formatter_image_settings($form, &$form_state, $settings) {
  99. $element = array();
  100. $element['image_style'] = array(
  101. '#title' => t('Image style'),
  102. '#type' => 'select',
  103. '#options' => image_style_options(FALSE),
  104. '#default_value' => $settings['image_style'],
  105. '#empty_option' => t('None (original image)'),
  106. );
  107. return $element;
  108. }
  109. /**
  110. * Implements hook_file_default_displays().
  111. */
  112. function media_vimeo_file_default_displays() {
  113. $default_displays = array();
  114. // Default settings for displaying as a video.
  115. $default_video_settings = array(
  116. 'media_large' => array(
  117. 'width' => 480,
  118. 'height' => 360,
  119. 'autoplay' => FALSE,
  120. ),
  121. 'media_original' => array(
  122. 'width' => 640,
  123. 'height' => 480,
  124. 'autoplay' => media_vimeo_variable_get('autoplay'),
  125. ),
  126. );
  127. foreach ($default_video_settings as $view_mode => $settings) {
  128. $display_name = 'video__' . $view_mode . '__media_vimeo_video';
  129. $default_displays[$display_name] = (object) array(
  130. 'api_version' => 1,
  131. 'name' => $display_name,
  132. 'status' => 1,
  133. 'weight' => 1,
  134. 'settings' => $settings,
  135. );
  136. }
  137. // Default settings for displaying a video preview image. We enable preview
  138. // images even for view modes that also play video, for use inside a running
  139. // WYSIWYG editor. The higher weight ensures that the video display is used
  140. // where possible.
  141. $default_image_styles = array(
  142. 'media_preview' => 'square_thumbnail',
  143. 'media_large' => 'large',
  144. 'media_original' => ''
  145. );
  146. foreach ($default_image_styles as $view_mode => $image_style) {
  147. $display_name = 'video__' . $view_mode . '__media_vimeo_image';
  148. $default_displays[$display_name] = (object) array(
  149. 'api_version' => 1,
  150. 'name' => $display_name,
  151. 'status' => 1,
  152. 'weight' => 2,
  153. 'settings' => array('image_style' => $image_style),
  154. );
  155. }
  156. return $default_displays;
  157. }