video_embed_field_overlay.module 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Implements hook_theme().
  4. */
  5. function video_embed_field_overlay_theme() {
  6. return array(
  7. 'video_embed_field_overlay' => array(
  8. 'variables' => array(
  9. 'show_thumbnail' => TRUE,
  10. 'thumbnail_path' => NULL,
  11. 'description' => NULL,
  12. 'image_style' => NULL,
  13. 'video_url' => NULL,
  14. 'video_style' => 'normal',
  15. ),
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_field_formatter_info().
  21. */
  22. function video_embed_field_overlay_field_formatter_info() {
  23. return array(
  24. 'video_embed_field_overlay' => array(
  25. 'label' => t('Overlay'),
  26. 'field types' => array('video_embed_field'),
  27. 'settings' => array(
  28. 'show_thumbnail' => TRUE,
  29. 'video_style' => 'normal',
  30. 'image_style' => image_style_load('video_thumbnail') ? 'video_thumbnail' : 'none',
  31. 'description' => 1,
  32. 'overlay' => 'dom-window'),
  33. ),
  34. );
  35. }
  36. /**
  37. * Implements hook_field_formatter_settings_form().
  38. */
  39. function video_embed_field_overlay_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  40. $display = $instance['display'][$view_mode];
  41. $settings = $display['settings'];
  42. $element = array();
  43. if ($display['type'] == 'video_embed_field_overlay') {
  44. // Video style
  45. $video_styles = video_embed_field_video_style_options(FALSE);
  46. $element['video_style'] = array(
  47. '#title' => t('Video style'),
  48. '#type' => 'select',
  49. '#default_value' => $settings['video_style'],
  50. '#options' => $video_styles,
  51. );
  52. // Show an image?
  53. $element['show_thumbnail'] = array(
  54. '#title' => t('Show thumbnail'),
  55. '#type' => 'select',
  56. '#options' => array(1 => t('Yes'), 0 => t('No')),
  57. '#default_value' => $settings['show_thumbnail'],
  58. );
  59. // Image style
  60. $element['image_style'] = array(
  61. '#title' => t('Image style'),
  62. '#type' => 'select',
  63. '#options' => image_style_options(FALSE),
  64. '#default_value' => $settings['image_style'],
  65. '#empty_option' => t('None (original image)'),
  66. );
  67. // Overlay library
  68. $element['overlay'] = array(
  69. '#title' => t('Overlay Library:'),
  70. '#type' => 'select',
  71. '#options' => array('dom-window' => t('DOM Window')),
  72. '#default_value' => $settings['overlay'],
  73. );
  74. }
  75. return $element;
  76. }
  77. /**
  78. * Implements hook_field_formatter_settings_summary().
  79. */
  80. function video_embed_field_overlay_field_formatter_settings_summary($field, $instance, $view_mode) {
  81. $display = $instance['display'][$view_mode];
  82. $settings = $display['settings'];
  83. $summary = array();
  84. if ($display['type'] == 'video_embed_field_overlay') {
  85. // Video style
  86. $video_styles = video_embed_field_video_style_options(FALSE);
  87. // Styles could be lost because of enabled/disabled modules that defines
  88. // their styles in code.
  89. if (isset($video_styles[$settings['video_style']])) {
  90. $summary[] = t('Video style: @style', array('@style' => $video_styles[$settings['video_style']]));
  91. }
  92. // Display thumbnail?
  93. if (isset($settings['show_thumbnail']) && $settings['show_thumbnail'] == TRUE) {
  94. $summary[] = t('Link: Showing thumbnail in link.');
  95. }
  96. else {
  97. $summary[] = t('Link: Showing plain-text link.');
  98. }
  99. // Image style
  100. $image_styles = image_style_options(FALSE);
  101. if (isset($image_styles[$settings['image_style']])) {
  102. $summary[] = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
  103. } //No Image style (original image)
  104. else {
  105. $summary[] = t('Original Image.');
  106. }
  107. // Overlay
  108. // @TODO: extend this to use a list of available overlay libraries.
  109. if (isset($settings['overlay'])) {
  110. $summary[] = t('Overlay Library: @overlay.', array('@overlay' => 'DOM Window'));
  111. }
  112. else {
  113. $summary[] = t('Overlay Library: @overlay.', array('@overlay' => 'DOM Window'));
  114. }
  115. }
  116. return implode('<br />', $summary);
  117. }
  118. /**
  119. * Implements hook_field_formatter_view().
  120. */
  121. function video_embed_field_overlay_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  122. $element = array();
  123. foreach ($items as $delta => $item) {
  124. if ($display['type'] == 'video_embed_field_overlay') {
  125. if (!isset($item['description']) || empty($item['description'])) {
  126. $item['description'] = t('Play video');
  127. }
  128. $element[$delta] = array(
  129. '#theme' => 'video_embed_field_overlay',
  130. '#thumbnail_path' => $item['thumbnail_path'],
  131. '#image_style' => $display['settings']['image_style'],
  132. '#video_url' => $item['video_url'],
  133. '#description' => $item['description'],
  134. '#show_thumbnail' => $display['settings']['show_thumbnail'],
  135. '#video_style' => $display['settings']['video_style'],
  136. '#attached' => array(
  137. 'css' => array(
  138. drupal_get_path('module', 'video_embed_field_overlay') . '/css/video_embed_field_overlay.css',
  139. ),
  140. ),
  141. );
  142. if (video_embed_field_overlay_library_exists()) {
  143. $element[$delta]['#attached']['library'] = array(array('domwindow', 'domwindow'));
  144. $element[$delta]['#attached']['js'] = array(drupal_get_path('module', 'video_embed_field_overlay') . '/js/video_embed_field_overlay.js');
  145. drupal_add_js(array(
  146. 'video_embed_field_overlay' => array(
  147. 'path' => drupal_get_path('module', 'video_embed_field_overlay'),
  148. ),
  149. ), 'setting');
  150. }
  151. else {
  152. drupal_set_message(t('DOM Window library was not found.'), 'warning');
  153. }
  154. }
  155. }
  156. return $element;
  157. }
  158. /**
  159. * Theme function wraps a video_embed_field element in an overlay
  160. */
  161. function theme_video_embed_field_overlay(&$variables) {
  162. $output = '';
  163. if (isset($variables['show_thumbnail']) && $variables['show_thumbnail'] == TRUE) {
  164. if (isset($variables['image_style']) && $variables['image_style']) {
  165. $thumbnail = theme('image_style', array(
  166. 'path' => $variables['thumbnail_path'],
  167. 'style_name' => $variables['image_style']));
  168. }
  169. else {
  170. $thumbnail = theme('image', array(
  171. 'path' => $variables['thumbnail_path'],
  172. 'alt' => $variables['description'],
  173. ));
  174. }
  175. }
  176. if (!isset($thumbnail) || empty($thumbnail)) {
  177. $link = l($variables['description'], $variables['video_url'], array(
  178. 'attributes' => array(
  179. 'class' => array('overlay'),
  180. 'title' => $variables['description'],
  181. ),
  182. 'html' => TRUE,
  183. ));
  184. }
  185. else {
  186. $link = l($thumbnail, $variables['video_url'], array(
  187. 'attributes' => array(
  188. 'class' => array('overlay'),
  189. ),
  190. 'html' => TRUE,
  191. ));
  192. }
  193. if (!isset($variables['video_style']) || empty($variables['video_style'])) {
  194. $variables['video_style'] = 'normal';
  195. }
  196. $embed_code = theme('video_embed_field_embed_code', array(
  197. 'url' => $variables['video_url'],
  198. 'style' => $variables['video_style'],
  199. ));
  200. $output .= '<div class="video-overlay">';
  201. $output .= ' <div class="video-overlay-thumbnail">';
  202. $output .= ' ' . $link;
  203. $output .= ' </div>';
  204. $output .= ' <div class="video-overlay-source">';
  205. $output .= ' ' . $embed_code;
  206. $output .= ' </div>';
  207. $output .= '</div>';
  208. return $output;
  209. }
  210. /**
  211. * Implements hook_image_default_styles().
  212. */
  213. function video_embed_field_overlay_image_default_styles() {
  214. $styles = array();
  215. // Exported image style: video_thumbnail
  216. $styles['video_thumbnail'] = array(
  217. 'name' => 'video_thumbnail',
  218. 'effects' => array(
  219. 1 => array(
  220. 'label' => 'Scale and crop',
  221. 'help' => 'Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.',
  222. 'effect callback' => 'image_scale_and_crop_effect',
  223. 'dimensions callback' => 'image_resize_dimensions',
  224. 'form callback' => 'image_resize_form',
  225. 'summary theme' => 'image_resize_summary',
  226. 'module' => 'image',
  227. 'name' => 'image_scale_and_crop',
  228. 'data' => array(
  229. 'width' => '510',
  230. 'height' => '290',
  231. ),
  232. 'weight' => '1',
  233. ),
  234. ),
  235. );
  236. // If the user has enabled the imagecache_canvasactions module, use it.
  237. // @see http://drupal.org/project/imagecache_actions
  238. if (module_exists('imagecache_canvasactions')) {
  239. $styles['video_thumbnail']['effects'][] = array(
  240. 'label' => 'Overlay (watermark)',
  241. 'help' => 'Choose the file image you wish to use as an overlay, and position it in a layer on top of the canvas.',
  242. 'effect callback' => 'canvasactions_file2canvas_image',
  243. 'form callback' => 'canvasactions_file2canvas_form',
  244. 'summary theme' => 'canvasactions_file2canvas_summary',
  245. 'module' => 'imagecache_canvasactions',
  246. 'name' => 'canvasactions_file2canvas',
  247. 'data' => array(
  248. 'xpos' => 'center',
  249. 'ypos' => 'center',
  250. 'alpha' => '100',
  251. // 'path' => drupal_get_path('module', 'video_embed_field_overlay') . '/images/video_overlay.png',
  252. 'path' => 'module://video_embed_field_overlay/images/video_overlay.png',
  253. ),
  254. 'weight' => '2',
  255. );
  256. }
  257. return $styles;
  258. }
  259. /**
  260. * Implements hook_modules_enabled().
  261. */
  262. function video_embed_field_overlay_modules_enabled($modules) {
  263. if (in_array('imagecache_canvasactions', $modules)) {
  264. drupal_static_reset('image_effect_definitions');
  265. $style = image_style_load('video_thumbnail');
  266. if ($style) {
  267. image_default_style_revert($style);
  268. drupal_set_message(t('Video Thumbnail image style has been reverted to its original state and the overlay action has been added.'));
  269. image_flush_caches();
  270. }
  271. }
  272. }
  273. /**
  274. * Helper function that checks if the domwindow library is present.
  275. */
  276. function video_embed_field_overlay_library_exists() {
  277. $libraries = array();
  278. if (empty($libraries)) {
  279. $libraries = libraries_get_libraries();
  280. }
  281. return array_key_exists('jquery.domwindow', $libraries);
  282. }