forked from md-systems/file_entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_entity.field.inc
401 lines (373 loc) · 13.5 KB
/
file_entity.field.inc
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
<?php
/**
* @file
* Field API integration for the file_entity module.
*/
/**
* Implements hook_field_formatter_info().
*/
function file_entity_field_formatter_info() {
$info['file_rendered'] = array(
'label' => t('Rendered file'),
'description' => t('Display the file in a specific view mode'),
'field types' => array('file', 'image'),
'settings' => array(
'file_view_mode' => 'default',
),
'file formatter' => array(
'hidden' => TRUE,
),
);
$info['file_download_link'] = array(
'label' => t('Download link'),
'description' => t('Displays a link that will force the browser to download the file.'),
'field types' => array('file', 'image'),
'settings' => array(
'text' => t('Download [file:name]'),
),
);
$info['file_audio'] = array(
'label' => t('Audio'),
'description' => t('Render the file using an HTML5 audio tag.'),
'field types' => array('file'),
'settings' => array(
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'multiple_file_behavior' => 'tags',
),
'file formatter' => array(
'mime types' => array('audio/*'),
),
);
$info['file_video'] = array(
'label' => t('Video'),
'description' => t('Render the file using an HTML5 video tag.'),
'field types' => array('file'),
'settings' => array(
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'muted' => FALSE,
'width' => NULL,
'height' => NULL,
'multiple_file_behavior' => 'tags',
),
'file formatter' => array(
'mime types' => array('video/*'),
),
);
return $info;
}
/**
* Implements hook_field_formatter_info_alter().
*/
function file_entity_field_formatter_info_alter(&$info) {
return;
// Add descriptions to core formatters.
$descriptions = array(
'file_default' => t('Create a simple link to the file. The link is prefixed by a file type icon and the name of the file is used as the link text'),
'file_table' => t('Build a two-column table where the first column contains a generic link to the file and the second column displays the size of the file.'),
'file_url_plain' => t('Display a plain text URL to the file.'),
'image' => t('Format the file as an image. The image can be displayed using an image style and can optionally be linked to the image file itself or its parent content.'),
);
foreach ($descriptions as $key => $description) {
if (isset($info[$key]) && empty($info[$key]['description'])) {
$info[$key]['description'] = $description;
}
}
// Formatters that can be used for images but not files, should have a
// default mimetype restriction added to the image/* mime type for use with
// file formatters.
foreach ($info as &$formatter) {
if (!isset($formatter['file formatter']) && in_array('image', $formatter['field types']) && !in_array('file', $formatter['field types'])) {
$formatter['file formatter']['mime types'] = array('image/*');
}
}
}
/**
* Implements hook_field_formatter_settings_form().
*/
function file_entity_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'file_rendered') {
$element['file_view_mode'] = array(
'#title' => t('View mode'),
'#type' => 'select',
'#options' => \Drupal::entityManager()->getViewModeOptions('file'),
'#default_value' => $settings['file_view_mode'],
// Never empty, so no #empty_option
);
}
elseif ($display['type'] == 'file_download_link') {
$element['text'] = array(
'#type' => 'textfield',
'#title' => t('Link text'),
'#description' => t('This field support tokens.'),
'#default_value' => $settings['text'],
'#required' => TRUE,
);
}
elseif ($display['type'] == 'file_audio') {
$element['controls'] = array(
'#title' => t('Show audio controls'),
'#type' => 'checkbox',
'#default_value' => $settings['controls'],
);
$element['autoplay'] = array(
'#title' => t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $settings['autoplay'],
);
$element['loop'] = array(
'#title' => t('Loop'),
'#type' => 'checkbox',
'#default_value' => $settings['loop'],
);
$element['multiple_file_behavior'] = array(
'#title' => t('Display of multiple files'),
'#type' => 'radios',
'#options' => array(
'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<audio>')),
'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<audio>')),
),
'#default_value' => $settings['multiple_file_behavior'],
// Hide this setting in the manage file display configuration.
'#access' => !empty($field),
);
}
elseif ($display['type'] == 'file_video') {
$element['controls'] = array(
'#title' => t('Show video controls'),
'#type' => 'checkbox',
'#default_value' => $settings['controls'],
);
$element['autoplay'] = array(
'#title' => t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $settings['autoplay'],
);
$element['loop'] = array(
'#title' => t('Loop'),
'#type' => 'checkbox',
'#default_value' => $settings['loop'],
);
$element['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $settings['width'],
'#size' => 5,
'#maxlength' => 5,
'#field_suffix' => t('pixels'),
);
$element['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $settings['height'],
'#size' => 5,
'#maxlength' => 5,
'#field_suffix' => t('pixels'),
);
$element['multiple_file_behavior'] = array(
'#title' => t('Display of multiple files'),
'#type' => 'radios',
'#options' => array(
'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<video>')),
'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<video>')),
),
'#default_value' => $settings['multiple_file_behavior'],
// Hide this setting in the manage file display configuration.
'#access' => !empty($field),
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function file_entity_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
if ($display['type'] === 'file_rendered') {
$view_mode_label = file_entity_view_mode_label($settings['file_view_mode'], t('Unknown'));
$summary[] = t('View mode: %mode', array('%mode' => $view_mode_label));
}
elseif ($display['type'] == 'file_download_link') {
$summary[] = t('Link text: %text', array('%text' => $settings['text']));
}
elseif ($display['type'] === 'file_audio') {
if (isset($settings['controls'])) {
$summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
}
if (isset($settings['autoplay'])) {
$summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
}
if (isset($settings['loop'])) {
$summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
}
if (isset($settings['multiple_file_behavior'])) {
$summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
}
}
elseif ($display['type'] === 'file_video') {
$summary_items = array();
if (isset($settings['controls'])) {
$summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
}
if (isset($settings['autoplay'])) {
$summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
}
if (isset($settings['loop'])) {
$summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
}
if (isset($settings['muted'])) {
$summary[] = t('Muted: %muted', array('%muted' => $settings['muted'] ? t('yes') : t('no')));
}
if ($settings['width'] && $settings['height']) {
$summary[] = t('Size: %width x %height', array('%width' => $settings['width'], '%height' => $settings['height']));
}
if (isset($settings['multiple_file_behavior'])) {
$summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
}
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function file_entity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'file_rendered') {
$view_mode = $settings['file_view_mode'];
// To prevent infinite recursion caused by reference cycles, we store
// diplayed nodes in a recursion queue.
$recursion_queue = &drupal_static(__FUNCTION__, array());
// If no 'referencing entity' is set, we are starting a new 'reference
// thread' and need to reset the queue.
// @todo Bug: $entity->referencing_entity on files referenced in a different
// thread on the page.
// E.g: 1 references 1+2 / 2 references 1+2 / visit homepage.
// We'd need a more accurate way...
if (!isset($entity->referencing_entity)) {
$recursion_queue = array();
}
// The recursion queue only needs to track files.
if ($entity_type == 'file') {
list($id) = entity_extract_ids($entity_type, $entity);
$recursion_queue[$id] = $id;
}
// Prevent 'empty' fields from causing a WSOD.
$items = array_filter($items);
// Check the recursion queue to determine which nodes should be fully
// displayed, and which nodes will only be displayed as a title.
$files_display = array();
foreach ($items as $delta => $item) {
if (!isset($recursion_queue[$item['fid']])) {
$files_display[$item['fid']] = file_load($item['fid']);
if (!empty($item['description'])) {
$files_display[$item['fid']]->description = $item['description'];
}
}
}
// Load and build the fully displayed nodes.
if ($files_display) {
foreach ($files_display as $fid => $file) {
$files_display[$fid]->referencing_entity = $entity;
$files_display[$fid]->referencing_field = $field['field_name'];
}
$output = file_view_multiple($files_display, $view_mode);
// Remove the first level from the output array.
$files_built = reset($output);
}
// Assemble the render array.
foreach ($items as $delta => $item) {
if (isset($files_built[$item['fid']])) {
$element[$delta] = $files_built[$item['fid']];
}
}
}
elseif ($display['type'] == 'file_download_link') {
foreach ($items as $delta => $item) {
$file = (object) $item;
if (file_entity_file_is_local($file) && file_entity_access('download', $file)) {
$element[$delta] = array(
'#theme' => 'file_entity_download_link',
'#file' => $file,
'#text' => $settings['text'],
);
}
}
}
elseif ($display['type'] == 'file_audio') {
$multiple_file_behavior = $settings['multiple_file_behavior'];
// Prevent 'empty' fields from causing a WSOD.
$items = array_filter($items);
// Build an array of sources for each <audio> element.
$source_lists = array();
if ($multiple_file_behavior == 'tags') {
foreach ($items as $delta => $item) {
if ($item['type'] == 'audio') {
$source_lists[] = array($item);
}
}
}
else {
foreach ($items as $delta => $item) {
if ($item['type'] == 'audio') {
$source_lists[0][] = $item;
}
}
}
// Render each source list as an <audio> element.
foreach ($source_lists as $delta => $sources) {
$element[$delta] = array(
'#theme' => 'file_entity_file_audio',
'#files' => $sources,
'#controls' => $settings['controls'],
'#autoplay' => $settings['autoplay'],
'#loop' => $settings['loop'],
);
}
}
elseif ($display['type'] == 'file_video') {
$multiple_file_behavior = $settings['multiple_file_behavior'];
// Prevent 'empty' fields from causing a WSOD.
$items = array_filter($items);
// Build an array of sources for each <video> element.
$source_lists = array();
if ($multiple_file_behavior == 'tags') {
foreach ($items as $delta => $item) {
if ($item['type'] == 'video') {
$source_lists[] = array($item);
}
}
}
else {
foreach ($items as $delta => $item) {
if ($item['type'] == 'video') {
$source_lists[0][] = $item;
}
}
}
// Render each source list as an <video> element.
foreach ($source_lists as $delta => $sources) {
$width = $settings['width'];
$height = $settings['height'];
$element[$delta] = array(
'#theme' => 'file_entity_file_video',
'#files' => $sources,
'#controls' => $settings['controls'],
'#autoplay' => $settings['autoplay'],
'#loop' => $settings['loop'],
'#muted' => $settings['muted'],
'#width' => ($width && $height) ? $width : NULL,
'#height' => ($width && $height) ? $height : NULL,
);
}
}
return $element;
}