-
Notifications
You must be signed in to change notification settings - Fork 2
/
varbase_media.module
728 lines (611 loc) · 27.2 KB
/
varbase_media.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
<?php
/**
* @file
* Contains varbase_media.module.
*/
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\entity_browser_generic_embed\OverrideHelper;
use Drupal\varbase_media\Plugin\media\Source\VarbaseMediaRemoteVideo;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Url;
use Drupal\media\OEmbed\Provider;
use Drupal\field\Entity\FieldConfig;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
// Include all helpers.
include_once __DIR__ . '/includes/helpers.inc';
// Include Varbase Media global tokens.
include_once __DIR__ . '/varbase_media.tokens.inc';
/**
* Implements hook_preprocess_field().
*/
function varbase_media_preprocess_field(&$variables) {
if ($variables['element']['#formatter'] == 'varbase_oembed') {
// Provide an extra variable to the field template when the field uses
// a formatter of type 'varbase_oembed'.
$iframe_url_helper = \Drupal::service('media.oembed.iframe_url_helper');
$entity = $variables['element']['#object'];
$view_mode = $variables['element']['#view_mode'];
$field_name = $variables['element']['#field_name'];
$bundle = $variables['element']['#bundle'];
// Get the field formatter settings...
$entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
$field_display = $entity_display->getComponent($field_name);
if ($bundle == "remote_video") {
$max_width = $field_display['settings']['max_width'];
$max_height = $field_display['settings']['max_height'];
$item = $variables['element']["#items"]->first();
$main_property = $item->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName();
$value = $item->{$main_property};
$provider = $entity->field_provider->value;
$url = Url::fromRoute('media.oembed_iframe', [], [
'query' => [
'url' => $value,
'max_width' => $max_width,
'max_height' => $max_height,
'type' => "remote_video",
'provider' => strtolower($provider ?? ''),
'view_mode' => $view_mode,
'hash' => $iframe_url_helper->getHash($value, $max_width, $max_height, $provider, $view_mode),
],
]);
$variables['items'][0]['content']['#attributes']['src'] = $url->toString();
}
}
elseif ($variables['element']['#formatter'] == 'oembed') {
// Fallback option for oembed old way, In case of change back to oembed.
// -------
// Provide an extra variable to the field template when the field uses
// a formatter of type 'oembed'.
$resource_fetcher = \Drupal::service('media.oembed.resource_fetcher');
$url_resolver = \Drupal::service('media.oembed.url_resolver');
$iframe_url_helper = \Drupal::service('media.oembed.iframe_url_helper');
$entity = $variables['element']['#object'];
$view_mode = $variables['element']['#view_mode'];
$field_name = $variables['element']['#field_name'];
$bundle = $variables['element']['#bundle'];
// Get the field formatter settings...
$entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
$field_display = $entity_display->getComponent($field_name);
if ($bundle == "remote_video") {
$max_width = $field_display['settings']['max_width'];
$max_height = $field_display['settings']['max_height'];
$item = $variables['element']["#items"]->first();
$main_property = $item->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName();
$value = $item->{$main_property};
// Fallback option for oembed old way, In case of change back to oembed
// fetch resource way.
$resource_url = $url_resolver->getResourceUrl($value, $max_width, $max_height);
$resource = $resource_fetcher->fetchResource($resource_url);
$provider = $resource->getProvider()->getName();
$url = Url::fromRoute('media.oembed_iframe', [], [
'query' => [
'url' => $value,
'max_width' => $max_width,
'max_height' => $max_height,
'type' => "remote_video",
'provider' => strtolower($provider ?? ''),
'view_mode' => $view_mode,
'hash' => $iframe_url_helper->getHash($value, $max_width, $max_height, $provider, $view_mode),
],
]);
$variables['items'][0]['content']['#attributes']['src'] = $url->toString();
}
}
}
/**
* Implements hook_entity_presave().
*/
function varbase_media_entity_presave(EntityInterface $entity) {
if ($entity->getEntityType()->id() == 'media' && $entity->bundle->target_id == 'remote_video') {
// If the field_provider exists in the remote video media type.
$field_field_media_remote_video_field_provider = FieldConfig::loadByName('media', 'remote_video', 'field_provider');
if (isset($field_field_media_remote_video_field_provider)) {
// Fetch the resource from the URL and save in the field_provider.
$url_resolver = \Drupal::service('media.oembed.url_resolver');
$resource_fetcher = \Drupal::service('media.oembed.resource_fetcher');
$resource_url = $url_resolver->getResourceUrl(($entity->field_media_oembed_video->value));
$resource = $resource_fetcher->fetchResource($resource_url);
$provider = strtolower($resource->getProvider()->getName() ?? '');
if ($entity->field_provider->value != $provider) {
$entity->set('field_provider', $provider);
}
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function varbase_media_preprocess_media_oembed_iframe(&$variables) {
// Send variables for all oembed iframe theme template.
$query = \Drupal::request()->query;
$variables['type'] = $query->get('type');
$variables['provider'] = $query->get('provider');
$variables['view_mode'] = $query->get('view_mode');
$variables['base_path'] = base_path();
$variables['varbase_media_path'] = \Drupal::service('module_handler')->getModule('varbase_media')->getPath();
}
/**
* Implements hook_theme_suggestions_media_oembed_iframe_alter().
*/
function varbase_media_theme_suggestions_media_oembed_iframe_alter(&$suggestions, &$vars) {
// Suggestions go here.
$query = \Drupal::request()->query;
$type = $query->get('type');
$provider = $query->get('provider');
$view_mode = $query->get('view_mode');
if ($type && $provider) {
$suggestions[] = "media_oembed_iframe__" . $provider;
$suggestions[] = "media_oembed_iframe__" . $provider . "__" . $view_mode;
$suggestions[] = "media_oembed_iframe__" . $view_mode;
$suggestions[] = "media_oembed_iframe__" . $type;
$suggestions[] = "media_oembed_iframe__" . $type . "__" . $view_mode;
$suggestions[] = "media_oembed_iframe__" . $type . "__" . $provider;
$suggestions[] = "media_oembed_iframe__" . $type . "__" . $provider . "__" . $view_mode;
}
}
/**
* Implements hook_ckeditor_css_alter().
*/
function varbase_media_ckeditor_css_alter(array &$css, Editor $editor) {
// Varbase media path.
$varbase_media_path = Drupal::service('module_handler')->getModule('varbase_media')->getPath();
// Attached the varbase media common style.
$css[] = $varbase_media_path . '/css/theme/varbase_media.common.css';
// Attached the varbase media common logged in users style.
$css[] = $varbase_media_path . '/css/theme/varbase_media.common_logged.css';
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function varbase_media_form_entity_embed_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
// Only at the embed step.
if ($form_state->get('step') == 'embed') {
// Get the entity values and attributes.
$entity_element = [];
$entity_element += $form_state->get('entity_element');
$form_state->set('entity_element', $entity_element);
$entity = $form_state->get('entity');
// Get the entity bundle type.
$bundle_type = $entity->bundle();
$builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
// Restrict the logic only to the media entity type.
if ($entity->getEntityTypeId() == "media") {
switch ($bundle_type) {
case "image":
// Image Media for review.
$media_view_mode = $builder->view($entity, 's03');
$media_markup = \Drupal::service('renderer')->renderRoot($media_view_mode);
// Render the Embed entity.
$form['entity'] = [
'#type' => 'item',
'#markup' => $media_markup,
];
// Change the "data align" field from radio buttons to Select list.
$form['attributes']['data-align']['#type'] = 'select';
$form['attributes']['data-align']['#wrapper_attributes'] = '';
$form['attributes']['data-align']['#description'] = t('Choose the positioning of the image.');
$form['attributes']['data-align']['#weight'] = -10;
// Add description for the caption field.
$form['attributes']['data-caption'] += [
'#description' => t('A caption will be displayed under the image, to describe it in context of your content.'),
];
// Adding the updated alt text to the media entity.
if (isset($entity_element['alt'])) {
$entity->field_media_image->alt = $entity_element['alt'];
}
// Adding the updated title text to the media entity.
if (isset($entity_element['title'])) {
$entity->field_media_image->title = $entity_element['title'];
}
if (isset($form['attributes']['data-entity-embed-display-settings'])) {
$form['attributes']['data-entity-embed-display-settings']['link_url']['#description'] = t('Start typing the title of a piece of content to select it. You can also enter an <br /> internal path such as /node/add or an external URL such as http://example.com.');
}
$entity->save();
break;
case "video":
case "remote_video":
// Video Media for review.
$media_view_mode = $builder->view($entity, 's06');
$media_markup = \Drupal::service('renderer')->renderRoot($media_view_mode);
// Render the Embed entity.
$form['entity'] = [
'#type' => 'item',
'#markup' => $media_markup,
];
if (isset($form['attributes']['data-align'])) {
unset($form['attributes']['data-align']);
}
if (isset($form['attributes']['data-entity-embed-display-settings'])) {
unset($form['attributes']['data-entity-embed-display-settings']);
}
if (isset($form['attributes']['data-caption'])) {
unset($form['attributes']['data-caption']);
}
if (isset($form['attributes']['data-entity-embed-display'])) {
$form['attributes']['data-entity-embed-display']['#access'] = FALSE;
$form['attributes']['data-entity-embed-display']['#default_value'] = 'view_mode:media.original';
}
break;
case "gallery":
// Gallery Media for review.
$media_view_mode = $builder->view($entity, 'browser_teaser');
$media_markup = \Drupal::service('renderer')->renderRoot($media_view_mode);
// Render the Embed entity.
$form['entity'] = [
'#type' => 'item',
'#markup' => '<div class="gallery-entity-embed-dialog-step--embed"><div class="media-library-item">' . $media_markup . '</div></div>',
];
// Render the Embed entity.
if (isset($form['attributes']['data-align'])) {
unset($form['attributes']['data-align']);
}
if (isset($form['attributes']['data-entity-embed-display-settings'])) {
unset($form['attributes']['data-entity-embed-display-settings']);
}
if (isset($form['attributes']['data-caption'])) {
unset($form['attributes']['data-caption']);
}
if (isset($form['attributes']['data-entity-embed-display'])) {
$form['attributes']['data-entity-embed-display']['#access'] = FALSE;
$form['attributes']['data-entity-embed-display']['#default_value'] = 'view_mode:media.full';
}
break;
default:
if (isset($form['attributes']['data-entity-embed-display-settings'])) {
unset($form['attributes']['data-entity-embed-display-settings']);
}
}
if (Drupal::service('module_handler')->moduleExists('blazy')) {
// Attach the Blazy library.
$form['#attached']['library'][] = 'blazy/load';
}
// No revision information or revision log message.
if (isset($form['revision_information'])) {
$form['revision_information']['#disabled'] = TRUE;
$form['revision_information']['#attributes']['style'][] = 'display: none;';
$form['revision_information']['#prefix'] = '<div style="display: none;">';
$form['revision_information']['#suffix'] = '</div>';
}
// Hide revision.
if (isset($form['revision'])) {
$form['revision']['#default_value'] = TRUE;
$form['revision']['#disabled'] = TRUE;
$form['revision']['#attributes']['style'][] = 'display: none;';
}
// Hide revision log message.
if (isset($form['revision_log_message'])) {
$form['revision_log_message']['#disabled'] = TRUE;
$form['revision_log_message']['#attributes']['style'][] = 'display: none;';
}
}
else {
// Remove all media fields for other entity types like ( node, term, or user).
if (isset($form['attributes']['data-caption'])) {
unset($form['attributes']['data-caption']);
}
if (isset($form['attributes']['data-align'])) {
unset($form['attributes']['data-align']);
}
}
}
}
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function varbase_media_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
// No revision information or revision log message.
if (isset($entity_form['revision_information'])) {
$entity_form['revision_information']['#disabled'] = TRUE;
$entity_form['revision_information']['#attributes']['style'][] = 'display:none;';
$entity_form['revision_information']['#prefix'] = '<div style="display: none;">';
$entity_form['revision_information']['#suffix'] = '</div>';
}
// Hide revision.
if (isset($entity_form['revision'])) {
$entity_form['revision']['#default_value'] = TRUE;
$entity_form['revision']['#disabled'] = TRUE;
$entity_form['revision']['#attributes']['style'][] = 'display: none;';
}
// Hide revision log message.
if (isset($entity_form['revision_log_message'])) {
$entity_form['revision_log_message']['#disabled'] = TRUE;
$entity_form['revision_log_message']['#attributes']['style'][] = 'display: none;';
}
}
/**
* Implements hook_theme_registry_alter().
*/
function varbase_media_theme_registry_alter(&$theme_registry) {
// Varbase Media path.
if (isset($theme_registry['entity_embed_container'])) {
$varbase_media_path = Drupal::service('module_handler')->getModule('varbase_media')->getPath();
$theme_registry['entity_embed_container']['path'] = $varbase_media_path . '/templates';
}
if (isset($theme_registry['fieldset__media_library_widget'])) {
$varbase_media_path = Drupal::service('module_handler')->getModule('varbase_media')->getPath();
$theme_registry['fieldset__media_library_widget']['path'] = $varbase_media_path . '/templates';
}
if (isset($theme_registry['media_library_item'])) {
$media_library_path = Drupal::service('module_handler')->getModule('media_library')->getPath();
$theme_registry['media_library_item']['path'] = $media_library_path . '/templates';
}
if (isset($theme_registry['media_library_item__widget'])) {
$media_library_path = Drupal::service('module_handler')->getModule('media_library')->getPath();
$theme_registry['media_library_item__widget']['path'] = $media_library_path . '/templates';
}
}
/**
* Implements hook_preprocess_media_library_item__widget().
*/
function varbase_media_preprocess_media_library_item__widget(array &$variables) {
$variables['content']['remove_button']['#attributes']['class'][] = 'media-library-item__remove';
$variables['content']['remove_button']['#attributes']['class'][] = 'icon-link';
}
/**
* Implements hook_preprocess_media_library_item().
*/
function varbase_media_preprocess_media_library_item(array &$variables) {
$variables['attributes']['class'][] = 'media-library-item';
$variables['attributes']['class'][] = 'media-library-item--grid';
}
/**
* Implements hook_preprocess_fieldset__media_library_widget().
*/
function varbase_media_preprocess_fieldset__media_library_widget(array &$variables) {
$variables['attributes']['class'][] = 'media-library-widget';
}
/**
* Implements hook_preprocess_container__media_library_widget_selection().
*/
function varbase_media_preprocess_container__media_library_widget_selection(array &$variables) {
$variables['attributes']['class'][] = 'media-library-selection';
}
/**
* Implements hook_preprocess_HOOK().
*/
function varbase_media_preprocess_entity_embed_container(&$variables) {
$variables['url'] = isset($variables['element']['#context']['data-entity-embed-display-settings']['link_url']) ? UrlHelper::filterBadProtocol($variables['element']['#context']['data-entity-embed-display-settings']['link_url']) : '';
}
/**
* Implements hook_entity_view_alter().
*/
function varbase_media_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($entity->getEntityTypeId() == 'media'
&& $build['#view_mode'] != 'field_preview') {
// Attached the varbase media common library.
$build['#attached']['library'][] = 'varbase_media/common';
if (!(\Drupal::currentUser()->isAnonymous())) {
// Attached the varbase media common logged in users library.
$build['#attached']['library'][] = 'varbase_media/common_logged';
}
if (isset($build['field_media_cover_image'])
&& isset($build['field_media_cover_image']['#items'])) {
$fields = $build['field_media_cover_image']['#items'];
if (is_object($fields)) {
// Hide thumbnail of media if we do have cover image data.
if (isset($build['thumbnail'])) {
$build['thumbnail']['#access'] = FALSE;
}
$build['field_media_cover_image']['#attached']['library'][] = 'varbase_media/varbase_video_player';
}
}
// Attach the varbase media video library for video embed field.
if (isset($build['field_media_oembed_video'])
&& isset($build['field_media_oembed_video'][0])) {
$build['field_media_oembed_video'][0]['#attached']['library'][] = 'varbase_media/varbase_video_player';
}
// Attach the varbase media video library for video file field.
if (isset($build['field_media_video_file'])) {
$build['field_media_video_file']['#attached']['library'][] = 'varbase_media/varbase_video_player';
}
}
}
/**
* Implements hook_media_source_info_alter().
*/
function varbase_media_media_source_info_alter(array &$sources) {
// Remote Video.
$sources['oembed:video']['input_match'] = [
'constraint' => 'oembed_resource',
'field_types' => [
'link',
'string',
'string_long',
],
];
$sources['oembed:video']['preview'] = TRUE;
OverrideHelper::pluginClass($sources['oembed:video'], VarbaseMediaRemoteVideo::class);
}
/**
* Implements hook_form_alter().
*/
function varbase_media_form_alter(&$form, &$form_state, $form_id) {
if ($form_state->getFormObject() instanceof EntityFormInterface) {
$entity_type = $form_state->getFormObject()->getEntity()->getEntityTypeId();
// Only for media entity type.
if ($entity_type == 'media') {
// No revision information or revision log message.
if (isset($form['revision_information'])) {
$form['revision_information']['#disabled'] = TRUE;
$form['revision_information']['#attributes']['style'][] = 'display:none;';
$form['revision_information']['#prefix'] = '<div style="display: none;">';
$form['revision_information']['#suffix'] = '</div>';
}
// Hide revision.
if (isset($form['revision'])) {
$form['revision']['#default_value'] = TRUE;
$form['revision']['#disabled'] = TRUE;
$form['revision']['#attributes']['style'][] = 'display: none;';
}
// Hide revision log message.
if (isset($form['revision_log_message'])) {
$form['revision_log_message']['#disabled'] = TRUE;
$form['revision_log_message']['#attributes']['style'][] = 'display: none;';
}
}
}
if ($form_id == 'entity_browser_image_browser_form'
|| $form_id == 'entity_browser_media_browser_form'
|| $form_id == 'entity_browser_editor_media_browser_form') {
$form['#attached']['library'][] = 'varbase_media/auto_fill_media_data';
}
if ($form_id === "entity_browser_widgets_config_form") {
// Attach the varbase media common library.
$form['#attached']['library'][] = 'varbase_media/common';
if (!(\Drupal::currentUser()->isAnonymous())) {
// Attached the varbase media common logged in users library.
$form['#attached']['library'][] = 'varbase_media/common_logged';
}
}
if ($form_id === 'editor_media_dialog') {
if (isset($form_state->getUserInput()['editor_object'])) {
$editor_object = $form_state->getUserInput()['editor_object'];
$media_embed_element = $editor_object['attributes'];
if (isset($media_embed_element['data-entity-type'])
&& $media_embed_element['data-entity-type'] == 'media') {
$media = \Drupal::service('entity.repository')->loadEntityByUuid('media', $media_embed_element['data-entity-uuid']);
if (isset($media)) {
$media_type = $media->bundle();
switch ($media_type) {
case 'image':
if (isset($form['alt'])) {
$form['alt']['#required'] = TRUE;
$field_definition = $media->getSource()->getSourceFieldDefinition($media->bundle->entity);
$item_class = $field_definition->getItemDefinition()->getClass();
if (is_a($item_class, ImageItem::class, TRUE)) {
$image_field_name = $field_definition->getName();
// We'll want the alt text from the same language as the host.
if (!empty($editor_object['hostEntityLangcode']) && $media->hasTranslation($editor_object['hostEntityLangcode'])) {
$media = $media->getTranslation($editor_object['hostEntityLangcode']);
}
$alt = $media_embed_element['alt'] ?? $media->{$image_field_name}->alt;
$form['alt']['#default_value'] = $alt;
$form['alt']['#placeholder'] = $media->{$image_field_name}->alt;
}
}
break;
case 'audio':
case 'file':
if (isset($form['view_mode'])) {
$form['view_mode']['#access'] = FALSE;
}
break;
case 'video':
case 'remote_video':
if (isset($form['caption'])) {
$form['caption']['#access'] = FALSE;
$form['caption']['#default_value'] = FALSE;
}
break;
}
}
}
}
}
if ($form_id === 'media_bulk_upload_form') {
$form['#submit'][] = '_varbase_media_bulk_upload_form_submit_handler';
}
}
/**
* Varbase media bulk upload form submit handler.
*
* Redirect the user back to the Media page (/admin/content/media)
* after successful bulk upload.
*/
function _varbase_media_bulk_upload_form_submit_handler(&$form, &$form_state) {
if (\Drupal::moduleHandler()->moduleExists('media_library')) {
$media_library_grid_url = Url::fromRoute('view.media_library.page');
$form_state->setRedirectUrl($media_library_grid_url);
}
else {
$media_library_table_url = Url::fromRoute('entity.media.collection');
$form_state->setRedirectUrl($media_library_table_url);
}
}
/**
* Implements hook_theme().
*/
function varbase_media_theme($existing, $type, $theme, $path) {
return [
'media_oembed_iframe__remote_video' => [
'template' => 'media-oembed-iframe--remote-video',
'variables' => [
'provider' => NULL,
'media' => NULL,
],
],
'media_entity_gallery_post' => [
'variables' => [
'post' => NULL,
'shortcode' => NULL,
],
],
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function varbase_media_preprocess_media_oembed_iframe__remote_video(&$variables) {
// Send variables for the remote_video oembed iframe theme template.
$query = \Drupal::request()->query;
$variables['type'] = $query->get('type');
$variables['provider'] = $query->get('provider');
$variables['view_mode'] = $query->get('view_mode');
$variables['base_path'] = base_path();
$variables['varbase_media_path'] = \Drupal::service('module_handler')->getModule('varbase_media')->getPath();
}
/**
* Implements hook_entity_embed_alter().
*/
function varbase_media_entity_embed_alter(array &$build, EntityInterface $entity, array &$context) {
// Only for entity embed review inside the CKEditor.
$preview_route_name = \Drupal::routeMatch()->getRouteName();
if ($preview_route_name == 'embed.preview' || $preview_route_name == 'entity_embed.preview') {
// Switch view mode for gallery in the CKEditor to show the Browser Teaser.
if (isset($context['data-embed-button'])
&& $context['data-embed-button'] == 'gallery') {
// Remove the contextual links.
if (isset($build['#contextual_links'])) {
unset($build['#contextual_links']);
}
if ($build['#context']['data-entity-embed-display'] == 'view_mode:media.full') {
$build['#context']['data-entity-embed-display'] = 'view_mode:media.browser_teaser';
$build['entity']['#view_mode'] = 'browser_teaser';
}
}
}
}
/**
* Implements hook_oembed_resource_url_alter().
*/
function varbase_media_oembed_resource_url_alter(array &$parsed_url, Provider $provider) {
// Process arguments for vimeo videos to be included in oEmbed.
if ($provider->getName() == 'Vimeo') {
$url = $parsed_url['query']['url'];
// Use '/&' as a separator between arguments.
$url = str_replace('&', '/&', $url);
$url = str_replace('?', '/&', $url);
$parsed_url['query']['url'] = $url;
}
}
/**
* Implements hook_library_info_alter().
*/
function varbase_media_library_info_alter(&$libraries, $extension) {
if ($extension === 'media_library' && isset($libraries['widget'])) {
$libraries['widget']['dependencies'][] = 'varbase_media/media_library_enhancements';
}
if ($extension === 'ckeditor5') {
$libraries['internal.drupal.ckeditor5.media']['dependencies'][] = 'drimage_improved/drimage_improved';
$libraries['internal.drupal.ckeditor5.media']['dependencies'][] = 'varbase_media/common';
$libraries['internal.drupal.ckeditor5.media']['dependencies'][] = 'varbase_media/common_logged';
$libraries['internal.drupal.ckeditor5.media']['dependencies'][] = 'varbase_media/varbase_video_player';
$libraries['internal.drupal.ckeditor5.media']['dependencies'][] = 'varbase_media/ckeditor_drimage';
$libraries['internal.drupal.ckeditor5.media']['dependencies'][] = 'varbase_media/ckeditor_varbase_video_player';
}
}