audiofield.module 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @file
  4. * Drupal Module: Audiofield.
  5. *
  6. * Allows the use of multiple audio player plugins for display of audio files.
  7. */
  8. use Drupal\Core\Routing\RouteMatchInterface;
  9. /**
  10. * Implements hook_library_info_build().
  11. */
  12. function audiofield_library_info_build() {
  13. $libraries = [];
  14. // Here we dynamically build a library for each skin.
  15. // This allows users to add custom skins to the library's skins folder.
  16. foreach (_audiofield_list_skins('jplayer_audio_player') as $skin) {
  17. $libraries[$skin['library_name']] = [
  18. 'css' => [
  19. 'component' => [
  20. $skin['path'] => [
  21. 'minified' => TRUE,
  22. ],
  23. ],
  24. ],
  25. 'dependencies' => [
  26. 'core/jquery',
  27. ],
  28. ];
  29. }
  30. return $libraries;
  31. }
  32. /**
  33. * Helper function to list available skins.
  34. */
  35. function _audiofield_list_skins($player) {
  36. $skins = [];
  37. if ($player == 'jplayer_audio_player') {
  38. $files = file_scan_directory(DRUPAL_ROOT . '/libraries/jplayer/dist/skin/', '#.*\.min\.css#');
  39. foreach ($files as $file) {
  40. // Generate a human readable name.
  41. $name = preg_replace("/^(jplayer\.)(.*)(\.min)/", '$2', $file->name);
  42. $skins[$name] = [
  43. 'library_name' => 'audiofield.jplayer.theme_' . preg_replace('#[^A-Za-z0-9]#', '', $name),
  44. 'name' => $name,
  45. 'path' => str_replace(DRUPAL_ROOT, '', $file->uri),
  46. ];
  47. }
  48. }
  49. ksort($skins);
  50. return $skins;
  51. }
  52. /**
  53. * Implements hook_theme().
  54. */
  55. function audiofield_theme($existing, $type, $theme, $path) {
  56. return [
  57. 'audiofield_download_links' => [
  58. 'variables' => [
  59. 'links' => NULL,
  60. ],
  61. ],
  62. 'audioplayer' => [
  63. 'variables' => [
  64. 'plugin_id' => NULL,
  65. 'plugin_theme' => NULL,
  66. 'files' => NULL,
  67. 'settings' => NULL,
  68. ],
  69. ],
  70. ];
  71. }
  72. /**
  73. * Implements hook_theme_suggestions_HOOK_alter().
  74. */
  75. function audiofield_theme_suggestions_audioplayer_alter(array &$suggestions, array $variables) {
  76. // Suggest a template using the plugin name.
  77. $suggestions[] = 'audioplayer__' . $variables['plugin_id'];
  78. // Suggest a template with the plugin/theme name if it exists.
  79. if (!empty($variables['plugin_theme'])) {
  80. $suggestions[] = 'audioplayer__' . $variables['plugin_id'] . '__' . $variables['plugin_theme'];
  81. }
  82. }
  83. /**
  84. * Implements hook_theme_registry_alter().
  85. */
  86. function audiofield_theme_registry_alter(&$theme_registry) {
  87. /*
  88. * By default, Drupal 8 does not include theme suggestions from inside the
  89. * module in which they were created, so we must add them manually here.
  90. */
  91. $path = drupal_get_path('module', 'audiofield');
  92. $audioplayer_templates = drupal_find_theme_templates($theme_registry, '.html.twig', $path);
  93. foreach ($audioplayer_templates as &$audioplayer_template) {
  94. $audioplayer_template['type'] = 'module';
  95. }
  96. $theme_registry += $audioplayer_templates;
  97. }
  98. /**
  99. * Implements hook_help().
  100. */
  101. function audiofield_help($route_name, RouteMatchInterface $route_match) {
  102. switch ($route_name) {
  103. case 'help.page.audiofield':
  104. $output = '';
  105. $output .= '<h3>' . t('About') . '</h3>';
  106. $output .= '<p>' . t('AudioField is a file formatter for the File and Link modules. It allows you to upload or link audio files and play them on your site using one of several audio players available. It supports most audio formats (depends on the selected audio player). Please see <a href=":url">the module documentation</a> for examples of the players in use.', [':url' => 'https://www.drupal.org/docs/8/modules/audiofield']) . '</p>';
  107. $output .= '<h3>' . t('Uses') . '</h3>';
  108. $output .= '<p>' . t('Audiofield supports the following audio players, which must be downloaded and installed as libraries:') . '</p>';
  109. $output .= '<dl>';
  110. $output .= '<dt>' . t('Default HTML5 audio player') . '</dt>';
  111. $output .= '<dd>' . t('Built into major browsers.') . '</dd>';
  112. $output .= '<dt>' . t('Audio.js') . '</dt>';
  113. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  114. ':url' => 'https://kolber.github.io/audiojs/',
  115. ]) . '</dd>';
  116. $output .= '<dt>' . t('jPlayer') . '</dt>';
  117. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  118. ':url' => 'http://jplayer.org/',
  119. ]) . '</dd>';
  120. $output .= '<dt>' . t('MediaElement') . '</dt>';
  121. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  122. ':url' => 'http://mediaelementjs.com/',
  123. ]) . '</dd>';
  124. $output .= '<dt>' . t('Projekktor') . '</dt>';
  125. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  126. ':url' => 'https://github.com/frankyghost/projekktor',
  127. ]) . '</dd>';
  128. $output .= '<dt>' . t('SoundManager') . '</dt>';
  129. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  130. ':url' => 'http://www.schillmania.com/projects/soundmanager2',
  131. ]) . '</dd>';
  132. $output .= '<dt>' . t('wavesurfer.js') . '</dt>';
  133. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  134. ':url' => 'https://wavesurfer-js.org',
  135. ]) . '</dd>';
  136. $output .= '<dt>' . t('WordPress Audio') . '</dt>';
  137. $output .= '<dd>' . t('<a href=":url">:url</a>', [
  138. ':url' => 'http://wpaudioplayer.com',
  139. ]) . '</dd>';
  140. $output .= '</dl>';
  141. return $output;
  142. }
  143. }