-
Notifications
You must be signed in to change notification settings - Fork 2
/
audiofield.module
234 lines (208 loc) · 7.16 KB
/
audiofield.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
// Load all Field module hooks for Audio.
module_load_include('inc', 'audiofield', 'audio.field');
module_load_include('inc', 'audiofield', 'audiofield.players');
/**
* Implementation of hook_menu().
*/
function audiofield_menu() {
$items['admin/config/media/audiofield'] = array(
'title' => 'Audio Field',
'description' => 'Configure Audiofield.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('audiofield_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_permission().
*/
function audiofield_permission() {
return array(
'download own audio files' => array(
'title' => t('Download Own Audio Files'),
'description' => t('Let the users download their own audio files.'),
),
'download all audio files' => array(
'title' => t('Download All Audio Files'),
'description' => t('Let the users download any audio files.'),
),
);
}
function audiofield_admin_settings_form() {
global $base_path;
global $base_url;
$audio_players = audiofield_players();
$players = array();
$download_players = '';
foreach ($audio_players as $id => $player) {
if ((isset($player['path']) && file_exists($player['path'])) || (isset($player['local']) && $player['local']) || (isset($player['module']) && module_exists($player['module']))) {
foreach ($player['filetypes'] as $filetype) {
$players[$filetype][$id] = $player['name'] . '<br/>'; /* . call_user_func($player['callback'], $base_path . $player['path'], "");*/
}
}
else {
if (isset($player['download_link'])) {
$download_players .= '<li>Download ' . l($player['name'], $player['download_link']) . '</li>';
}
}
}
$config = config('audiofield.settings');
//MP3 Players list
if (!empty($players['mp3'])) {
$form['audiofield_audioplayer'] = array(
'#type' => 'radios',
'#title' => t('MP3 Audio Players'),
'#options' => $players['mp3'],
'#default_value' => $config->get('audioplayer'),
);
unset($players['mp3']);
}
//Other players list (wav, ogg,...)
foreach ($players as $filetype => $type_player) {
$form['audiofield_audioplayer_' . $filetype] = array(
'#type' => 'radios',
'#title' => check_plain($filetype . ' ' . t('Audio Players')),
'#options' => $type_player,
'#default_value' => $config->get('audioplayer_' . $filetype),
);
}
$form['audiofield_players_dir'] = array(
'#type' => 'textfield',
'#title' => t('Audio Players Directory'),
'#description' => t('Download and extract audio players in this directory'),
'#default_value' => $config->get('players_dir'),
);
if (!empty($download_players)) {
$form['audiofield_downloadaudioplayer'] = array(
'#type' => 'item',
'#title' => t('Download and install audio players'),
'#markup' => '<ul class="audiofield-download-players">' . $download_players . '</ul>',
);
}
// Add a submit button.
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Implementation of hook_theme().
*/
function audiofield_theme() {
$theme = _audiofield_theme();
return $theme;
}
/**
* Get the object for the suitable player for the parameter resource
*/
function audiofield_get_player($audio_url, $op, $options = array()) {
global $base_path;
//Lets convert $op to lowercase
$op = strtolower($op);
$audio_players = audiofield_players();
$file_type_setting_name = 'audioplayer' . ($op == 'mp3' ? '' : "_$op");
$player_id = config_get('audiofield.settings', $file_type_setting_name);
$player = isset($audio_players[$player_id]) ? $audio_players[$player_id] : NULL;
if (empty($player)) {
return audiofield_embeddedplayer($audio_url);
}
if(isset($player['path'])) {
$path = $base_path . $player['path'];
}
else {
$path = '';
}
return call_user_func($player['callback'], $path, $audio_url, $options);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function audiofield_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
$instance = $form['#instance'];
if ($instance['widget']['type'] == 'audiofield_widget' && $form['instance']['settings']['file_extensions']['#default_value'] == 'txt') {
$form['instance']['settings']['file_extensions']['#default_value'] = 'mp3';
}
}
/**
* Implementation of hook_form_[form_id]_alter().
*
* Modify the add new field form to change the default formatter.
*/
function audiofield_form_field_ui_field_settings_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'audiofield_form_content_field_overview_submit';
}
/**
* Submit handler to set a new field's formatter to "audiofield_embedded".
*/
function audiofield_form_content_field_overview_submit(&$form, &$form_state) {
$entity_type = 'node';
$field_name = $form_state['values']['field']['field_name'];
$bundle = $form_state['complete form']['#bundle'];
$instance = field_read_instance($entity_type, $field_name, $bundle);
if ($instance['widget']['module'] == 'audiofield') {
foreach ($instance['display'] as $display_type => $display_settings) {
if ($instance['display'][$display_type]['type'] == 'file_default') {
$instance['display'][$display_type]['type'] = 'audiofield_embedded';
}
}
field_update_instance($instance);
}
}
/**
* Implements hook_field_conditional_state_settings_alter().
*
* Add support for Field Conditional States
*/
function audiofield_field_conditional_state_settings_alter(&$settings) {
$settings['audiofield_widget'] = array(
'form_elements' => array(0=>array(0, 'upload')),
'field_data' => array(0),
'reprocess_from_root' => TRUE,
'field_states' => array(
'enabled',
'disabled',
'required',
'optional',
'visible',
'invisible',
),
'trigger_states' => array('empty', 'filled'),
'trigger_value_widget' => '_field_conditional_state_default_trigger_value_widget',
'trigger_value_submit' => '_field_conditional_state_default_trigger_value_submit',
);
}
/**
* Implements hook_filefield_sources_widgets().
*
* This returns a list of widgets that are compatible with FileField Sources.
*/
function audiofield_filefield_sources_widgets() {
return array('audiofield_widget');
}
/**
* Submit handler for module_settings_form().
*/
function audiofield_admin_settings_form_submit($form, &$form_state) {
$config = config('audiofield.settings');
$config->set('audioplayer', $form_state['values']['audiofield_audioplayer']);
$config->set('audioplayer_wav', $form_state['values']['audiofield_audioplayer_wav']);
$config->set('audioplayer_ogg', $form_state['values']['audiofield_audioplayer_ogg']);
$config->set('players_dir', $form_state['values']['audiofield_players_dir']);
$config->save();
backdrop_set_message(t('The AudioField configuration options have been saved.'));
}
/**
* Implements hook_config_info().
*/
function audiofield_config_info() {
$prefixes['audiofield.settings'] = array(
'label' => t('Audiofield settings'),
'group' => t('Configuration'),
);
return $prefixes;
}