debut_media.module 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. include_once('debut_media.features.inc');
  3. /**
  4. * Implements hook_init().
  5. *
  6. * Add javascript and css required for the media browser link.
  7. */
  8. function debut_media_init() {
  9. if (arg(0) == 'file' && is_null(arg(1))) {
  10. $path = drupal_get_path('module', 'media');
  11. $element['#attached'] = array(
  12. 'js' => array($path . '/js/media.admin.js'),
  13. 'css' => array($path . '/css/media.css'),
  14. );
  15. module_load_include('inc', 'media', 'includes/media.browser');
  16. media_attach_browser_js($element);
  17. // Render an empty element to add the js and css.
  18. drupal_render($element);
  19. }
  20. }
  21. /**
  22. * Implements hook_menu_alter().
  23. *
  24. * Add a local action link to the media page.
  25. */
  26. function debut_media_menu_alter(&$items) {
  27. if (isset($items['media/browser'])) {
  28. $items['media/add'] = $items['media/browser'];
  29. $items['media/add']['type'] = MENU_LOCAL_ACTION;
  30. }
  31. }
  32. /**
  33. * Implements hook_entity_info_alter().
  34. *
  35. * Enable custom settings for the 'full' file view mode.
  36. * See http://drupal.org/node/1291428.
  37. */
  38. function debut_media_entity_info_alter(&$entity_info) {
  39. $entity_info['file']['view modes']['full']['custom settings'] = TRUE;
  40. }
  41. /**
  42. * Implements hook_module_implements_alter().
  43. *
  44. * Extends the core file entity to be fieldable. Modules can define file types
  45. * via hook_file_type_info(). For each defined type, create a bundle, so that
  46. * fields can be configured per file type.
  47. */
  48. function debut_media_module_implements_alter(&$implementations, $hook) {
  49. if ($hook == 'entity_info_alter') {
  50. // Move debut_media_entity_info_alter() to the end of the list.
  51. // module_implements() iterates through $implementations with a foreach
  52. // loop which PHP iterates in the order that the items were added, so to
  53. // move an item to the end of the array, we remove it and then add it.
  54. $group = $implementations['debut_media'];
  55. unset($implementations['debut_media']);
  56. $implementations['debut_media'] = $group;
  57. }
  58. }
  59. /**
  60. * Implements hook_views_data_alter().
  61. *
  62. * Use the media_title field for the file fid argument's name field.
  63. *
  64. * @todo: determine why this doesn't appear to work with views contextual
  65. * filters;
  66. */
  67. function debut_media_views_data_alter(&$data) {
  68. $data['file_managed']['fid']['argument']['name field'] = 'media_title';
  69. $data['file_managed']['fid']['argument']['name table'] = 'field_data_media_title';
  70. }
  71. /**
  72. * Implements hook_page_alter().
  73. *
  74. * Set the page title for media items to the title of the item.
  75. */
  76. function debut_media_page_alter(&$variables) {
  77. if (arg(0) == 'file' && $file = menu_get_object('file')) {
  78. if (!empty($file->media_title['und'][0]['safe_value'])) {
  79. drupal_set_title($file->media_title['und'][0]['safe_value']);
  80. }
  81. }
  82. // Add late in the page generation process to override the jcarousel css.
  83. drupal_add_css(drupal_get_path('module', 'debut_media') . '/debut_media.css');
  84. }
  85. /**
  86. * Implementation of Styles module hook_styles_default_styles().
  87. */
  88. function debut_media_styles_default_styles() {
  89. return array(
  90. 'file' => array(
  91. 'styles' => array(
  92. 'medium_large' => array(
  93. 'label' => 'Medium large',
  94. 'description' => 'A medium large format of the media.',
  95. ),
  96. 'small_square_thumbnail' => array(
  97. 'label' => 'Small square thumbnail',
  98. 'description' => 'A small, square format of the media.',
  99. ),
  100. ),
  101. ),
  102. );
  103. }
  104. /**
  105. * Implementation of Styles module hook_styles_default_presets_alter().
  106. */
  107. function debut_media_styles_default_presets_alter(&$presets) {
  108. foreach (array_keys(debut_media_image_default_styles()) as $image_style) {
  109. // Cover the containers defined by file_styles and media_youtube.
  110. foreach (array('application', 'image', 'audio', 'video', 'default', 'media_youtube', 'media_vimeo') as $type) {
  111. // Ensure another module hasn't removed the container.
  112. if (isset($presets['file']['containers'][$type])) {
  113. $presets['file']['containers'][$type]['styles'][$image_style] = array(
  114. 'default preset' => 'medium_large',
  115. );
  116. $presets['file']['containers'][$type]['presets'][$image_style] = array(
  117. array(
  118. 'name' => 'image_style',
  119. 'settings' => array(
  120. 'image_style' => $image_style,
  121. ),
  122. ),
  123. array(
  124. 'name' => 'thumbnail',
  125. 'settings' => array(),
  126. ),
  127. );
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Implementation of hook_field_default_fields_alter().
  134. *
  135. * Selectively add media fields to content types provided by features. To
  136. * receive a media_field instance, the feature should include the following in
  137. * its .info file:
  138. * debut[media][node_types][my_content_type] = my_content_type
  139. * where my_content_type is the name of a feature-provided content type.
  140. *
  141. * The media field status of a feature's content type can be altered by using
  142. * hook_system_info_alter(). Example:
  143. * %code
  144. * function example_system_info_alter(&$info, $module, $type) {
  145. * if ($type == $module && $module->name == 'debut_article') {
  146. * unset($info['debut']['media']['article']);
  147. * }
  148. * }
  149. * %endcode
  150. */
  151. function debut_media_field_default_fields_alter(&$items) {
  152. // Get features modules and determine which if claim media.
  153. $features = features_get_features();
  154. $node_types = array();
  155. foreach ($features as $feature) {
  156. $info = $feature->info;
  157. if (isset($info['debut']) && isset($info['debut']['media']) && isset($info['debut']['media']['node_types'])) {
  158. // Key the array by node type to match the format of the 'service_links_node_types' variable.
  159. $node_types = array_merge($node_types, array_combine($info['debut']['media']['node_types'], $info['debut']['media']['node_types']));
  160. }
  161. }
  162. // Add media fields to the designated node types.
  163. if (!empty($node_types)) {
  164. $field = debut_media_media_field();
  165. foreach ($node_types as $type) {
  166. // Don't override an existing field. This allows features to provide
  167. // their own version of the field.
  168. if (!isset($items["node-$type-field_media"])) {
  169. $field['field_instance']['bundle'] = $type;
  170. $items["node-$type-field_media"] = $field;
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Return the structure of the media field.
  177. *
  178. * The instance bundle is marked as 'placeholder'. It should be converted to
  179. * the appropriate node type before being used.
  180. */
  181. function debut_media_media_field() {
  182. $field = array(
  183. 'field_config' => array(
  184. 'active' => '1',
  185. 'cardinality' => '-1',
  186. 'deleted' => '0',
  187. 'entity_types' => array(),
  188. 'field_name' => 'field_media',
  189. 'foreign keys' => array(
  190. 'fid' => array(
  191. 'columns' => array(
  192. 'fid' => 'fid',
  193. ),
  194. 'table' => 'file_managed',
  195. ),
  196. ),
  197. 'indexes' => array(
  198. 'fid' => array(
  199. 0 => 'fid',
  200. ),
  201. ),
  202. 'module' => 'file',
  203. 'settings' => array(
  204. 'display_default' => 0,
  205. 'display_field' => 0,
  206. 'uri_scheme' => 'public',
  207. ),
  208. 'translatable' => '0',
  209. 'type' => 'file',
  210. ),
  211. 'field_instance' => array(
  212. 'bundle' => 'placeholder',
  213. 'deleted' => '0',
  214. 'description' => '',
  215. 'display' => array(
  216. 'default' => array(
  217. 'label' => 'hidden',
  218. 'module' => 'file_entity',
  219. 'settings' => array(
  220. 'file_view_mode' => 'media_large',
  221. ),
  222. 'type' => 'file_rendered',
  223. 'weight' => '4',
  224. ),
  225. 'teaser' => array(
  226. 'label' => 'above',
  227. 'settings' => array(),
  228. 'type' => 'hidden',
  229. 'weight' => 0,
  230. ),
  231. ),
  232. 'entity_type' => 'node',
  233. 'field_name' => 'field_media',
  234. 'label' => 'Media',
  235. 'required' => 0,
  236. 'settings' => array(
  237. 'description_field' => 0,
  238. 'file_directory' => '',
  239. 'file_extensions' => 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp',
  240. 'max_filesize' => '',
  241. 'user_register_form' => FALSE,
  242. ),
  243. 'widget' => array(
  244. 'active' => 1,
  245. 'module' => 'media',
  246. 'settings' => array(
  247. 'allowed_schemes' => array(
  248. 'public' => 'public',
  249. 'vimeo' => 'vimeo',
  250. 'youtube' => 'youtube',
  251. ),
  252. 'allowed_types' => array(
  253. 'audio' => 0,
  254. 'default' => 'default',
  255. 'image' => 'image',
  256. 'video' => 'video',
  257. ),
  258. 'browser_plugins' => array(
  259. 'library' => 0,
  260. 'media_default--media_browser_1' => 0,
  261. 'media_internet' => 0,
  262. 'upload' => 0,
  263. ),
  264. 'progress_indicator' => 'throbber',
  265. ),
  266. 'type' => 'media_generic',
  267. 'weight' => '10',
  268. ),
  269. ),
  270. );
  271. return $field;
  272. }
  273. /**
  274. * Implements hook_file_default_displays_alter().
  275. *
  276. * Clone settings for 'media_large' to new 'full' view mode. Add a generic file
  277. * fallback for each view mode + file type combo.
  278. *
  279. * @todo: remove 'media_large' to 'full' cloning once media provider modules
  280. * catch up. See http://drupal.org/node/1291428.
  281. */
  282. function debut_media_file_default_displays_alter(&$items) {
  283. $info = entity_get_info('file');
  284. $bundles = array_keys($info['bundles']);
  285. $view_modes = array_keys($info['view modes']);
  286. foreach ($items as $key => $data) {
  287. list($file_type, $view_mode, $formatter_name) = explode('__', $key);
  288. if ($view_mode == 'media_large') {
  289. $display_name = implode('__', array($file_type, 'full', $formatter_name));
  290. if (!isset($items[$display_name])) {
  291. $items[$display_name] = clone($data);
  292. $items[$display_name]->name = $display_name;
  293. }
  294. }
  295. }
  296. foreach ($bundles as $file_type) {
  297. foreach ($view_modes as $view_mode) {
  298. $display_name = implode('__', array($file_type, $view_mode, 'file_field_file_default'));
  299. if (!isset($items[$display_name])) {
  300. $items[$display_name] = (object) array(
  301. 'api_version' => 1,
  302. 'name' => $display_name,
  303. 'status' => 1,
  304. 'weight' => 35,
  305. 'settings' => array(),
  306. );
  307. }
  308. }
  309. }
  310. }