media.api.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Media module.
  5. */
  6. /**
  7. * Parses a url or embedded code into a unique URI.
  8. *
  9. * @param string $url
  10. * The original URL or embed code to parse.
  11. *
  12. * @return array
  13. * The unique URI for the file, based on its stream wrapper, or NULL.
  14. *
  15. * @see hook_media_parse_alter()
  16. * @see media_parse_to_file()
  17. * @see media_add_from_url_validate()
  18. */
  19. function hook_media_parse($url) {
  20. // Only parse URLs from our website of choice: examplevideo.com
  21. if (substr($url, 0, 27) == 'http://www.examplevideo.com') {
  22. // Each video has a 5 digit ID, i.e. http://www.examplevideo.com/12345
  23. // Grab the ID and use it in our URI.
  24. $id = substr($url, 28, 33);
  25. return file_stream_wrapper_uri_normalize('examplevideo://video/' . $id);
  26. }
  27. }
  28. /**
  29. * Alters the parsing of urls and embedded codes into unique URIs.
  30. *
  31. * @param string $success
  32. * The unique URI for the file, based on its stream wrapper, or NULL.
  33. * @param array $context
  34. * A nested array of contextual information containing the following keys:
  35. * - url: The original URL or embed code to parse.
  36. * - module: The name of the module which is attempting to parse the url or
  37. * embedded code into a unique URI.
  38. *
  39. * @see hook_media_parse()
  40. * @see hook_media_browser_plugin_info()
  41. * @see media_get_browser_plugin_info()
  42. */
  43. function hook_media_parse_alter(&$success, $context) {
  44. $url = $context['url'];
  45. $url_info = parse_url($url);
  46. // Restrict users to only embedding secure links.
  47. if ($url_info['scheme'] != 'https') {
  48. $success = NULL;
  49. }
  50. // Use a custom handler for detecting YouTube videos.
  51. if ($context['module' == 'media_youtube']) {
  52. $handler = new CustomYouTubeHandler($url);
  53. $success = $handler->parse($url);
  54. }
  55. }
  56. /**
  57. * Returns a list of plugins for the media browser.
  58. *
  59. * @return array
  60. * A nested array of plugin information, keyed by plugin name. Each plugin
  61. * info array may have the following keys:
  62. * - title: (required) A name for the tab in the media browser.
  63. * - class: (required) The class name of the handler. This class must
  64. * implement a view() method, and may (should) extend the
  65. * @link MediaBrowserPlugin MediaBrowserPlugin @endlink class.
  66. * - weight: (optional) Integer to determine the tab order. Defaults to 0.
  67. * - access callback: (optional) A callback for user access checks.
  68. * - access arguments: (optional) An array of arguments for the user access
  69. * check.
  70. *
  71. * Additional custom keys may be provided for use by the handler.
  72. *
  73. * @see hook_media_browser_plugin_info_alter()
  74. * @see media_get_browser_plugin_info()
  75. */
  76. function hook_media_browser_plugin_info() {
  77. $info['media_upload'] = array(
  78. 'title' => t('Upload'),
  79. 'class' => 'MediaBrowserUpload',
  80. 'weight' => -10,
  81. 'access callback' => 'user_access',
  82. 'access arguments' => array('create files'),
  83. );
  84. return $info;
  85. }
  86. /**
  87. * Alter the list of plugins for the media browser.
  88. *
  89. * @param array $info
  90. * The associative array of media browser plugin definitions from
  91. * hook_media_browser_plugin_info().
  92. *
  93. * @see hook_media_browser_plugin_info()
  94. * @see media_get_browser_plugin_info()
  95. */
  96. function hook_media_browser_plugin_info_alter(&$info) {
  97. $info['media_upload']['title'] = t('Upload 2.0');
  98. $info['media_upload']['class'] = 'MediaBrowserUploadImproved';
  99. }
  100. /**
  101. * Alter the plugins before they are rendered.
  102. *
  103. * @param array $plugin_output
  104. * The associative array of media browser plugin information from
  105. * media_get_browser_plugin_info().
  106. *
  107. * @see hook_media_browser_plugin_info()
  108. * @see media_get_browser_plugin_info()
  109. */
  110. function hook_media_browser_plugins_alter(&$plugin_output) {
  111. $plugin_output['upload']['form']['upload']['#title'] = t('Upload 2.0');
  112. $plugin_output['media_internet']['form']['embed_code']['#size'] = 100;
  113. }
  114. /**
  115. * Alter a singleton of the params passed to the media browser.
  116. *
  117. * @param array $stored_params
  118. * An array of parameters provided when a media_browser is launched.
  119. *
  120. * @see media_browser()
  121. * @see media_set_browser_params()
  122. */
  123. function hook_media_browser_params_alter(&$stored_params) {
  124. $stored_params['view_mode'] = 'custom';
  125. $stored_params['types'][] = 'document';
  126. unset($stored_params['enabledPlugins'][0]);
  127. }
  128. /**
  129. *
  130. * Alter the media multiple elements widget
  131. *
  132. * @param $table
  133. * the table of elements that will be rendered via theme_table()
  134. * @param $element
  135. * elements that are actually attached
  136. *
  137. * @see theme_media_widget_multiple()
  138. */
  139. function hook_media_widget_multiple_alter(&$table, &$element) {
  140. $table['attributes']['class'][] = 'myclass';
  141. }