1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file media_youtube/includes/media_youtube.formatters.inc
|
5
|
*
|
6
|
* Formatters for Media: YouTube.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Implements hook_file_formatter_info().
|
11
|
*/
|
12
|
function media_youtube_file_formatter_info() {
|
13
|
$formatters['media_youtube_video'] = array(
|
14
|
'label' => t('YouTube Video'),
|
15
|
'file types' => array('video'),
|
16
|
'default settings' => array(),
|
17
|
'view callback' => 'media_youtube_file_formatter_video_view',
|
18
|
'settings callback' => 'media_youtube_file_formatter_video_settings',
|
19
|
'mime types' => array('video/youtube'),
|
20
|
);
|
21
|
|
22
|
$formatters['media_youtube_video']['default settings'] = array(
|
23
|
'width' => 640,
|
24
|
'height' => 390,
|
25
|
'autohide' => 2,
|
26
|
'autoplay' => FALSE,
|
27
|
'color' => 'red',
|
28
|
'enablejsapi' => FALSE,
|
29
|
'loop' => FALSE,
|
30
|
'modestbranding' => FALSE,
|
31
|
'nocookie' => FALSE,
|
32
|
'origin' => '',
|
33
|
'protocol' => 'https:',
|
34
|
'protocol_specify' => FALSE,
|
35
|
'rel' => TRUE,
|
36
|
'showinfo' => TRUE,
|
37
|
'theme' => 'dark',
|
38
|
);
|
39
|
|
40
|
$formatters['media_youtube_image'] = array(
|
41
|
'label' => t('YouTube Preview Image'),
|
42
|
'file types' => array('video'),
|
43
|
'default settings' => array(
|
44
|
'image_style' => '',
|
45
|
),
|
46
|
'view callback' => 'media_youtube_file_formatter_image_view',
|
47
|
'settings callback' => 'media_youtube_file_formatter_image_settings',
|
48
|
'mime types' => array('video/youtube'),
|
49
|
);
|
50
|
|
51
|
return $formatters;
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Implements hook_file_formatter_FORMATTER_view().
|
56
|
*/
|
57
|
function media_youtube_file_formatter_video_view($file, $display, $langcode) {
|
58
|
$scheme = file_uri_scheme($file->uri);
|
59
|
// WYSIWYG does not yet support video inside a running editor instance.
|
60
|
if ($scheme == 'youtube' && empty($file->override['wysiwyg'])) {
|
61
|
$element = array(
|
62
|
'#theme' => 'media_youtube_video',
|
63
|
'#uri' => $file->uri,
|
64
|
'#options' => array(),
|
65
|
);
|
66
|
|
67
|
// Fake a default for attributes so the ternary doesn't choke.
|
68
|
$display['settings']['attributes'] = array();
|
69
|
|
70
|
foreach (array('width', 'height', 'autohide', 'autoplay', 'color', 'enablejsapi', 'loop', 'modestbranding', 'nocookie', 'origin', 'protocol', 'protocol_specify', 'rel', 'showinfo', 'theme', 'attributes') as $setting) {
|
71
|
$element['#options'][$setting] = isset($file->override[$setting]) ? $file->override[$setting] : $display['settings'][$setting];
|
72
|
}
|
73
|
return $element;
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Implements hook_file_formatter_FORMATTER_settings().
|
79
|
*/
|
80
|
function media_youtube_file_formatter_video_settings($form, &$form_state, $settings) {
|
81
|
$element = array();
|
82
|
|
83
|
$element['width'] = array(
|
84
|
'#title' => t('Width'),
|
85
|
'#type' => 'textfield',
|
86
|
'#default_value' => $settings['width'],
|
87
|
'#element_validate' => array('_youtube_validate_video_width_and_height'),
|
88
|
);
|
89
|
$element['height'] = array(
|
90
|
'#title' => t('Height'),
|
91
|
'#type' => 'textfield',
|
92
|
'#default_value' => $settings['height'],
|
93
|
'#element_validate' => array('_youtube_validate_video_width_and_height'),
|
94
|
);
|
95
|
|
96
|
// @see https://developers.google.com/youtube/player_parameters#Parameters
|
97
|
|
98
|
// Multiple options.
|
99
|
$element['theme'] = array(
|
100
|
'#title' => t('Player theme'),
|
101
|
'#type' => 'radios',
|
102
|
'#options' => array(
|
103
|
'dark' => t('Dark'),
|
104
|
'light' => t('Light'),
|
105
|
),
|
106
|
'#default_value' => $settings['theme'],
|
107
|
);
|
108
|
$element['color'] = array(
|
109
|
'#title' => t('Progress bar color'),
|
110
|
'#type' => 'radios',
|
111
|
'#options' => array(
|
112
|
'red' => t('Red'),
|
113
|
'white' => t('White'),
|
114
|
),
|
115
|
'#default_value' => $settings['color'],
|
116
|
);
|
117
|
$element['autohide'] = array(
|
118
|
'#title' => t('Controls during playback'),
|
119
|
'#type' => 'radios',
|
120
|
'#options' => array(
|
121
|
'0' => t('Keep progress bar and player controls on screen while playing'),
|
122
|
'2' => t('Hide progress bar while playing'),
|
123
|
'1' => t('Hide progress bar and player controls'),
|
124
|
),
|
125
|
'#default_value' => $settings['autohide'],
|
126
|
);
|
127
|
|
128
|
// Single Options.
|
129
|
$element['autoplay'] = array(
|
130
|
'#title' => t('Autoplay video on load'),
|
131
|
'#type' => 'checkbox',
|
132
|
'#default_value' => $settings['autoplay'],
|
133
|
);
|
134
|
$element['loop'] = array(
|
135
|
'#title' => t('Loop video'),
|
136
|
'#type' => 'checkbox',
|
137
|
'#default_value' => $settings['loop'],
|
138
|
);
|
139
|
|
140
|
// Note: make sure the positive/negitive language lines up with option
|
141
|
// processing in media_youtube.theme.inc.
|
142
|
$element['showinfo'] = array(
|
143
|
'#title' => t('Display video title and uploader'),
|
144
|
'#type' => 'checkbox',
|
145
|
'#default_value' => $settings['showinfo'],
|
146
|
);
|
147
|
$element['modestbranding'] = array(
|
148
|
'#title' => t('Remove YouTube logo from the control bar'),
|
149
|
'#type' => 'checkbox',
|
150
|
'#default_value' => $settings['modestbranding'],
|
151
|
);
|
152
|
$element['rel'] = array(
|
153
|
'#title' => t('Show related videos when playback ends'),
|
154
|
'#type' => 'checkbox',
|
155
|
'#default_value' => $settings['rel'],
|
156
|
);
|
157
|
$element['nocookie'] = array(
|
158
|
'#title' => t('Use privacy enhanced (no cookie) mode'),
|
159
|
'#type' => 'checkbox',
|
160
|
'#default_value' => $settings['nocookie'],
|
161
|
);
|
162
|
$element['protocol_specify'] = array(
|
163
|
'#title' => t('Specify an http protocol'),
|
164
|
'#type' => 'checkbox',
|
165
|
'#default_value' => $settings['protocol_specify'],
|
166
|
'#description' => t('An explicit protocol may be neccesary for videos embedded in RSS feeds and emails. If no protocol is specified, iframes will be protocol relative.'),
|
167
|
);
|
168
|
$element['protocol'] = array(
|
169
|
'#title' => t('Iframe protocol'),
|
170
|
'#type' => 'radios',
|
171
|
'#default_value' => $settings['protocol'],
|
172
|
'#options' => array(
|
173
|
'http:' => 'http://',
|
174
|
'https:' => 'https://',
|
175
|
),
|
176
|
'#states' => array(
|
177
|
'invisible' => array(
|
178
|
':input[name="displays[media_youtube_video][settings][protocol_specify]"]' => array('checked' => FALSE),
|
179
|
),
|
180
|
),
|
181
|
);
|
182
|
|
183
|
// JS api.
|
184
|
$element['enablejsapi'] = array(
|
185
|
'#title' => t('Enable the <a href="https://developers.google.com/youtube/js_api_reference">Javascript API</a>'),
|
186
|
'#type' => 'checkbox',
|
187
|
'#default_value' => $settings['enablejsapi'],
|
188
|
'#description' => 'An id in the format <code>media-youtube-{video_id}</code> will be added to each iframe.',
|
189
|
);
|
190
|
$element['origin'] = array(
|
191
|
'#title' => t('Origin'),
|
192
|
'#type' => 'textfield',
|
193
|
'#description' => t('If the Javascript API is enabled, enter your site\'s domain for added security.'),
|
194
|
'#default_value' => $settings['origin'],
|
195
|
'#states' => array(
|
196
|
'invisible' => array(
|
197
|
':input[name="displays[media_youtube_video][settings][enablejsapi]"]' => array('checked' => FALSE),
|
198
|
),
|
199
|
),
|
200
|
'#element_validate' => array('_youtube_validate_jsapi_domain'),
|
201
|
);
|
202
|
|
203
|
return $element;
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Validation for width and height.
|
208
|
*/
|
209
|
function _youtube_validate_video_width_and_height($element, &$form_state, $form) {
|
210
|
|
211
|
// Check if the value is a number with an optional decimal or percentage sign, or "auto".
|
212
|
if (!empty($element['#value']) && !preg_match('/^(auto|([0-9]*(\.[0-9]+)?%?))$/', $element['#value'])) {
|
213
|
form_error($element, t("The value entered for @dimension is invalid. Please insert a unitless integer for pixels, a percent, or \"auto\". Note that percent and auto may not function correctly depending on the browser and doctype.", array('@dimension' => $element['#title'])));
|
214
|
}
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Validation for Js API Origin.
|
219
|
*/
|
220
|
function _youtube_validate_jsapi_domain ($element, &$form_state, $form) {
|
221
|
|
222
|
// Check if the value is a url with http/s and no trailing directories.
|
223
|
if (!empty($element['#value']) && !preg_match('/^https?\:\/\/[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}){1,2}$/', $element['#value'])) {
|
224
|
form_error($element, t('Please insert a valid domain in the format http://www.yourdomain.com'));
|
225
|
}
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Implements hook_file_formatter_FORMATTER_view().
|
230
|
*/
|
231
|
function media_youtube_file_formatter_image_view($file, $display, $langcode) {
|
232
|
$scheme = file_uri_scheme($file->uri);
|
233
|
if ($scheme == 'youtube') {
|
234
|
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
|
235
|
$image_style = $display['settings']['image_style'];
|
236
|
$valid_image_styles = image_style_options(FALSE);
|
237
|
// @TODO: If autosubmit is removed and we allow view modes that insert
|
238
|
// images in the WYSIWYG, add file->overrides handling.
|
239
|
if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
|
240
|
$element = array(
|
241
|
'#theme' => 'image',
|
242
|
'#path' => $wrapper->getOriginalThumbnailPath(),
|
243
|
'#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
|
244
|
);
|
245
|
}
|
246
|
else {
|
247
|
$element = array(
|
248
|
'#theme' => 'image_style',
|
249
|
'#style_name' => $image_style,
|
250
|
'#path' => $wrapper->getLocalThumbnailPath(),
|
251
|
'#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
|
252
|
);
|
253
|
}
|
254
|
|
255
|
return $element;
|
256
|
}
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Implements hook_file_formatter_FORMATTER_settings().
|
261
|
*/
|
262
|
function media_youtube_file_formatter_image_settings($form, &$form_state, $settings) {
|
263
|
$element = array();
|
264
|
$element['image_style'] = array(
|
265
|
'#title' => t('Image style'),
|
266
|
'#type' => 'select',
|
267
|
'#options' => image_style_options(FALSE),
|
268
|
'#default_value' => $settings['image_style'],
|
269
|
'#empty_option' => t('None (original image)'),
|
270
|
);
|
271
|
return $element;
|
272
|
}
|
273
|
|
274
|
/**
|
275
|
* Implements hook_file_default_displays_alter().
|
276
|
*/
|
277
|
function media_youtube_file_default_displays_alter(&$file_displays) {
|
278
|
// Video previews should be displayed using a large filetype icon.
|
279
|
$file_display = new stdClass();
|
280
|
$file_display->api_version = 1;
|
281
|
$file_display->name = 'video__default__media_youtube_video';
|
282
|
$file_display->weight = 0;
|
283
|
$file_display->status = TRUE;
|
284
|
$file_display->settings = array(
|
285
|
'width' => '640',
|
286
|
'height' => '390',
|
287
|
'theme' => 'dark',
|
288
|
'color' => 'red',
|
289
|
'autohide' => '2',
|
290
|
'autoplay' => 0,
|
291
|
'loop' => 0,
|
292
|
'showinfo' => 1,
|
293
|
'modestbranding' => 0,
|
294
|
'rel' => 1,
|
295
|
'nocookie' => 0,
|
296
|
'protocol_specify' => 0,
|
297
|
'protocol' => 'https:',
|
298
|
'enablejsapi' => 0,
|
299
|
'origin' => '',
|
300
|
);
|
301
|
$file_displays['video__default__media_youtube_video'] = $file_display;
|
302
|
|
303
|
$file_display = new stdClass();
|
304
|
$file_display->api_version = 1;
|
305
|
$file_display->name = 'video__preview__media_youtube_image';
|
306
|
$file_display->weight = 0;
|
307
|
$file_display->status = TRUE;
|
308
|
$file_display->settings = array(
|
309
|
'image_style' => 'media_thumbnail',
|
310
|
);
|
311
|
$file_displays['video__preview__media_youtube_image'] = $file_display;
|
312
|
|
313
|
$file_display = new stdClass();
|
314
|
$file_display->api_version = 1;
|
315
|
$file_display->name = 'video__teaser__media_youtube_video';
|
316
|
$file_display->weight = 0;
|
317
|
$file_display->status = TRUE;
|
318
|
$file_display->settings = array(
|
319
|
'width' => '560',
|
320
|
'height' => '340',
|
321
|
'theme' => 'dark',
|
322
|
'color' => 'red',
|
323
|
'autohide' => '2',
|
324
|
'autoplay' => 0,
|
325
|
'loop' => 0,
|
326
|
'showinfo' => 1,
|
327
|
'modestbranding' => 0,
|
328
|
'rel' => 1,
|
329
|
'nocookie' => 0,
|
330
|
'protocol_specify' => 0,
|
331
|
'protocol' => 'https:',
|
332
|
'enablejsapi' => 0,
|
333
|
'origin' => '',
|
334
|
);
|
335
|
$file_displays['video__teaser__media_youtube_video'] = $file_display;
|
336
|
}
|