-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_filter.module
896 lines (798 loc) · 27.1 KB
/
video_filter.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
<?php
/**
* @file
* Video filter is a highly flexible and easy extendable filter module to embed
* any type of video in your site using a simple tag.
*/
module_load_include('inc', 'video_filter', 'video_filter.codecs');
/**
* Implements hook_menu().
*/
function video_filter_menu() {
$items = array();
$items['video_filter/dashboard/%'] = array(
'title' => 'Videofilter',
'description' => 'Dashboard',
'page callback' => 'video_filter_dashboard_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'page arguments' => array(2),
'theme callback' => 'ajax_base_page_theme',
);
$items['video_filter/instructions'] = array(
'title' => 'Videofilter instructions',
'description' => 'instructions',
'page callback' => 'video_filter_instructions_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'theme callback' => 'ajax_base_page_theme',
);
$items['editor/dialog/video-filter/%filter_format'] = array(
'title' => 'Select video',
'page callback' => 'backdrop_get_form',
'page arguments' => array('video_filter_select_video_form', 3),
'access callback' => 'filter_dialog_access',
'access arguments' => array(3, 'video_filter'),
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
'file' => 'video_filter.pages.inc',
);
return $items;
}
/**
* Implements hook_filter_info().
*/
function video_filter_filter_info() {
$filters = array();
$codecs = array_keys(_video_filter_map_codecs_name(video_filter_get_codec_info()));
$filters['video_filter'] = array(
'title' => t('Video Filter'),
'description' => t('Substitutes [video:URL] with embedded HTML.'),
'process callback' => '_video_filter_process',
'settings callback' => '_video_filter_settings',
'default settings' => array(
'video_filter_width' => '400',
'video_filter_height' => '400',
'video_filter_autoplay' => 1,
'video_filter_related' => 1,
'video_filter_privacy' => 1,
'video_filter_codecs' => array_combine($codecs, $codecs),
'video_filter_multiple_sources' => TRUE,
),
'tips callback' => '_video_filter_tips',
// See http://drupal.org/node/1061244.
'weight' => -1,
);
return $filters;
}
/**
* Filter settings callback
*
* @see video_filter_filter_info().
*/
function _video_filter_settings($form, &$form_state, $filter, $format) {
$settings['video_filter_width'] = array(
'#type' => 'textfield',
'#title' => t('Default width setting'),
'#default_value' => $filter->settings['video_filter_width'],
'#maxlength' => 4,
);
$settings['video_filter_height'] = array(
'#type' => 'textfield',
'#title' => t('Default height setting'),
'#default_value' => $filter->settings['video_filter_height'],
'#maxlength' => 4,
);
$settings['video_filter_autoplay'] = array(
'#type' => 'radios',
'#title' => t('Default autoplay setting'),
'#description' => t('Not all video formats support this setting.'),
'#default_value' => $filter->settings['video_filter_autoplay'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_related'] = array(
'#type' => 'radios',
'#title' => t('Related videos setting'),
'#description' => t('Show "related videos"? Not all video formats support this setting.'),
'#default_value' => $filter->settings['video_filter_related'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_privacy'] = array(
'#type' => 'radios',
'#title' => t('Use privacy-enhanced mode'),
'#description' => t('Prevent video service from setting cookies in the site visitor\'s browser if the codec provides it.'),
'#default_value' => $filter->settings['video_filter_privacy'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_multiple_sources'] = array(
'#type' => 'radios',
'#title' => t('Allow multiple sources'),
'#description' => t('Allow the use of multiple sources (used source is selected at random).'),
'#default_value' => $filter->settings['video_filter_multiple_sources'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_codecs'] = array(
'#type' => 'checkboxes',
'#title' => t('Codecs'),
'#description' => t('Choose which codecs will be available.'),
'#default_value' => $filter->settings['video_filter_codecs'],
'#options' => _video_filter_map_codecs_name(video_filter_get_codec_info()),
);
return $settings;
}
/**
* Implements hook_library_info().
*/
function video_filter_library_info() {
$module_path = backdrop_get_path('module', 'video_filter');
$info = system_get_info('module', 'video_filter');
$libraries['video_filter.ckeditor5.video-filter'] = array(
'title' => 'CKEditor plugin to insert videos.',
'version' => $info['version'],
'js' => array(
$module_path . '/editors/ckeditor5/video-filter.js' => array(),
),
'css' => array(
$module_path . '/css/video_filter.css' => array(),
),
'dependencies' => array(
array('ckeditor5', 'ckeditor5'),
),
);
return $libraries;
}
/**
* Creates a list of codec names, keyed by machine_name.
*
* @param array $codecs
*
* @return array
*/
function _video_filter_map_codecs_name($codecs) {
$codecs_map = array();
foreach ($codecs as $codec_cod => $codec) {
$codecs_map[$codec_cod] = $codec['name'];
}
return $codecs_map;
}
/**
* Filter tips callback
*
* @see video_filter_filter_info().
*/
function _video_filter_tips($filter, $format, $long = FALSE) {
if ($long) {
$codecs = video_filter_get_codec_enabled($filter->settings['video_filter_codecs']);
$supported = array();
$instructions = array();
foreach ($codecs as $codec) {
$supported[] = $codec['name'];
$instructions[] = isset($codec['instructions']) ? '<li>' . $codec['name'] . ':<br/>' . $codec['instructions'] . '</li>' : '';
}
return t('
<p><strong>Video Filter</strong></p>
<p>You may insert videos from popular video sites by using a simple tag <code>[video:URL]</code>.</p>
<p>Examples:</p>
<ul>
<li>Single video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId]</code></li>
<li>Random video out of multiple:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId1,http://www.youtube.com/watch?v=uN1qUeId2]</code></li>
<li>Override default autoplay setting: <code>[video:http://www.youtube.com/watch?v=uN1qUeId autoplay:1]</code></li>
<li>Override default width and height:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId width:X height:Y]</code></li>
<li>Override default aspect ratio:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId ratio:4/3]</code></li>
<li>Align the video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId align:right]</code></li>
</ul>
<p>Supported sites: @codecs.</p>
<p>Special instructions:</p>
<small>Some codecs need special input. You\'ll find those instructions here.</small>
<ul>!instructions</ul>', array(
'@codecs' => implode(', ', $supported),
'!instructions' => implode('', $instructions),
)
);
}
else {
return t('You may insert videos with <code>[video:URL]</code> or with various settings <code>[video:URL autoplay:1 width:X height:Y ratio:4/3 align:right]</code>. See <a href="@url" target="_blank">full instructions</a>.', array('@url' => url('video_filter/instructions')));
}
}
/**
* Filter process callback
*
* @see video_filter_filter_info().
*/
function _video_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
foreach ($matches_code[0] as $ci => $code) {
$video = array(
'source' => $matches_code[2][$ci],
'autoplay' => $filter->settings['video_filter_autoplay'],
'related' => $filter->settings['video_filter_related'],
);
// Pick random out of multiple sources separated by comma (,).
if ($filter->settings['video_filter_multiple_sources'] && strstr($video['source'], ',')) {
$sources = explode(',', $video['source']);
$random = array_rand($sources, 1);
$video['source'] = $sources[$random];
}
// Load all codecs.
$codecs = video_filter_get_codec_enabled($filter->settings['video_filter_codecs']);
// Find codec.
foreach ($codecs as $codec_name => $codec) {
if (!is_array($codec['regexp'])) {
$codec['regexp'] = array($codec['regexp']);
}
// Try different regular expressions.
foreach ($codec['regexp'] as $delta => $regexp) {
if (preg_match($regexp, $video['source'], $matches)) {
$video['codec'] = $codec;
$video['codec']['delta'] = $delta;
$video['codec']['matches'] = $matches;
// Used in theme function:
$video['codec']['codec_name'] = $codec_name;
break 2;
}
}
}
// Codec found.
if (isset($video['codec'])) {
// Override default attributes.
if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
foreach ($matches_attributes[0] as $ai => $attribute) {
$video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
}
}
// Use configured ratio if present, otherwise use that from the codec,
// if set. Fall back to 1.
$ratio = 1;
if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio)) {
// Validate given ratio parameter.
$ratio = $tratio[1] / $tratio[2];
}
elseif (isset($video['codec']['ratio'])) {
if (is_float($video['codec']['ratio']) || is_int($video['codec']['ratio'])) {
$ratio = $video['codec']['ratio'];
}
elseif (preg_match('/(\d+)\s*\/\s*(\d+)/', $video['codec']['ratio'], $cratio)) {
$ratio = $cratio[1] / $cratio[2];
}
}
// Sets video width & height after any user input has been parsed.
// First, check if user has set a width.
if (isset($video['width']) && !isset($video['height'])) {
if ($ratio) {
$video['height'] = ceil($video['width'] / $ratio);
}
else {
$video['height'] = $filter->settings['video_filter_height'];
}
}
// Else, if user has set height.
elseif (isset($video['height']) && !isset($video['width'])) {
if ($ratio) {
$video['width'] = ceil($video['height'] * $ratio);
}
else {
$video['width'] = $filter->settings['video_filter_height'];
}
}
// Maybe both?
elseif (isset($video['height']) && isset($video['width'])) {
$video['width'] = $video['width'];
$video['height'] = $video['height'];
}
// Fall back to defaults.
elseif (!isset($video['height']) && !isset($video['width'])) {
$video['width'] = $filter->settings['video_filter_width'] != '' ? $filter->settings['video_filter_width'] : 400;
$video['height'] = $filter->settings['video_filter_height'] != '' ? $filter->settings['video_filter_height'] : 400;
}
// Default value for control bar height.
$control_bar_height = 0;
if (isset($video['control_bar_height'])) {
// Respect control_bar_height option if present.
$control_bar_height = $video['control_bar_height'];
}
elseif (isset($video['codec']['control_bar_height'])) {
// Respect setting provided by codec otherwise.
$control_bar_height = $video['codec']['control_bar_height'];
}
// Ensure that width and height are integers. e.g. 600px should be 600.
$video['height'] = intval($video['height']);
$video['width'] = intval($video['width']);
$video['height'] += $control_bar_height;
// Resize to fit within width and height respecting aspect ratio.
if ($ratio) {
$scale_factor = min(array(
($video['height'] - $control_bar_height),
$video['width'] / $ratio,
));
$video['height'] = round($scale_factor + $control_bar_height);
$video['width'] = round($scale_factor * $ratio);
}
$video['autoplay'] = (bool) $video['autoplay'];
$video['align'] = (isset($video['align']) && in_array($video['align'], array(
'left',
'right',
'center',
))) ? $video['align'] : NULL;
// Privacy setting
$video['privacy'] = $filter->settings['video_filter_privacy'];
// Let modules have final say on video parameters.
backdrop_alter('video_filter_video', $video);
if (isset($video['codec']['html5_callback']) && is_callable($video['codec']['html5_callback'], FALSE)) {
$replacement = call_user_func($video['codec']['html5_callback'], $video);
}
elseif (is_callable($video['codec']['callback'], FALSE)) {
$replacement = call_user_func($video['codec']['callback'], $video);
}
else {
// Invalid callback.
$replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $video['codec']['callback'] . ' -->';
}
}
// Invalid format.
else {
$replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
}
$text = str_replace($code, $replacement, $text);
}
}
return $text;
}
/**
* Wrapper that calls the theme function.
*/
function video_filter_iframe($video) {
return theme('video_filter_iframe', array('video' => $video));
}
/**
* Get a list of all available video codecs.
*/
function video_filter_get_codec_info() {
static $codecs;
if (!isset($codecs)) {
$codecs = module_invoke_all('codec_info');
backdrop_alter('video_filter_codec_info', $codecs);
}
return $codecs;
}
/**
* Create a list of codecs across all filters.
*
* @param array $filters_codecs
*
* @return array
*/
function _video_filter_merge_format_codecs($filters_codecs) {
$codecs = array_pop($filters_codecs);
foreach ($filters_codecs as $format_name => $format_codecs) {
foreach ($format_codecs as $codec_name => $codec_value) {
if (!empty($codec_value) && empty($codecs[$codec_name])) {
$codecs[$codec_name] = $codec_value;
}
}
}
return $codecs;
}
/**
* Get a list of enabled video codecs.
*/
function video_filter_get_codec_enabled($video_filter_codecs) {
$codecs = array_intersect_key(
video_filter_get_codec_info(),
array_filter($video_filter_codecs)
);
return $codecs;
}
/**
* Function that outputs HTML5 compatible iFrame for codecs that support it.
*
* @ingroup themeable
*/
function theme_video_filter_iframe($variables) {
$video = $variables['video'];
$classes = video_filter_get_classes($video);
$attributes = '';
if (!empty($video['attributes'])) {
$attributes = backdrop_attributes($video['attributes']);
}
$output = '<div class="video-filter"><iframe src="' . $video['source'] . '" width="' . $video['width'] . '" height="' . $video['height'] . '" class="' . implode(' ', $classes) . '" allowfullscreen="true"' . $attributes . '></iframe></div>';
return $output;
}
/**
* Implements hook_theme().
*/
function video_filter_theme($existing, $type, $theme, $path) {
return array(
'video_filter_iframe' => array(
'variables' => array('video' => NULL, 'params' => NULL),
),
'video_filter_dashboard' => array(
'variables' => array('form' => NULL),
'template' => 'video_filter_dashboard',
),
);
}
/**
* Helper function that extracts some classes from $video.
*/
function video_filter_get_classes($video) {
$classes = array(
'video-filter',
// Add codec name.
'video-' . $video['codec']['codec_name'],
);
// Add alignment.
if (isset($video['align'])) {
$classes[] = 'video-' . $video['align'];
}
// First match is the URL, we don't want that as a class.
unset($video['codec']['matches'][0]);
foreach ($video['codec']['matches'] as $match) {
$classes[] = 'vf-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $match));
}
return $classes;
}
/**
* Template preprocess function for video_filter_dashboard().
*/
function template_preprocess_video_filter_dashboard(&$variables) {
// Construct page title.
$variables['head_title'] = t('Video filter dashboard');
$variables['head'] = backdrop_get_html_head();
$variables['language'] = $GLOBALS['language'];
$variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
$variables['messages'] = isset($variables['show_messages']) ? theme('status_messages') : '';
$variables['css'] = backdrop_add_css();
$variables['styles'] = backdrop_get_css();
$variables['scripts'] = backdrop_get_js();
}
/**
* Creates the dashboard.
*/
function video_filter_dashboard_page($editor) {
module_invoke('admin_bar', 'suppress');
// Add CSS.
backdrop_add_css(backdrop_get_path('module', 'video_filter') . '/css/video_filter.css');
switch ($editor) {
case 'ckeditor':
// Add JavaScript.
backdrop_add_js(backdrop_get_path('module', 'video_filter') . '/editors/ckeditor/video_filter_dialog.js');
break;
}
$form = backdrop_get_form('_video_filter_form');
print theme('video_filter_dashboard', array('form' => render($form)));
exit();
}
/**
* Creates the instructions page.
*/
function video_filter_instructions_page() {
module_invoke('admin_bar', 'suppress');
$form = backdrop_get_form('_video_filter_instructions_form');
print theme('video_filter_dashboard', array('form' => render($form)));
exit();
}
/**
* Dialog form
*/
function _video_filter_form() {
$form['video_filter'] = array(
'#type' => 'fieldset',
'#title' => t('Insert Video'),
'#weight' => 0,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#attributes' => array('class' => array('clearfix')),
);
$form['video_filter']['file_url'] = array(
'#type' => 'textfield',
'#title' => t('Video URL'),
'#maxlength' => 255,
'#size' => 80,
'#default_value' => '',
'#weight' => 1,
);
$form['video_filter']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#maxlength' => 255,
'#size' => 80,
'#default_value' => '',
'#weight' => 2,
);
$form['video_filter']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#maxlength' => 255,
'#size' => 80,
'#default_value' => '',
'#weight' => 3,
);
$form['video_filter']['align'] = array(
'#type' => 'select',
'#title' => t('Align'),
'#default_value' => 'none',
'#options' => array(
'none' => t('None'),
'left' => t('Left'),
'right' => t('Right'),
'center' => t('Center'),
),
'#weight' => 4,
);
$form['video_filter']['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#weight' => 5,
);
$form += _video_filter_instructions_form();
$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#weight' => 98,
);
$form['insert'] = array(
'#type' => 'button',
'#value' => t('Insert'),
'#weight' => 99,
);
return $form;
}
function _video_filter_instructions_form() {
$form['instructions'] = array(
'#type' => 'fieldset',
'#title' => t('Instructions'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array('class' => array('clearfix')),
'#weight' => 97,
);
$text = '<p>' . t('Insert a 3rd party video from one of the following providers; this list may vary depending on the text format being used.') . '</p>';
$text .= _video_filter_instructions();
$form['instructions']['text'] = array(
'#type' => 'item',
'#markup' => $text,
);
return $form;
}
/**
* Implements hook_ckeditor_plugins().
*/
function video_filter_ckeditor_plugins() {
$image_prefix = backdrop_get_path('module', 'video_filter') . '/editors/ckeditor/';
$plugins['video_filter'] = array(
'label' => t('Video filter'),
'path' => backdrop_get_path('module', 'video_filter') . '/editors/ckeditor',
'file' => 'plugin.js',
'enabled callback' => 'video_filter_ckeditor_video_filter_plugin_check',
'buttons' => array(
'video_filter' => array(
'label' => t('Video filter'),
'image' => $image_prefix . 'video_filter.png',
),
),
);
return $plugins;
}
/**
* Enabled callback for hook_ckeditor_plugins().
*
* Checks if the Video Filter plugin should be enabled based on the configuration of
* a text format and editor.
*
* @param $format
* The filter format object for which CKEditor is checking settings.
*
* @return bool
* TRUE if the video filter plugin is enabled, FALSE otherwise.
*/
function video_filter_ckeditor_video_filter_plugin_check($format, $plugin_name) {
// Automatically enable this plugin if the Video Filter button is enabled.
foreach ($format->editor_settings['toolbar'] as $row) {
foreach ($row as $button_group) {
if (in_array('video_filter', $button_group['items'])) {
return TRUE;
}
}
}
return FALSE;
}
/**
* Implements hook_tinymce_external_plugins().
*/
function video_filter_tinymce_external_plugins($format) {
if (module_exists('tinymce_video_filter')) {
return;
}
$module_url = base_path() . backdrop_get_path('module', 'video_filter');
$plugins = array(
'video_filter' => array(
'plugin_path' => $module_url . '/editors/tinymce/plugin.js',
'buttons' => array(
'video_filter' => array(
'icon' => 'embed',
'tooltip' => t('Add video'),
),
),
'variables' => array(
'videoFilterInstructionUrl' => url('video_filter/instructions'),
'videoFilterTooltip' => t('Add video'),
),
),
);
return $plugins;
}
/**
* Implements hook_ckeditor5_plugins().
*
* Return a list of all plugins provided by this module.
*/
function video_filter_ckeditor5_plugins() {
$image_prefix = backdrop_get_path('module', 'video_filter') . '/editors/ckeditor5/icons';
$plugins['videoFilter.VideoFilter'] = array(
'library' => array('video_filter', 'video_filter.ckeditor5.video-filter'),
'css' => array(backdrop_get_path('module', 'video_filter') . '/css/video_filter.css'),
'enabled_callback' => 'video_filter_ckeditor5_video_filter_plugin_check',
'buttons' => array(
'videoFilter' => array(
'label' => t('Video filter'),
'image' => $image_prefix . '/video.svg',
'plugins' => array('videoFilter.VideoFilter'),
),
),
);
return $plugins;
}
/**
* Implements hook_ckeditor5_upgrade_button_mapping().
*/
function video_filter_ckeditor5_upgrade_button_mapping() {
return array(
'video_filter' => 'videoFilter',
);
}
/**
* Enabled callback for hook_ckeditor5_plugins().
*/
function video_filter_ckeditor5_video_filter_plugin_check($format, $plugin_name) {
return !empty($format->filters['video_filter']->status);
}
/**
* Implements hook_ckeditor5_config_alter().
*/
function video_filter_ckeditor5_config_alter(array &$config, $format) {
// Create a token for access to dialogs.
if (in_array('videoFilter.VideoFilter', $config['pluginList'])) {
$token = filter_editor_dialog_token($format, 'video_filter');
$config['video_filter']['dialogUrl'] = url('editor/dialog/video-filter/' . $format->format, array('query' => array(
'token' => $token,
'calling_path' => $_GET['q'],
)));
}
}
/**
* Implements hook_element_info_alter().
*/
function video_filter_element_info_alter(&$types) {
if (isset($types['text_format']) && is_array($types['text_format'])) {
// @todo use Backdrop.ckeditor.openDialog() instead so no need to override styles below.
backdrop_add_css(backdrop_get_path('module', 'video_filter') . '/css/dialog.css');
_video_filter_add_settings('ckeditor');
}
}
function _video_filter_add_settings($editor) {
static $editor_settings_added = array();
static $global_settings_added = FALSE;
if (!isset($editor_settings_added[$editor])) {
$editor_settings_added[$editor] = TRUE;
// Add popup url.
$settings = array(
'video_filter' => array(
'url' => array($editor => url('video_filter/dashboard/' . $editor)),
'instructions_url' => url('video_filter/instructions'),
),
);
backdrop_add_js($settings, 'setting');
}
if (!$global_settings_added) {
$global_settings_added = TRUE;
// Add global settings for video_filter.
$settings = array(
'video_filter' => array(
'modulepath' => backdrop_get_path('module', 'video_filter'),
),
);
backdrop_add_js($settings, 'setting');
}
}
/**
* Parses Codec into instructions for WYSIWYG popup.
*/
function _video_filter_instructions() {
// Get all codecs the user has permission to use in at least one text format.
global $user;
$formats = filter_formats($user);
$filters_codecs = array();
foreach ($formats as $format) {
if (isset($format->filters['video_filter']) && $format->filters['video_filter']->status) {
$filters_codecs[$format->name] = $format->filters['video_filter']->settings['video_filter_codecs'];
}
}
$video_filter_codecs = _video_filter_merge_format_codecs($filters_codecs);
$codecs = video_filter_get_codec_enabled($video_filter_codecs);
$output = '<ul>';
foreach ($codecs as $codec) {
$output .= '<li><strong>' . $codec['name'] . '</strong><br />' . t('e.g.') . ' <code>' . $codec['sample_url'] . '</code>';
if (!empty($codec['instructions'])) {
$output .= '<br />' . $codec['instructions'];
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
/**
* Build the token to insert.
*
* @param string $file_url
* @param array $attributes
* Optional attributes.
*
* @return string
*/
function _video_filter_build_token($file_url, $attributes) {
$token = '[video:' . $file_url;
if (!empty($attributes['width'])) {
$token .= ' width:' . $attributes['width'];
}
if (!empty($attributes['height'])) {
$token .= ' height:' . $attributes['height'];
}
if ($attributes['align'] != 'none') {
$token .= ' align:' . $attributes['align'];
}
if (!empty($attributes['autoplay'])) {
$token .= ' autoplay:' . $attributes['autoplay'];
}
$token .= ']';
return $token;
}
/**
* Requests data from an oEmbed provider.
*
* Note: This function currently only supports JSON responses.
*
* @param string $endpoint
* The provider endpoint URL.
* @param array $arguments
* An associative array of URL arguments to send the provider.
*
* @return array|FALSE
* An array of data if the request is successful, otherwise FALSE.
*
* @todo Support other response formats than JSON?
*/
function video_filter_oembed_request($endpoint, array $arguments) {
// Make HTTP request.
$result = backdrop_http_request(url($endpoint, array('query' => $arguments)));
if ($data = json_decode($result->data)) {
// Success.
return (array) $data;
}
else {
// Failure. Either the resource doesn't exist or there was an error with the
// request.
return FALSE;
}
}