1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* File formatters for YouTube videos.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_file_formatter_info().
|
10
|
*/
|
11
|
function media_youtube_file_formatter_info() {
|
12
|
$formatters['media_youtube_video'] = array(
|
13
|
'label' => t('YouTube Video'),
|
14
|
'file types' => array('video'),
|
15
|
'default settings' => array(
|
16
|
'width' => 640,
|
17
|
'height' => 390,
|
18
|
'autohide' => 2,
|
19
|
'autoplay' => FALSE,
|
20
|
'color' => 'red',
|
21
|
'enablejsapi' => FALSE,
|
22
|
'loop' => FALSE,
|
23
|
'modestbranding' => FALSE,
|
24
|
'nocookie' => FALSE,
|
25
|
'origin' => '',
|
26
|
'protocol' => 'https:',
|
27
|
'protocol_specify' => FALSE,
|
28
|
'rel' => TRUE,
|
29
|
'showinfo' => TRUE,
|
30
|
'theme' => 'dark',
|
31
|
'captions' => FALSE,
|
32
|
),
|
33
|
'view callback' => 'media_youtube_file_formatter_video_view',
|
34
|
'settings callback' => 'media_youtube_file_formatter_video_settings',
|
35
|
'mime types' => array('video/youtube'),
|
36
|
);
|
37
|
|
38
|
$formatters['media_youtube_image'] = array(
|
39
|
'label' => t('YouTube Preview Image'),
|
40
|
'file types' => array('video'),
|
41
|
'default settings' => array(
|
42
|
'image_style' => '',
|
43
|
),
|
44
|
'view callback' => 'media_youtube_file_formatter_image_view',
|
45
|
'settings callback' => 'media_youtube_file_formatter_image_settings',
|
46
|
'mime types' => array('video/youtube'),
|
47
|
);
|
48
|
|
49
|
return $formatters;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Implements hook_file_formatter_FORMATTER_view().
|
54
|
*/
|
55
|
function media_youtube_file_formatter_video_view($file, $display, $langcode) {
|
56
|
$scheme = file_uri_scheme($file->uri);
|
57
|
|
58
|
// WYSIWYG does not yet support video inside a running editor instance.
|
59
|
if ($scheme == 'youtube' && empty($file->override['wysiwyg'])) {
|
60
|
$element = array(
|
61
|
'#theme' => 'media_youtube_video',
|
62
|
'#uri' => $file->uri,
|
63
|
'#options' => array(),
|
64
|
);
|
65
|
|
66
|
// Fake a default for attributes so the ternary doesn't choke.
|
67
|
$display['settings']['attributes'] = array();
|
68
|
|
69
|
foreach (array('width', 'height', 'autohide', 'autoplay', 'color', 'enablejsapi', 'loop', 'modestbranding', 'nocookie', 'origin', 'protocol', 'protocol_specify', 'rel', 'showinfo', 'theme', 'attributes', 'captions') as $setting) {
|
70
|
$element['#options'][$setting] = isset($file->override[$setting]) ? $file->override[$setting] : $display['settings'][$setting];
|
71
|
}
|
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
|
$element['captions'] = array(
|
128
|
'#title' => t('Captions displaying options'),
|
129
|
'#type' => 'radios',
|
130
|
'#options' => array(
|
131
|
'0' => t('Turns captions off by default'),
|
132
|
'1' => t('Turns captions on by default'),
|
133
|
'2' => t('Turns captions on and set the caption language for the video by default'),
|
134
|
),
|
135
|
'#default_value' => $settings['captions'],
|
136
|
);
|
137
|
|
138
|
// Single Options.
|
139
|
$element['autoplay'] = array(
|
140
|
'#title' => t('Autoplay video on load'),
|
141
|
'#type' => 'checkbox',
|
142
|
'#default_value' => $settings['autoplay'],
|
143
|
);
|
144
|
$element['loop'] = array(
|
145
|
'#title' => t('Loop video'),
|
146
|
'#type' => 'checkbox',
|
147
|
'#default_value' => $settings['loop'],
|
148
|
);
|
149
|
|
150
|
// Note: make sure the positive/negitive language lines up with option
|
151
|
// processing in media_youtube.theme.inc.
|
152
|
$element['showinfo'] = array(
|
153
|
'#title' => t('Display video title and uploader'),
|
154
|
'#type' => 'checkbox',
|
155
|
'#default_value' => $settings['showinfo'],
|
156
|
);
|
157
|
$element['modestbranding'] = array(
|
158
|
'#title' => t('Remove YouTube logo from the control bar'),
|
159
|
'#type' => 'checkbox',
|
160
|
'#default_value' => $settings['modestbranding'],
|
161
|
);
|
162
|
$element['rel'] = array(
|
163
|
'#title' => t('Show related videos when playback ends'),
|
164
|
'#type' => 'checkbox',
|
165
|
'#default_value' => $settings['rel'],
|
166
|
);
|
167
|
$element['nocookie'] = array(
|
168
|
'#title' => t('Use privacy enhanced (no cookie) mode'),
|
169
|
'#type' => 'checkbox',
|
170
|
'#default_value' => $settings['nocookie'],
|
171
|
);
|
172
|
$element['protocol_specify'] = array(
|
173
|
'#title' => t('Specify an http protocol'),
|
174
|
'#type' => 'checkbox',
|
175
|
'#default_value' => $settings['protocol_specify'],
|
176
|
'#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.'),
|
177
|
);
|
178
|
$element['protocol'] = array(
|
179
|
'#title' => t('Iframe protocol'),
|
180
|
'#type' => 'radios',
|
181
|
'#default_value' => $settings['protocol'],
|
182
|
'#options' => array(
|
183
|
'http:' => 'http://',
|
184
|
'https:' => 'https://',
|
185
|
),
|
186
|
'#states' => array(
|
187
|
'invisible' => array(
|
188
|
':input[name="displays[media_youtube_video][settings][protocol_specify]"]' => array('checked' => FALSE),
|
189
|
),
|
190
|
),
|
191
|
);
|
192
|
|
193
|
// JS api.
|
194
|
$element['enablejsapi'] = array(
|
195
|
'#title' => t('Enable the <a href="https://developers.google.com/youtube/js_api_reference">Javascript API</a>'),
|
196
|
'#type' => 'checkbox',
|
197
|
'#default_value' => $settings['enablejsapi'],
|
198
|
'#description' => 'An id in the format <code>media-youtube-{video_id}</code> will be added to each iframe.',
|
199
|
);
|
200
|
$element['origin'] = array(
|
201
|
'#title' => t('Origin'),
|
202
|
'#type' => 'textfield',
|
203
|
'#description' => t('If the Javascript API is enabled, enter your site\'s domain for added security.'),
|
204
|
'#default_value' => $settings['origin'],
|
205
|
'#states' => array(
|
206
|
'invisible' => array(
|
207
|
':input[name="displays[media_youtube_video][settings][enablejsapi]"]' => array('checked' => FALSE),
|
208
|
),
|
209
|
),
|
210
|
'#element_validate' => array('_youtube_validate_jsapi_domain'),
|
211
|
);
|
212
|
|
213
|
return $element;
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Validation for width and height.
|
218
|
*/
|
219
|
function _youtube_validate_video_width_and_height($element, &$form_state, $form) {
|
220
|
// Check if the value is a number with an optional decimal or percentage sign, or "auto".
|
221
|
if (!empty($element['#value']) && !preg_match('/^(auto|([0-9]*(\.[0-9]+)?%?))$/', $element['#value'])) {
|
222
|
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'])));
|
223
|
}
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Validation for Js API Origin.
|
228
|
*/
|
229
|
function _youtube_validate_jsapi_domain ($element, &$form_state, $form) {
|
230
|
// Check if the value is a url with http/s and no trailing directories.
|
231
|
if (!empty($element['#value']) && !preg_match('/^https?\:\/\/[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}){1,2}$/', $element['#value'])) {
|
232
|
form_error($element, t('Please insert a valid domain in the format http://www.yourdomain.com'));
|
233
|
}
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Implements hook_file_formatter_FORMATTER_view().
|
238
|
*/
|
239
|
function media_youtube_file_formatter_image_view($file, $display, $langcode) {
|
240
|
$scheme = file_uri_scheme($file->uri);
|
241
|
if ($scheme == 'youtube') {
|
242
|
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
|
243
|
$image_style = $display['settings']['image_style'];
|
244
|
$valid_image_styles = image_style_options(FALSE);
|
245
|
|
246
|
if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
|
247
|
$element = array(
|
248
|
'#theme' => 'image',
|
249
|
'#path' => str_replace('http:', '', $wrapper->getOriginalThumbnailPath()),
|
250
|
'#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
|
251
|
);
|
252
|
}
|
253
|
else {
|
254
|
$element = array(
|
255
|
'#theme' => 'image_style',
|
256
|
'#style_name' => $image_style,
|
257
|
'#path' => $wrapper->getLocalThumbnailPath(),
|
258
|
'#alt' => isset($file->override['attributes']['alt']) ? $file->override['attributes']['alt'] : $file->filename,
|
259
|
);
|
260
|
}
|
261
|
|
262
|
return $element;
|
263
|
}
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Implements hook_file_formatter_FORMATTER_settings().
|
268
|
*/
|
269
|
function media_youtube_file_formatter_image_settings($form, &$form_state, $settings) {
|
270
|
$element = array();
|
271
|
|
272
|
$element['image_style'] = array(
|
273
|
'#title' => t('Image style'),
|
274
|
'#type' => 'select',
|
275
|
'#options' => image_style_options(FALSE),
|
276
|
'#default_value' => $settings['image_style'],
|
277
|
'#empty_option' => t('None (original image)'),
|
278
|
);
|
279
|
|
280
|
return $element;
|
281
|
}
|