youtube.module
19 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?php
/**
* @file
* Youtube field module adds a field for YouTube videos.
*/
require_once (dirname(__FILE__) . '/youtube.inc');
/**
* Implements hook_menu().
*/
function youtube_menu() {
$items['admin/config/media/youtube'] = array(
'title' => 'YouTube settings',
'description' => 'Configure sitewide settings for embedded YouTube video fields.',
'page callback' => 'drupal_get_form',
'page arguments' => array('youtube_settings_form'),
'access arguments' => array('administer youtube'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function youtube_permission() {
return array(
'administer youtube' => array(
'title' => t('Administer YouTube field'),
'description' => t('Set default configurations for YouTube field settings.'),
),
);
}
/**
* Settings form for YouTube field module.
*/
function youtube_settings_form($form) {
$form = array();
$form['text'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('The following settings will be used as default values
on all YouTube video fields. More settings can be altered in the display
settings of individual fields.') . '</p>',
);
$form['youtube_global'] = array(
'#type' => 'fieldset',
'#title' => t('Video parameters'),
);
$form['youtube_global']['youtube_suggest'] = array(
'#type' => 'checkbox',
'#title' => t('Show suggested videos when the video finishes (rel).'),
'#default_value' => variable_get('youtube_suggest', TRUE),
);
$form['youtube_global']['youtube_modestbranding'] = array(
'#type' => 'checkbox',
'#title' => t('Do not show YouTube logo on video player (modestbranding).'),
'#default_value' => variable_get('youtube_modestbranding', FALSE),
);
$form['youtube_global']['youtube_theme'] = array(
'#type' => 'checkbox',
'#title' => t('Use a light colored control bar for video player controls (theme).'),
'#default_value' => variable_get('youtube_theme', FALSE),
);
$form['youtube_global']['youtube_color'] = array(
'#type' => 'checkbox',
'#title' => t('Use a white colored video progress bar (color).'),
'#default_value' => variable_get('youtube_color', FALSE),
'#description' => 'Note: the modestbranding parameter will be ignored when this is in use.',
);
$form['youtube_global']['youtube_enablejsapi'] = array(
'#type' => 'checkbox',
'#title' => t('Enable use of the JavaScript API (enablejsapi, origin).'),
'#default_value' => variable_get('youtube_enablejsapi', FALSE),
'#description' => 'For more information on the Javascript API and how to use it, see the <a href="https://developers.google.com/youtube/js_api_reference">JavaScript API documentation</a>.',
);
$form['youtube_global']['youtube_wmode'] = array(
'#type' => 'checkbox',
'#title' => t('Fix overlay problem in IE (wmode).'),
'#default_value' => variable_get('youtube_wmode', TRUE),
'#description' => t('Checking this will fix the issue of a YouTube video showing above elements with fixed or absolute positioning (including Drupal\'s Overlay and Toolbar).'),
);
$form['youtube_thumbs'] = array(
'#type' => 'fieldset',
'#title' => t('Thumbnails'),
);
$form['youtube_thumbs']['youtube_thumb_dir'] = array(
'#type' => 'textfield',
'#title' => t('YouTube thumbnail directory'),
'#field_prefix' => variable_get('file_public_path', conf_path() . '/files') . '/',
'#field_suffix' => '/thumbnail.png',
'#description' => t('Location, within the files directory, where you would like the YouTube thumbnails stored.'),
'#default_value' => variable_get('youtube_thumb_dir', 'youtube'),
);
$form['youtube_thumbs']['youtube_thumb_hires'] = array(
'#type' => 'checkbox',
'#title' => t('Save higher resolution thumbnail images'),
'#description' => t('This will save thumbnails larger than the default size, 480x360, to the thumbnails directory specified above.'),
'#default_value' => variable_get('youtube_thumb_hires', FALSE),
);
$form['youtube_thumbs']['youtube_thumb_delete_all'] = array(
'#type' => 'submit',
'#value' => t('Refresh existing thumbnail image files'),
'#submit' => array('youtube_thumb_delete_all'),
);
$form['youtube_privacy'] = array(
'#type' => 'checkbox',
'#title' => t('Enable privacy-enhanced mode.'),
'#default_value' => variable_get('youtube_privacy', FALSE),
'#description' => t('Checking this box will prevent YouTube from setting cookies in your site visitors browser.'),
);
$form['youtube_player_class'] = array(
'#type' => 'textfield',
'#title' => t('YouTube player class'),
'#default_value' => variable_get('youtube_player_class', 'youtube-field-player'),
'#description' => t('The iframe of every player will be given this class. They will also be given IDs based off of this value.'),
);
return system_settings_form($form);
}
/**
* Implements hook_field_info().
*/
function youtube_field_info() {
return array(
// We name our field as the associative name of the array.
'youtube' => array(
'label' => t('YouTube video'),
'description' => t('A video hosted on YouTube.'),
'default_widget' => 'youtube',
'default_formatter' => 'youtube_video',
'property_type' => 'youtube',
'property_callbacks' => array('youtube_property_info_callback'),
),
);
}
/**
* Callback to alter the property info of youtube fields.
*
* @see hook_field_info().
*/
function youtube_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
$name = $field['field_name'];
$property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
$property['type'] = ($field['cardinality'] != 1) ? 'list<youtube>' : 'youtube';
$property['getter callback'] = 'entity_metadata_field_verbatim_get';
$property['setter callback'] = 'entity_metadata_field_verbatim_set';
$property['property info'] = youtube_field_data_property_info();
unset($property['query callback']);
}
/**
* Defines info for the properties of youtube field data.
*/
function youtube_field_data_property_info($name = NULL) {
return array(
'input' => array(
'label' => t('YouTube URL'),
'description' => t('The absolute URL for the YouTube video.'),
'type' => 'text',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'video_id' => array(
'label' => t('YouTube Video ID'),
'description' => t('The ID assigned to the YouTube video'),
'type' => 'text',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
);
}
/**
* Implements hook_field_validate().
*/
function youtube_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['input'])) {
$video_id = youtube_get_video_id($item['input']);
if (!$video_id || strlen($video_id) > 15) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'youtube_invalid',
'message' => t('Please provide a valid YouTube URL.'),
);
}
}
}
}
/**
* Implements hook_field_is_empty().
*/
function youtube_field_is_empty($item, $field) {
return empty($item['input']);
}
/**
* Implements hook_field_formatter_info().
*/
function youtube_field_formatter_info() {
$formatters = array(
// This formatter displays your youtube video.
'youtube_video' => array(
'label' => t('YouTube video'),
'field types' => array('youtube'),
'settings' => array(
'youtube_size' => '420x315',
'youtube_width' => NULL,
'youtube_height' => NULL,
'youtube_autoplay' => FALSE,
'youtube_showinfo' => FALSE,
'youtube_controls' => FALSE,
'youtube_autohide' => FALSE,
'youtube_iv_load_policy' => FALSE,
),
),
// This formatter just displays a thumbnail for your video.
'youtube_thumbnail' => array(
'label' => t('YouTube thumbnail'),
'field types' => array('youtube'),
'settings' => array(
'image_style' => 'thumbnail',
'image_link' => '',
),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function youtube_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'youtube_video') {
$element['youtube_size'] = array(
'#type' => 'select',
'#title' => t('YouTube video size'),
'#options' => youtube_size_options(),
'#default_value' => $settings['youtube_size'],
);
$element['youtube_width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#size' => 10,
'#default_value' => $settings['youtube_width'],
'#states' => array(
'visible' => array(
':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][youtube_size]"]' => array('value' => 'custom'),
),
),
);
$element['youtube_height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#size' => 10,
'#default_value' => $settings['youtube_height'],
'#states' => array(
'visible' => array(
':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][youtube_size]"]' => array('value' => 'custom'),
),
),
);
$element['youtube_autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Play video automatically when loaded (autoplay).'),
'#default_value' => $settings['youtube_autoplay'],
);
$element['youtube_showinfo'] = array(
'#type' => 'checkbox',
'#title' => t('Hide video title and uploader info (showinfo).'),
'#default_value' => $settings['youtube_showinfo'],
);
$element['youtube_controls'] = array(
'#type' => 'checkbox',
'#title' => t('Always hide video controls (controls).'),
'#default_value' => $settings['youtube_controls'],
);
$element['youtube_autohide'] = array(
'#type' => 'checkbox',
'#title' => t('Hide video controls after play begins (autohide).'),
'#default_value' => $settings['youtube_autohide'],
);
$element['youtube_iv_load_policy'] = array(
'#type' => 'checkbox',
'#title' => t('Hide video annotations by default (iv_load_policy).'),
'#default_value' => $settings['youtube_iv_load_policy'],
);
}
if ($display['type'] == 'youtube_thumbnail') {
$element['image_style'] = array(
'#type' => 'select',
'#title' => t('Image style'),
'#options' => image_style_options(FALSE),
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
);
// Option to link the thumbnail to its original node, the YouTube video, or
// (if the youtube_colorbox is enabled) a Colorbox modal window.
$element['image_link'] = array(
'#title' => t('Link image to'),
'#type' => 'select',
'#default_value' => $settings['image_link'],
'#empty_option' => t('Nothing'),
'#options' => youtube_thumbnail_link_types(),
);
if (module_exists('youtube_colorbox')) {
// Add Colorbox settings to this form.
youtube_colorbox_thumbnail_field_formatter_settings($element, $instance, $settings, $field['field_name']);
}
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function youtube_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
// Summary for the video style.
if ($display['type'] == 'youtube_video') {
$video_sizes = youtube_size_options();
if (isset($video_sizes[$settings['youtube_size']])) {
$summary = t('YouTube video: @size', array('@size' => $video_sizes[$settings['youtube_size']]));
}
else {
$summary = t('YouTube video: 450px by 315px');
}
$parameters = array(
$settings['youtube_autoplay'],
$settings['youtube_showinfo'],
$settings['youtube_controls'],
$settings['youtube_autohide'],
$settings['youtube_iv_load_policy'],
);
foreach ($parameters as $parameter) {
if ($parameter) {
$summary .= t(', custom parameters');
break;
}
}
return $summary;
}
// Summary for the thumbnail style.
if ($display['type'] == 'youtube_thumbnail') {
$image_styles = image_style_options(FALSE);
// Unset possible 'No defined styles' option.
unset($image_styles['']);
if (isset($image_styles[$settings['image_style']])) {
$summary = t('Image style: @style.', array('@style' => $image_styles[$settings['image_style']]));
}
else {
$summary = t('Original image.');
}
// Display this setting only if image is linked.
$link_types = youtube_thumbnail_link_types();
if (isset($settings['image_link']) && isset($link_types[$settings['image_link']])) {
$summary .= '<br/>' . t('Linked to: ') . $link_types[$settings['image_link']] . '.';
}
return $summary;
}
}
/**
* Implements hook_field_formatter_view().
*/
function youtube_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
// This formatter outputs the youtube embed code.
case 'youtube_video':
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#theme' => 'youtube_video',
'#video_id' => $item['video_id'],
'#entity_title' => $entity->title,
'#size' => array_key_exists('youtube_size', $display['settings']) ? $display['settings']['youtube_size']: NULL,
'#width' => array_key_exists('youtube_width', $display['settings']) ? $display['settings']['youtube_width'] : NULL,
'#height' => array_key_exists('youtube_height', $display['settings']) ? $display['settings']['youtube_height'] : NULL,
'#autoplay' => array_key_exists('youtube_autoplay', $display['settings']) ? $display['settings']['youtube_autoplay'] : FALSE,
'#showinfo' => array_key_exists('youtube_showinfo', $display['settings']) ? $display['settings']['youtube_showinfo'] : FALSE,
'#controls' => array_key_exists('youtube_controls', $display['settings']) ? $display['settings']['youtube_controls'] : FALSE,
'#autohide' => array_key_exists('youtube_autohide', $display['settings']) ? $display['settings']['youtube_autohide'] : FALSE,
'#iv_load_policy' => array_key_exists('youtube_iv_load_policy', $display['settings']) ? $display['settings']['youtube_iv_load_policy'] : FALSE,
);
}
break;
// This formatter uses an imagecache preset to generate a thumbnail.
case 'youtube_thumbnail':
// Check if the formatter involves a link.
if (isset($display['settings']['image_link'])) {
switch ($display['settings']['image_link']) {
case 'content':
$uri = entity_uri($entity_type, $entity);
$uri['options']['html'] = TRUE;
break;
case 'youtube':
$link_youtube = TRUE;
break;
case 'colorbox':
$link_colorbox = TRUE;
break;
}
}
foreach ($items as $delta => $item) {
// If the thumbnail is linked to it's youtube page, take the original url.
if (!empty($link_youtube)) {
$uri = array(
'path' => $item['input'],
'options' => array('html' => TRUE),
);
}
// Add support for the colorbox module.
if (module_exists('youtube_colorbox') && !empty($link_colorbox)) {
// Always open in an iframe for proper origin access.
if (!empty($display['settings']['colorbox']['parameters'])) {
$display['settings']['colorbox']['parameters']['iframe'] = TRUE;
}
$uri = youtube_colorbox_field_item_uri($item, $display['settings']);
}
$element[$delta] = array(
'#theme' => 'youtube_thumbnail',
'#video_id' => $item['video_id'],
'#entity_title' => $entity->title,
'#image_style' => $display['settings']['image_style'],
'#image_link' => isset($uri) ? $uri : '',
);
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_info().
*/
function youtube_field_widget_info() {
return array(
'youtube' => array(
'label' => t('YouTube'),
'field types' => array('youtube'),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function youtube_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value = isset($items[$delta]['input']) ? $items[$delta]['input'] : '';
$element['input'] = $element + array(
'#type' => 'textfield',
'#default_value' => $value,
'#size' => 60,
'#maxlength' => 1024,
'#element_validate' => array('youtube_input_validate'),
);
// Add our own description if one is not provided by the UI.
if ($element['#description'] == '') {
$element['input']['#description'] = t('Enter the YouTube URL. Valid URL
formats include: http://www.youtube.com/watch?v=1SqBdS0XkV4 and
http://youtu.be/1SqBdS0XkV4');
}
if (isset($items[$delta]['video_id'])) {
$element['video_id'] = array(
'#prefix' => '<div class="youtube-video-id">',
'#markup' => t('YouTube video ID: !video_id', array('!video_id' => $items[$delta]['video_id'])),
'#suffix' => '</div>',
'#weight' => 1,
);
}
return $element;
}
/**
* Validation for the youtube field itself.
*/
function youtube_input_validate($element, &$form_state, $form) {
$input = $element['#value'];
$video_id = youtube_get_video_id($input);
if ($video_id) {
$video_id_element = array(
'#parents' => $element['#parents'],
);
array_pop($video_id_element['#parents']);
$video_id_element['#parents'][] = 'video_id';
form_set_value($video_id_element, $video_id, $form_state);
}
}
/**
* Implements hook_field_widget_error().
*/
function youtube_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'youtube_invalid':
form_error($element, $error['message']);
break;
}
}
/**
* Implements of hook_theme().
*/
function youtube_theme($existing, $type, $theme, $path) {
return array(
'youtube_thumbnail' => array(
'variables' => array(
'video_id' => NULL,
'entity_title' => NULL,
'image_style' => NULL,
'image_link' => NULL
),
'file' => 'youtube.theme.inc',
),
'youtube_video' => array(
'variables' => array(
'video_id' => NULL,
'entity_title' => NULL,
'size' => NULL,
'width' => NULL,
'height' => NULL,
'autoplay' => FALSE,
'showinfo' => FALSE,
'controls' => FALSE,
'autohide' => FALSE,
'iv_load_policy' => FALSE,
),
'file' => 'youtube.theme.inc',
),
);
}