-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathislandora_vtt.module
More file actions
129 lines (112 loc) · 3.32 KB
/
Copy pathislandora_vtt.module
File metadata and controls
129 lines (112 loc) · 3.32 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
<?php
/**
* @file
* Contains islandora_vtt.module.
*/
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\islandora_vtt\Hook\IslandoraVttHooks;
use Drupal\media\Entity\Media;
/**
* Implements hook_theme().
*/
#[LegacyHook]
function islandora_vtt_theme($existing, $type, $theme, $path) {
return \Drupal::service(IslandoraVttHooks::class)->theme($existing, $type, $theme, $path);
}
/**
* Implements hook_preprocess_media().
*/
#[LegacyHook]
function islandora_vtt_preprocess_media(&$vars) {
\Drupal::service(IslandoraVttHooks::class)->preprocessMedia($vars);
}
/**
* Helper function to get a media's sibling VTT files.
*/
function islandora_vtt_get_vtts($media) {
$rows = islandora_vtt_get_vtt_file_rows($media);
if (empty($rows)) {
return [];
}
$mids = array_column($rows, 'mid');
return Media::loadMultiple($mids);
}
/**
* Helper function to get VTT file rows from the current node.
*/
function islandora_vtt_get_vtt_file_rows($media) {
$node_id = islandora_vtt_get_node_id($media);
if (!$node_id) {
return [];
}
$tid = islandora_vtt_extracted_text_tid();
if (!$tid) {
return [];
}
$query = \Drupal::database()->select('media_field_data', 'mfd');
$query->join('media__field_media_of', 'mo', 'mo.entity_id = mfd.mid');
$query->join('media__field_media_use', 'u', 'u.entity_id = mfd.mid');
$query->join('media__field_media_file', 'f', 'f.entity_id = mfd.mid');
$query->join('file_managed', 'fm', 'fm.fid = f.field_media_file_target_id');
$query
->fields('mfd', ['mid', 'bundle', 'langcode']);
$query->addField('f', 'field_media_file_target_id', 'fid');
$query->addField('fm', 'uri', 'uri');
$query->addField('fm', 'filename', 'filename');
$rows = $query
->condition('mo.deleted', 0)
->condition('u.deleted', 0)
->condition('f.deleted', 0)
->condition('mfd.bundle', 'extracted_text')
->condition('mo.field_media_of_target_id', $node_id)
->condition('u.field_media_use_target_id', $tid)
->orderBy('mfd.langcode')
->orderBy('mfd.mid')
->execute()
->fetchAllAssoc('mid');
if (empty($rows)) {
return [];
}
$media_by_id = Media::loadMultiple(array_keys($rows));
return array_filter($rows, static function ($row) use ($media_by_id) {
return isset($media_by_id[$row->mid]) && $media_by_id[$row->mid]->access('view');
});
}
/**
* Helper function to get the node ID that should own the VTT media.
*/
function islandora_vtt_get_node_id($media) {
$node = \Drupal::routeMatch()->getParameter('node');
if (is_object($node) && $node->id()) {
return $node->id();
}
if (is_numeric($node)) {
return $node;
}
if ($media->hasField('field_media_of') && !$media->field_media_of->isEmpty()) {
return $media->field_media_of->target_id;
}
return NULL;
}
/**
* Helper function to get the first media sibling VTT file.
*/
function islandora_vtt_get_vtt($media) {
$vtts = islandora_vtt_get_vtts($media);
return reset($vtts) ?: FALSE;
}
/**
* Helper function to get the extracted text term ID.
*/
function islandora_vtt_extracted_text_tid() {
static $tid = '';
if ($tid != '') {
return $tid;
}
$tid = \Drupal::database()->query('SELECT tid FROM {taxonomy_term_field_data}
WHERE name = :name AND vid = :vid', [
':vid' => 'islandora_media_use',
':name' => 'Extracted Text',
])->fetchField();
return $tid;
}