file_styles.theme.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * Theme functions for File styles.
  5. */
  6. /**
  7. * A wrapper for the image style preview.
  8. */
  9. function theme_file_styles_image_style_preview($variables) {
  10. $style_name = $variables['style_name'];
  11. $styles = image_styles();
  12. if (isset($styles[$style_name])) {
  13. // The theme_image_style_preview function is in image.admin,
  14. // but not properly referenced in hook_theme.
  15. // @TODO: File a bug report and fix this in core.
  16. module_load_include('inc', 'image', 'image.admin');
  17. drupal_add_css(drupal_get_path('module', 'image') . '/image.admin.css');
  18. // Ensure we revert the module & storage namespace for the Image module.
  19. if (isset($styles[$style_name]['image_module'])) {
  20. $styles[$style_name]['module'] = $styles[$style_name]['image_module'];
  21. $styles[$style_name]['storage'] = $styles[$style_name]['image_storage'];
  22. }
  23. return theme('image_style_preview', array('style' => $styles[$style_name]));
  24. }
  25. }
  26. function theme_file_styles_styles_default($variables) {
  27. $file = $variables['object'];
  28. $path = drupal_get_path('module', 'file');
  29. $thumbnail = theme('image',
  30. array(
  31. 'path' => $path . '/images/file-unknown.png',
  32. 'alt' => t('Thumbnail for !filename.', array('!filename' => $file->filename)),
  33. 'attributes' => array('width' => 100, 'height' => 75, 'class' => 'file-unknown'),
  34. )
  35. );
  36. return $thumbnail;
  37. }
  38. function theme_file_styles_preview_default($variables) {
  39. return 'default: theme_file_styles_preview_default';
  40. }
  41. function theme_file_styles_field_formatter_styles_default($variables) {
  42. $file = $variables['object'];
  43. $file_url = file_create_url($file->uri);
  44. return l($file->filename, $file_url);
  45. }
  46. function theme_file_styles_field_formatter_styles($variables) {
  47. // The formatter name needs to match what theme_field_formatter_image expects.
  48. $variables['element']['#formatter'] = '__' . $variables['style']['name'];
  49. // Add Alt & Title to the element.
  50. $variables['element']['#item']['alt'] = $variables['element']['#item']['title'] = $variables['element']['#item']['description'];
  51. // Defer to Image Styles for the rest.
  52. return theme('field_formatter_image', $variables);
  53. }
  54. function theme_file_styles_image($variables) {
  55. $file = $variables['object'];
  56. $style_name = $variables['style_name'];
  57. $vars = array();
  58. $vars['path'] = $file->uri;
  59. // Allow image attributes to be provided by the passed-in file object. 'alt'
  60. // and 'title' need to be separated out because they are treated specially by
  61. // theme_image().
  62. if (isset($file->override)) {
  63. $vars['attributes'] = $file->override;
  64. foreach (array('alt', 'title') as $attribute) {
  65. if (isset($vars['attributes'][$attribute])) {
  66. $vars[$attribute] = $vars['attributes'][$attribute];
  67. unset($vars['attributes'][$attribute]);
  68. }
  69. }
  70. }
  71. // Add default values for 'alt' and 'title'.
  72. if (!isset($vars['alt'])) {
  73. $vars['alt'] = isset($variables['description']) ? $variables['description'] : (isset($file->description) ? $file->description : '');
  74. }
  75. if (!isset($vars['title']) || $vars['title'] === '') {
  76. $vars['title'] = isset($variables['title']) ? $variables['title'] : $vars['alt'];
  77. }
  78. // Special case for 'original'.
  79. // @TODO Certainly can be more elegant than this.
  80. if ($style_name == 'original') {
  81. $vars['getsize'] = FALSE;
  82. return theme('image', $vars);
  83. }
  84. $vars['style_name'] = $style_name;
  85. //@TODO: How to add fields here?
  86. return theme('image_style', $vars);
  87. }
  88. /**
  89. * Support for HTML5 videos out of the box.
  90. */
  91. function theme_file_styles_video($variables) {
  92. $file = $variables['object'];
  93. $style_name = $variables['style_name'];
  94. $vars = array();
  95. $width = isset($variables['width']) ? $variables['width'] : NULL;
  96. $height = isset($variables['height']) ? $variables['height'] : NULL;
  97. $path = file_create_url($file->uri);
  98. $preset_name = $variables['preset_name'];
  99. $preset = styles_containers_available_styles('file', 'video', $preset_name);
  100. // By default, we don't display as a thumbnail.
  101. $thumbnail = FALSE;
  102. foreach ($preset['effects'] as $effect) {
  103. switch ($effect['name']) {
  104. case 'resize':
  105. $width = isset($effect['data']['width']) ? $effect['data']['width'] : $width;
  106. $height = isset($effect['data']['height']) ? $effect['data']['height'] : $height;
  107. break;
  108. case 'thumbnail':
  109. $thumbnail = TRUE;
  110. break;
  111. }
  112. }
  113. if (isset($variables['object']->override)) {
  114. $override = $variables['object']->override;
  115. $width = isset($override['width']) ? $override['width'] : $width;
  116. $height = isset($override['height']) ? $override['height'] : $height;
  117. if (isset($override['wysiwyg']) && $override['wysiwyg']) {
  118. $thumbnail = TRUE;
  119. }
  120. }
  121. $vars['attributes'] = array('width' => $width, 'height' => $height);
  122. if ($thumbnail) {
  123. return theme('image', array('path' => drupal_get_path('module', 'file_styles') . '/images/file-video.png', 'attributes' => $vars['attributes'], 'getsize' => FALSE));
  124. }
  125. $width = isset($vars['attributes']['width']) ? ('width="' . $vars['attributes']['width'] . '"') : '';
  126. $height = isset($vars['attributes']['height']) ? ('height="' . $vars['attributes']['height'] . '"') : '';
  127. switch($file->filemime) {
  128. case 'video/ogg':
  129. $source = "<source src='$path' type='video/ogg; codecs=\"theora, vorbis\"'>";
  130. break;
  131. case 'video/mp4':
  132. $source = "<source src='$path' type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'>";
  133. break;
  134. }
  135. $output = "<video $width $height controls>$source</video>";
  136. return $output;
  137. }