-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvideo_embed_field.module
775 lines (690 loc) · 21 KB
/
video_embed_field.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
<?php
/**
* @file
* Provides a simple field for easily embedding videos from youtube or vimeo
*
* This module is not intended to replace media or video - it does not allow for
* any local storage of videos, custom players or anything else.
* It simply allows users to embed videos from youtube and vimeo - and provides
* hooks to allow other modules to provide more providers.
*
*/
// Load all Field module hooks.
module_load_include('inc', 'video_embed_field', 'video_embed_field.field');
// Load the admin forms.
module_load_include('inc', 'video_embed_field', 'video_embed_field.admin');
// Load our default handlers.
module_load_include('inc', 'video_embed_field', 'video_embed_field.handlers');
// Load feeds mapping hooks.
module_load_include('inc', 'video_embed_field', 'video_embed_field.feeds');
/**
* Implements hook_default_video_styles().
*/
function video_embed_field_default_video_embed_styles() {
$styles = array();
$handlers = video_embed_get_handlers();
// Create the normal handler.
$normal = new stdClass();
$normal->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */
$normal->api_version = 1;
$normal->name = 'normal';
$normal->title = 'Normal';
$normal->data = array();
$teaser = new stdClass();
$teaser->disabled = FALSE; /* Edit this to true to make a default video_embed_style disabled initially */
$teaser->api_version = 1;
$teaser->name = 'teaser';
$teaser->title = 'Teaser';
$teaser->data = array();
// Add in our settings for each of the handlers.
foreach ($handlers as $name => $handler) {
$normal->data[$name] = $handler['defaults'];
$teaser->data[$name] = $handler['defaults'];
$teaser->data[$name]['width'] = 480;
$teaser->data[$name]['height'] = 270;
}
return array($normal, $teaser);
}
/**
* Implements hook_menu().
*/
function video_embed_field_menu() {
$items = array();
$items['vef/load/%'] = array(
'title' => 'Video Embed Field - Load Video',
'page callback' => '_video_embed_field_show_video',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/config/media/vef/settings'] = array(
'title' => 'Settings',
'description' => 'Video Embed Field settings',
'page callback' => 'backdrop_get_form',
'page arguments' => array('video_embed_field_settings_form'),
'file' => 'video_embed_field.admin.inc',
'access arguments' => array('administer video styles'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function video_embed_field_permission() {
return array(
'administer video styles' => array(
'title' => t('Administer video styles'),
'description' => t('Create and modify styles for embedded videos.'),
),
);
}
/**
* Implements hook_theme().
*/
function video_embed_field_theme() {
return array(
// Theme functions in video_embed_field.admin.inc.
'video_embed_field_video_style_list' => array(
'variables' => array('styles' => NULL),
),
'video_embed_field_embed_code' => array(
'template' => 'video-embed-field-embed-code',
'variables' => array(
'url' => NULL,
'style' => 'normal',
'video_data' => array(),
),
),
'video_embed_field_colorbox_code' => array(
'variables' => array(
'image_url' => NULL,
'image_style' => 'normal',
'image_alt' => NULL,
'video_url' => NULL,
'video_style' => NULL,
'video_data' => array(),
),
),
);
}
/**
* Implements hook_views_api().
*/
function video_embed_field_views_api() {
return array(
'api' => 3,
'path' => backdrop_get_path('module', 'video_embed_field') . '/views',
);
}
/**
* Get an array of all styles and their settings.
*
* @return array
* An array of styles keyed by the video style name (name).
*
* @see video_embed_field_video_style_load()
*/
function video_embed_field_video_styles() {
$styles = &backdrop_static(__FUNCTION__);
// Grab from cache or build the array.
if (!isset($styles)) {
$filenames = config_get_names_with_prefix('video_embed_field.video_style');
foreach ($filenames as $filename) {
$config = config($filename);
$name = $config->get('machine_name');
$preset = $config->get();
$styles[$name] = (object) $preset;
}
}
return $styles;
}
/**
* Load a style by style name. May be used as a loader for menu items.
*
* @param string $name
* The name of the style.
*
* @return array
* An video style array containing the following keys:
* - "name": The unique video style ID.
* - "title": The human readable video style name.
* - "data": An array of video settings within this video style.
* If the video style name or ID is not valid, an empty array is returned.
*/
function video_embed_field_video_style_load($name) {
$styles = video_embed_field_video_styles();
return isset($styles[$name]) ? $styles[$name] : FALSE;
}
/**
* Creates a hook that other modules can implement to get handlers.
*
* Can be used to add more handlers if needed - from other modules and such.
*
* @see hook_video_embed_handler_info
* @see video_embed_field.api.php
*/
function video_embed_get_handlers() {
$handlers = &backdrop_static(__FUNCTION__);
if (!isset($handlers)) {
if ($handlers = cache_get('video_embed_field_handlers')) {
$handlers = $handlers->data;
}
else {
$handlers = module_invoke_all('video_embed_handler_info');
backdrop_alter('video_embed_handler_info', $handlers);
cache_set('video_embed_field_handlers', $handlers);
}
}
return $handlers;
}
/**
* Retrieves the video handler for a video URL.
*
* @param string $url
* The video URL.
*
* @return string|bool
* The handler name for the URL, FALSE in case there is no handler.
*/
function video_embed_get_handler($url) {
// Process video URL.
if (!stristr($url, 'http://') && !stristr($url, 'https://')) {
$protocol = backdrop_is_https() ? 'https://' : 'http://';
$url = $protocol . $url;
}
$parts = parse_url($url);
if (!isset($parts['host'])) {
return FALSE;
}
$host = $parts['host'];
if (stripos($host, 'www.') > -1) {
$host = substr($host, 4);
}
$domains = _video_embed_field_get_provider_domains();
$handlers = video_embed_get_handlers();
if (isset($domains[$host])) {
$handler_name = $domains[$host];
$handler = $handlers[$handler_name];
$handler['name'] = $handler_name;
return $handler;
}
else {
return FALSE;
}
}
/**
* Creates a form from the player configuration options.
*
* @param array $defaults
* The default settings for the various fields.
*
* @return array
* The configuration form array.
*/
function video_embed_field_get_form($defaults) {
$form = array();
$handlers = video_embed_get_handlers();
foreach ($handlers as $name => $handler) {
if (isset($handler['form']) && function_exists($handler['form'])) {
$handler_defaults = isset($defaults[$name]) ? $defaults[$name] : array();
$handler_defaults = array_merge($handler['defaults'], $handler_defaults);
$form[$name] = call_user_func($handler['form'], $handler_defaults);
$form[$name] += array(
'#type' => 'fieldset',
'#title' => t('@provider settings', array('@provider' => $handler['title'])),
'#tree' => TRUE,
'#element_validate' => isset($handler['form_validate']) ? array($handler['form_validate']) : array(),
);
}
}
return $form;
}
/**
* Validates the iframe CSS dimensions.
*
* @param array $element
* The element to validate.
*/
function video_embed_field_validate_dimensions($element) {
if (!preg_match('/^(\d*)(px|%)?$/', $element['width']['#value'], $results)) {
form_error($element['width'], t('You should use a valid CSS value for width in @plugin plugin', array('@plugin' => $element['#title'])));
}
if (!preg_match('/^(\d*)(px|%)?$/', $element['height']['#value'], $results)) {
form_error($element['height'], t('You should use a valid CSS value for height in @plugin plugin', array('@plugin' => $element['#title'])));
}
}
/**
* Get an array of image styles suitable for using as select list options.
*
* @param bool $include_empty
* If TRUE a <none> option will be inserted in the options array.
*
* @return array
* Array of image styles both key and value are set to style name.
*/
function video_embed_field_video_style_options($include_empty = TRUE) {
$styles = video_embed_field_video_styles();
$options = array();
if (!empty($styles)) {
if ($include_empty) {
$options[''] = t('<none>');
}
foreach ($styles as $style) {
$options[$style->machine_name] = $style->title;
}
}
else {
$options[''] = t('No defined styles');
}
return $options;
}
/**
* Implements hook_filter_info().
*/
function video_embed_field_filter_info() {
$filters['video_embed_field'] = array(
'title' => t('Video Embedding'),
'description' => t('Replaces [VIDEO::https://www.youtube.com/watch?v=someVideoID::aVideoStyle] tags with embedded videos.'),
'process callback' => 'video_embed_field_filter_process',
'tips callback' => '_filter_video_embed_tips',
);
return $filters;
}
/**
* Implements callback_filter_tips().
*
* Provides help for the URL filter.
*
* @see filter_filter_info()
*/
function _filter_video_embed_tips($filter, $format, $long = FALSE) {
return t('Replaces [VIDEO::https://www.youtube.com/watch?v=someVideoID::aVideoStyle] tags with embedded videos.');
}
/**
* Video Embed Field filter process callback.
*/
function video_embed_field_filter_process($text, $filter, $format) {
preg_match_all('/ \[VIDEO:: ( [^\[\]]+ )* \] /x', $text, $matches);
$tag_match = (array) array_unique($matches[1]);
foreach ($tag_match as $tag) {
$parts = explode('::', $tag);
// Get video style.
if (isset($parts[1])) {
$style = $parts[1];
}
else {
$style = 'normal';
}
$embed_code = theme('video_embed_field_embed_code', array('url' => $parts[0], 'style' => $style));
$text = str_replace('[VIDEO::' . $tag . ']', $embed_code, $text);
}
return $text;
}
/**
* Processes variables to format a video player.
*
* @param array $variables
* Contains the following information:
* - $url
* - $style
* - $video_data.
*
* @see video-embed.tpl.php
*/
function template_preprocess_video_embed_field_embed_code(&$variables) {
$variables['embed_code'] = '';
if (!empty($variables['url']) && $handler = video_embed_get_handler($variables['url'])) {
$variables['handler'] = $handler['name'];
// Load the style.
$style = video_embed_field_video_style_load($variables['style']);
// If there was an issue load in the default style.
if ($style == FALSE) {
$style = video_embed_field_video_style_load('normal');
}
if (isset($style->data[$variables['handler']])) {
$variables['style_settings'] = $style->data[$variables['handler']];
}
// Safety value for when we add new handlers and there are styles stored.
elseif (isset($handler['defaults'])) {
$variables['style_settings'] = $handler['defaults'];
}
// Prepare the URL.
if (!stristr($variables['url'], 'http://') && !stristr($variables['url'], 'https://')) {
$protocol = backdrop_is_https() ? 'https://' : 'http://';
$variables['url'] = $protocol . $variables['url'];
}
// Prepare embed code.
if ($handler && isset($handler['function']) && function_exists($handler['function'])) {
$embed_code = call_user_func($handler['function'], $variables['url'], $variables['style_settings']);
$variables['embed_code'] = backdrop_render($embed_code);
}
else {
$variables['embed_code'] = l($variables['url'], $variables['url']);
}
// Prepare video data.
$variables['data'] = $variables['video_data'];
unset($variables['video_data']);
}
}
/**
* Returns image style image with a link to an embedded video in colorbox.
*
* @param array $variables
* An associative array containing:
* - image_url: The image URL.
* - image_style: The image style to use.
* - image_alt: The image ALT attribute.
* - video_url: The video URL.
* - video_style: The video style to use.
* - video_data: An array of data about the video.
*
* @return string
* The themed output.
*
* @ingroup themeable
*/
function theme_video_embed_field_colorbox_code($variables) {
$path = video_embed_field_get_ajax_url($variables['video_url'], $variables['video_style']);
if (!empty($variables['image_alt'])) {
// If description is enabled (=!empty), pass it to Colorbox.
$path['options']['attributes']['title'] = $variables['image_alt'];
}
$image = array(
'#theme' => 'image_formatter',
'#item' => array('uri' => $variables['image_url'], 'alt' => $variables['image_alt']),
'#image_style' => $variables['image_style'],
'#path' => $path,
);
return backdrop_render($image);
}
/**
* Gets the thumbnail url for a given video url.
*
* @param string $url
* The url of the video.
*
* @return string
* String representing the url of the thumbnail, or FALSE on error.
*/
function video_embed_field_thumbnail_url($url) {
$info = FALSE;
if ($handler = video_embed_get_handler($url)) {
if (isset($handler['thumbnail_function']) && function_exists($handler['thumbnail_function'])) {
$info = call_user_func($handler['thumbnail_function'], $url);
$info['handler'] = $handler['name'];
}
if (empty($info['url']) && isset($handler['thumbnail_default']) && file_exists($handler['thumbnail_default'])) {
$info = array(
'handler' => $handler['name'],
'id' => 'default_thumbnail',
'url' => $handler['thumbnail_default'],
);
}
}
return $info;
}
/**
* Gets a video data array for a given video url.
*
* @param string $url
* A video URL of the data array you want returned.
*
* @return array|false
* An array of video data, or FALSE on error.
*/
function video_embed_field_get_video_data($url) {
$handler = video_embed_get_handler($url);
$data = FALSE;
if ($handler && isset($handler['name'], $handler['data_function']) && function_exists($handler['data_function'])) {
$data = call_user_func($handler['data_function'], $url);
if ($data && is_array($data)) {
$data['handler'] = $handler['name'];
}
}
return $data;
}
/**
* Generates the AJAX path array from the video URL and the video_style.
*
* @param string $video_url
* The URL to the video.
* @param string $video_style
* The video style to render the video.
*
* @return array
* The AJAX path array.
*/
function video_embed_field_get_ajax_url($video_url, $video_style) {
$style = video_embed_field_video_style_load($video_style);
// If there was an issue load in the default style.
if ($style == FALSE) {
$style = video_embed_field_video_style_load('normal');
}
$handler = video_embed_get_handler($video_url);
$data = $style->data[$handler['name']];
// Write values for later AJAX load.
$hash = _video_embed_field_store_video($video_url, $video_style);
return array(
'path' => 'vef/load/' . $hash,
'options' => array(
'attributes' => array(
'class' => array(
'colorbox-load',
),
),
'query' => array(
'width' => $data['width'],
'height' => $data['height'] + 5,
'iframe' => TRUE,
),
),
);
}
/**
* Fetches all available provider domains.
*
* @return array
* An array containing the allowed video domains.
*/
function _video_embed_field_get_provider_domains() {
$domains = array();
$handlers = video_embed_get_handlers();
foreach ($handlers as $name => $handler) {
if (isset($handler['function']) && function_exists($handler['function'])) {
foreach ($handler['domains'] as $domain) {
$domains[$domain] = $name;
}
}
}
return $domains;
}
/**
* Fetches all available provider domains for certain field instance.
*
* @param array $instance
* The instance definition.
*
* @return array
* An array containing the allowed video domains.
*/
function _video_embed_field_get_instance_provider_domains($instance) {
return array_intersect(_video_embed_field_get_provider_domains(), $instance['settings']['allowed_providers']);
}
/**
* Fetches settings string.
*
* @param array $settings
* The settings array.
*
* @return string
* The settings string generated from the settings array.
*/
function _video_embed_code_get_settings_str($settings = array()) {
$values = array();
foreach ($settings as $name => $value) {
if (!isset($value)) {
$values[] = $name;
}
else {
$values[] = $name . '=' . $value;
}
}
return implode('&', $values);
}
/**
* Stores a video to be loaded later from an _video_embed_field_load_video.
*
* @param string $video_url
* The video URL.
* @param string $video_style
* The video style.
*
* @return string
* The hash generated for the video URL and the given style.
*/
function _video_embed_field_store_video($video_url, $video_style) {
// Create a hash key.
$hash = _video_embed_field_hash($video_url, $video_style);
// Check that this record doesn't already exist before saving it.
if (!_video_embed_field_load_video($hash)) {
$record = array(
'vhash' => $hash,
'video_url' => $video_url,
'video_style' => $video_style,
);
cache_set('vef-store-' . $hash, $record);
// Add it to our static cache so we won't have to go to the database.
$static_cache = &backdrop_static('vef_video_store', array());
$static_cache[$hash] = $record;
}
return $hash;
}
/**
* Renders a video for an AJAX call.
*
* @param string $hash
* The video hash.
*
* @return
* Do not return because this prints an AJAX output.
*/
function _video_embed_field_show_video($hash) {
$data = _video_embed_field_load_video($hash);
if ($data) {
$video = array(
'#theme' => 'video_embed_field_embed_code',
'#style' => $data['video_style'],
'#url' => $data['video_url'],
);
print backdrop_render($video);
}
else {
$message = 'Video unable to load data from hash %hash';
$substitutions = array('%hash' => $hash);
watchdog('video_embed_field', $message, $substitutions, WATCHDOG_WARNING);
}
backdrop_exit();
}
/**
* Loads a video from the video store given its hash.
*
* @param string $hash
* The video hash.
*
* @return array|bool
* An array with video definition, FALSE if the hash does not exist.
*/
function _video_embed_field_load_video($hash) {
$static_cache = &backdrop_static('vef_video_store', array());
// Check if we've already loaded it.
if (isset($static_cache[$hash])) {
return $static_cache[$hash];
}
else {
$result = cache_get('vef-store-' . $hash);
if ($result) {
// Cache it before returning.
$data = $result->data;
$static_cache[$hash] = $data;
return $data;
}
else {
return FALSE;
}
}
}
/**
* Creates a hash for storing or looking up a video in the store table.
*
* @param string $video_url
* The video URL.
* @param string $video_style
* The video style.
*
* @return string
* The hash generated for the video URL and the given style.
*/
function _video_embed_field_hash($video_url, $video_style) {
return md5('vef' . $video_url . $video_style);
}
/**
* Implements hook_autoload_info().
*/
function video_embed_field_autoload_info() {
return array(
'views_embed_field_views_handler_field_thumbnail_path' => 'views/handlers/views_embed_field_views_handler_field_thumbnail_path.inc',
);
}
/**
* Implements hook_preset_types().
*/
function video_embed_field_preset_types() {
return array(
'video_style' => array(
'name' => 'Video style',
'name_plural' => 'Video styles',
'path' => 'admin/config/media/vef',
'path_title' => 'Video Embed Field',
'path_description' => 'Administer Video Embed Field\'s video styles.',
'id_name' => 'Style',
'permission' => 'administer video styles',
),
);
}
/**
* Implements hook_preset_form().
*/
function video_embed_field_preset_form($preset_type, $id) {
$form = array();
if ($preset_type == 'video_style') {
// Load current style.
$config = config('video_embed_field.' . $preset_type . '.' . $id);
$style = $config->get();
// Grab the settings off the parser form.
$values = isset($style['data']) ? $style['data'] : array();
$parser_form = video_embed_field_get_form($values);
// General settings for playback - formerly in the configuration section.
$form['data'] = array(
'#type' => 'vertical_tabs',
'#title' => t('Playback settings'),
'#tree' => TRUE,
) + $parser_form;
}
return $form;
}
/**
* Implements hook_config_info().
*/
function video_embed_field_config_info() {
$prefixes['video_embed_field.settings'] = array(
'label' => t('Video Embed Field settings'),
'group' => t('Configuration'),
);
$prefixes['video_embed_field.video_style'] = array(
'name_key' => 'machine_name',
'label_key' => 'title',
'group' => t('Video styles'),
);
return $prefixes;
}