1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Field API integration for the file_entity module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_field_formatter_info().
|
10
|
*/
|
11
|
function file_entity_field_formatter_info() {
|
12
|
$info['file_rendered'] = array(
|
13
|
'label' => t('Rendered file'),
|
14
|
'description' => t('Display the file in a specific view mode'),
|
15
|
'field types' => array('file', 'image'),
|
16
|
'settings' => array(
|
17
|
'file_view_mode' => 'default',
|
18
|
),
|
19
|
'file formatter' => array(
|
20
|
'hidden' => TRUE,
|
21
|
),
|
22
|
);
|
23
|
$info['file_download_link'] = array(
|
24
|
'label' => t('Download link'),
|
25
|
'description' => t('Displays a link that will force the browser to download the file.'),
|
26
|
'field types' => array('file', 'image'),
|
27
|
'settings' => array(
|
28
|
'text' => t('Download [file:name]'),
|
29
|
),
|
30
|
);
|
31
|
$info['file_audio'] = array(
|
32
|
'label' => t('Audio'),
|
33
|
'description' => t('Render the file using an HTML5 audio tag.'),
|
34
|
'field types' => array('file'),
|
35
|
'settings' => array(
|
36
|
'controls' => TRUE,
|
37
|
'autoplay' => FALSE,
|
38
|
'loop' => FALSE,
|
39
|
'multiple_file_behavior' => 'tags',
|
40
|
),
|
41
|
'file formatter' => array(
|
42
|
'mime types' => array('audio/*'),
|
43
|
),
|
44
|
);
|
45
|
$info['file_video'] = array(
|
46
|
'label' => t('Video'),
|
47
|
'description' => t('Render the file using an HTML5 video tag.'),
|
48
|
'field types' => array('file'),
|
49
|
'settings' => array(
|
50
|
'controls' => TRUE,
|
51
|
'autoplay' => FALSE,
|
52
|
'loop' => FALSE,
|
53
|
'muted' => FALSE,
|
54
|
'width' => NULL,
|
55
|
'height' => NULL,
|
56
|
'multiple_file_behavior' => 'tags',
|
57
|
),
|
58
|
'file formatter' => array(
|
59
|
'mime types' => array('video/*'),
|
60
|
),
|
61
|
);
|
62
|
return $info;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Implements hook_field_formatter_info_alter().
|
67
|
*/
|
68
|
function file_entity_field_formatter_info_alter(&$info) {
|
69
|
// Add descriptions to core formatters.
|
70
|
$descriptions = array(
|
71
|
'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'),
|
72
|
'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.'),
|
73
|
'file_url_plain' => t('Display a plain text URL to the file.'),
|
74
|
'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.'),
|
75
|
);
|
76
|
foreach ($descriptions as $key => $description) {
|
77
|
if (isset($info[$key]) && empty($info[$key]['description'])) {
|
78
|
$info[$key]['description'] = $description;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
// Formatters that can be used for images but not files, should have a
|
83
|
// default mimetype restriction added to the image/* mime type for use with
|
84
|
// file formatters.
|
85
|
foreach ($info as &$formatter) {
|
86
|
if (!isset($formatter['file formatter']) && in_array('image', $formatter['field types']) && !in_array('file', $formatter['field types'])) {
|
87
|
$formatter['file formatter']['mime types'] = array('image/*');
|
88
|
}
|
89
|
}
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Implements hook_field_formatter_settings_form().
|
94
|
*/
|
95
|
function file_entity_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
|
96
|
$display = $instance['display'][$view_mode];
|
97
|
$settings = $display['settings'];
|
98
|
$element = array();
|
99
|
|
100
|
if ($display['type'] == 'file_rendered') {
|
101
|
$element['file_view_mode'] = array(
|
102
|
'#title' => t('View mode'),
|
103
|
'#type' => 'select',
|
104
|
'#options' => file_entity_view_mode_labels(),
|
105
|
'#default_value' => $settings['file_view_mode'],
|
106
|
// Never empty, so no #empty_option
|
107
|
);
|
108
|
}
|
109
|
elseif ($display['type'] == 'file_download_link') {
|
110
|
$element['text'] = array(
|
111
|
'#type' => 'textfield',
|
112
|
'#title' => t('Link text'),
|
113
|
'#description' => t('This field support tokens.'),
|
114
|
'#default_value' => $settings['text'],
|
115
|
'#required' => TRUE,
|
116
|
);
|
117
|
}
|
118
|
elseif ($display['type'] == 'file_audio') {
|
119
|
$element['controls'] = array(
|
120
|
'#title' => t('Show audio controls'),
|
121
|
'#type' => 'checkbox',
|
122
|
'#default_value' => $settings['controls'],
|
123
|
);
|
124
|
$element['autoplay'] = array(
|
125
|
'#title' => t('Autoplay'),
|
126
|
'#type' => 'checkbox',
|
127
|
'#default_value' => $settings['autoplay'],
|
128
|
);
|
129
|
$element['loop'] = array(
|
130
|
'#title' => t('Loop'),
|
131
|
'#type' => 'checkbox',
|
132
|
'#default_value' => $settings['loop'],
|
133
|
);
|
134
|
$element['multiple_file_behavior'] = array(
|
135
|
'#title' => t('Display of multiple files'),
|
136
|
'#type' => 'radios',
|
137
|
'#options' => array(
|
138
|
'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<audio>')),
|
139
|
'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<audio>')),
|
140
|
),
|
141
|
'#default_value' => $settings['multiple_file_behavior'],
|
142
|
// Hide this setting in the manage file display configuration.
|
143
|
'#access' => !empty($field),
|
144
|
);
|
145
|
|
146
|
}
|
147
|
elseif ($display['type'] == 'file_video') {
|
148
|
$element['controls'] = array(
|
149
|
'#title' => t('Show video controls'),
|
150
|
'#type' => 'checkbox',
|
151
|
'#default_value' => $settings['controls'],
|
152
|
);
|
153
|
$element['autoplay'] = array(
|
154
|
'#title' => t('Autoplay'),
|
155
|
'#type' => 'checkbox',
|
156
|
'#default_value' => $settings['autoplay'],
|
157
|
);
|
158
|
$element['loop'] = array(
|
159
|
'#title' => t('Loop'),
|
160
|
'#type' => 'checkbox',
|
161
|
'#default_value' => $settings['loop'],
|
162
|
);
|
163
|
$element['width'] = array(
|
164
|
'#type' => 'textfield',
|
165
|
'#title' => t('Width'),
|
166
|
'#default_value' => $settings['width'],
|
167
|
'#size' => 5,
|
168
|
'#maxlength' => 5,
|
169
|
'#field_suffix' => t('pixels'),
|
170
|
);
|
171
|
$element['height'] = array(
|
172
|
'#type' => 'textfield',
|
173
|
'#title' => t('Height'),
|
174
|
'#default_value' => $settings['height'],
|
175
|
'#size' => 5,
|
176
|
'#maxlength' => 5,
|
177
|
'#field_suffix' => t('pixels'),
|
178
|
);
|
179
|
$element['multiple_file_behavior'] = array(
|
180
|
'#title' => t('Display of multiple files'),
|
181
|
'#type' => 'radios',
|
182
|
'#options' => array(
|
183
|
'tags' => t('Use multiple @tag tags, each with a single source', array('@tag' => '<video>')),
|
184
|
'sources' => t('Use multiple sources within a single @tag tag', array('@tag' => '<video>')),
|
185
|
),
|
186
|
'#default_value' => $settings['multiple_file_behavior'],
|
187
|
// Hide this setting in the manage file display configuration.
|
188
|
'#access' => !empty($field),
|
189
|
);
|
190
|
}
|
191
|
|
192
|
return $element;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Implements hook_field_formatter_settings_summary().
|
197
|
*/
|
198
|
function file_entity_field_formatter_settings_summary($field, $instance, $view_mode) {
|
199
|
$display = $instance['display'][$view_mode];
|
200
|
$settings = $display['settings'];
|
201
|
$summary = array();
|
202
|
|
203
|
if ($display['type'] === 'file_rendered') {
|
204
|
$view_mode_label = file_entity_view_mode_label($settings['file_view_mode'], t('Unknown'));
|
205
|
$summary[] = t('View mode: %mode', array('%mode' => $view_mode_label));
|
206
|
}
|
207
|
elseif ($display['type'] == 'file_download_link') {
|
208
|
$summary[] = t('Link text: %text', array('%text' => $settings['text']));
|
209
|
}
|
210
|
elseif ($display['type'] === 'file_audio') {
|
211
|
if (isset($settings['controls'])) {
|
212
|
$summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
|
213
|
}
|
214
|
if (isset($settings['autoplay'])) {
|
215
|
$summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
|
216
|
}
|
217
|
if (isset($settings['loop'])) {
|
218
|
$summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
|
219
|
}
|
220
|
if (isset($settings['multiple_file_behavior'])) {
|
221
|
$summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
|
222
|
}
|
223
|
|
224
|
}
|
225
|
elseif ($display['type'] === 'file_video') {
|
226
|
$summary_items = array();
|
227
|
if (isset($settings['controls'])) {
|
228
|
$summary[] = t('Controls: %controls', array('%controls' => $settings['controls'] ? 'visible' : 'hidden'));
|
229
|
}
|
230
|
if (isset($settings['autoplay'])) {
|
231
|
$summary[] = t('Autoplay: %autoplay', array('%autoplay' => $settings['autoplay'] ? t('yes') : t('no')));
|
232
|
}
|
233
|
if (isset($settings['loop'])) {
|
234
|
$summary[] = t('Loop: %loop', array('%loop' => $settings['loop'] ? t('yes') : t('no')));
|
235
|
}
|
236
|
if (isset($settings['muted'])) {
|
237
|
$summary[] = t('Muted: %muted', array('%muted' => $settings['muted'] ? t('yes') : t('no')));
|
238
|
}
|
239
|
if ($settings['width'] && $settings['height']) {
|
240
|
$summary[] = t('Size: %width x %height', array('%width' => $settings['width'], '%height' => $settings['height']));
|
241
|
}
|
242
|
if (isset($settings['multiple_file_behavior'])) {
|
243
|
$summary[] = t('Multiple files: %multiple', array('%multiple' => $settings['multiple_file_behavior']));
|
244
|
}
|
245
|
}
|
246
|
|
247
|
return implode('<br />', $summary);
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* Implements hook_field_formatter_view().
|
252
|
*/
|
253
|
function file_entity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
254
|
$settings = $display['settings'];
|
255
|
$element = array();
|
256
|
|
257
|
if ($display['type'] == 'file_rendered') {
|
258
|
$view_mode = $settings['file_view_mode'];
|
259
|
|
260
|
// To prevent infinite recursion caused by reference cycles, we store
|
261
|
// diplayed nodes in a recursion queue.
|
262
|
$recursion_queue = &drupal_static(__FUNCTION__, array());
|
263
|
|
264
|
// If no 'referencing entity' is set, we are starting a new 'reference
|
265
|
// thread' and need to reset the queue.
|
266
|
// @todo Bug: $entity->referencing_entity on files referenced in a different
|
267
|
// thread on the page.
|
268
|
// E.g: 1 references 1+2 / 2 references 1+2 / visit homepage.
|
269
|
// We'd need a more accurate way...
|
270
|
if (!isset($entity->referencing_entity)) {
|
271
|
$recursion_queue = array();
|
272
|
}
|
273
|
|
274
|
// The recursion queue only needs to track files.
|
275
|
if ($entity_type == 'file') {
|
276
|
list($id) = entity_extract_ids($entity_type, $entity);
|
277
|
$recursion_queue[$id] = $id;
|
278
|
}
|
279
|
|
280
|
// Prevent 'empty' fields from causing a WSOD.
|
281
|
$items = array_filter($items);
|
282
|
|
283
|
// Check the recursion queue to determine which nodes should be fully
|
284
|
// displayed, and which nodes will only be displayed as a title.
|
285
|
$files_display = array();
|
286
|
foreach ($items as $delta => $item) {
|
287
|
if (!isset($recursion_queue[$item['fid']])) {
|
288
|
$files_display[$item['fid']] = file_load($item['fid']);
|
289
|
}
|
290
|
}
|
291
|
|
292
|
// Load and build the fully displayed nodes.
|
293
|
if ($files_display) {
|
294
|
foreach ($files_display as $fid => $file) {
|
295
|
$files_display[$fid]->referencing_entity = $entity;
|
296
|
$files_display[$fid]->referencing_field = $field['field_name'];
|
297
|
}
|
298
|
$output = file_view_multiple($files_display, $view_mode);
|
299
|
// Remove the first level from the output array.
|
300
|
$files_built = reset($output);
|
301
|
}
|
302
|
|
303
|
// Assemble the render array.
|
304
|
foreach ($items as $delta => $item) {
|
305
|
if (isset($files_built[$item['fid']])) {
|
306
|
$element[$delta] = $files_built[$item['fid']];
|
307
|
}
|
308
|
}
|
309
|
}
|
310
|
elseif ($display['type'] == 'file_download_link') {
|
311
|
foreach ($items as $delta => $item) {
|
312
|
$file = (object) $item;
|
313
|
if (file_entity_access('download', $file)) {
|
314
|
$element[$delta] = array(
|
315
|
'#theme' => 'file_entity_download_link',
|
316
|
'#file' => $file,
|
317
|
'#text' => $settings['text'],
|
318
|
);
|
319
|
}
|
320
|
}
|
321
|
}
|
322
|
elseif ($display['type'] == 'file_audio') {
|
323
|
$multiple_file_behavior = $settings['multiple_file_behavior'];
|
324
|
|
325
|
// Prevent 'empty' fields from causing a WSOD.
|
326
|
$items = array_filter($items);
|
327
|
|
328
|
// Build an array of sources for each <audio> element.
|
329
|
$source_lists = array();
|
330
|
if ($multiple_file_behavior == 'tags') {
|
331
|
foreach ($items as $delta => $item) {
|
332
|
if ($item['type'] == 'audio') {
|
333
|
$source_lists[] = array($item);
|
334
|
}
|
335
|
}
|
336
|
}
|
337
|
else {
|
338
|
foreach ($items as $delta => $item) {
|
339
|
if ($item['type'] == 'audio') {
|
340
|
$source_lists[0][] = $item;
|
341
|
}
|
342
|
}
|
343
|
}
|
344
|
|
345
|
// Render each source list as an <audio> element.
|
346
|
foreach ($source_lists as $delta => $sources) {
|
347
|
$element[$delta] = array(
|
348
|
'#theme' => 'file_entity_file_audio',
|
349
|
'#files' => $sources,
|
350
|
'#controls' => $settings['controls'],
|
351
|
'#autoplay' => $settings['autoplay'],
|
352
|
'#loop' => $settings['loop'],
|
353
|
);
|
354
|
}
|
355
|
}
|
356
|
elseif ($display['type'] == 'file_video') {
|
357
|
$multiple_file_behavior = $settings['multiple_file_behavior'];
|
358
|
|
359
|
// Prevent 'empty' fields from causing a WSOD.
|
360
|
$items = array_filter($items);
|
361
|
|
362
|
// Build an array of sources for each <video> element.
|
363
|
$source_lists = array();
|
364
|
if ($multiple_file_behavior == 'tags') {
|
365
|
foreach ($items as $delta => $item) {
|
366
|
if ($item['type'] == 'video') {
|
367
|
$source_lists[] = array($item);
|
368
|
}
|
369
|
}
|
370
|
}
|
371
|
else {
|
372
|
foreach ($items as $delta => $item) {
|
373
|
if ($item['type'] == 'video') {
|
374
|
$source_lists[0][] = $item;
|
375
|
}
|
376
|
}
|
377
|
}
|
378
|
|
379
|
// Render each source list as an <video> element.
|
380
|
foreach ($source_lists as $delta => $sources) {
|
381
|
$width = $settings['width'];
|
382
|
$height = $settings['height'];
|
383
|
$element[$delta] = array(
|
384
|
'#theme' => 'file_entity_file_video',
|
385
|
'#files' => $sources,
|
386
|
'#controls' => $settings['controls'],
|
387
|
'#autoplay' => $settings['autoplay'],
|
388
|
'#loop' => $settings['loop'],
|
389
|
'#muted' => $settings['muted'],
|
390
|
'#width' => ($width && $height) ? $width : NULL,
|
391
|
'#height' => ($width && $height) ? $height : NULL,
|
392
|
);
|
393
|
}
|
394
|
}
|
395
|
|
396
|
return $element;
|
397
|
}
|