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
|
<?php
/**
* @file
* API extensions of Drupal core's file.inc.
*/
/**
* The {file_managed}.type value when the file type has not yet been determined.
*/
define('FILE_TYPE_NONE', 'undefined');
/**
* Returns information about file formatters from hook_file_formatter_info().
*
* @param string $formatter_type
* (optional) A file formatter type name. If ommitted, all file formatter
* will be returned.
*
* @return string|array
* Either a file formatter description, as provided by
* hook_file_formatter_info(), or an array of all existing file formatters,
* keyed by formatter type name.
*/
function file_info_formatter_types($formatter_type = NULL) {
$info = &drupal_static(__FUNCTION__);
if (!isset($info)) {
$info = module_invoke_all('file_formatter_info');
drupal_alter('file_formatter_info', $info);
uasort($info, '_file_entity_sort_weight_label');
}
if ($formatter_type) {
if (isset($info[$formatter_type])) {
return $info[$formatter_type];
}
}
else {
return $info;
}
}
/**
* Clears caches that are related to file entity.
*
* Clears all cached configuration related to file types, formatters, and
* display settings.
*/
function file_info_cache_clear() {
// Clear the CTools managed caches.
ctools_include('export');
ctools_export_load_object_reset('file_type');
ctools_export_load_object_reset('file_display');
// Clear the formatter type cache, managed by file_info_formatter_types().
drupal_static_reset('file_info_formatter_types');
// Clear file type caches.
drupal_static_reset('file_type_get_names');
}
/**
* Construct a drupal_render() style array from an array of loaded files.
*
* @param array $files
* An array of files as returned by file_load_multiple().
* @param string $view_mode
* View mode.
* @param int $weight
* An integer representing the weight of the first file in the list.
* @param string $langcode
* A string indicating the language field values are to be shown in. If no
* language is provided the current content language is used.
*
* @return array
* An array in the format expected by drupal_render().
*/
function file_view_multiple($files, $view_mode = 'full', $weight = 0, $langcode = NULL) {
if (empty($files)) {
return array();
}
field_attach_prepare_view('file', $files, $view_mode, $langcode);
entity_prepare_view('file', $files, $langcode);
$build = array();
foreach ($files as $file) {
$build['files'][$file->fid] = file_view($file, $view_mode, $langcode);
$build['files'][$file->fid]['#weight'] = $weight;
$weight++;
}
$build['files']['#sorted'] = TRUE;
return $build;
}
/**
* Generate an array for rendering the given file.
*
* @param object $file
* A file object.
* @param string $view_mode
* View mode.
* @param string $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*
* @return array
* An array as expected by drupal_render().
*/
function file_view($file, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// Populate $file->content with a render() array.
file_build_content($file, $view_mode, $langcode);
$build = $file->content;
// We don't need duplicate rendering info in $file->content.
unset($file->content);
$build += array(
'#theme' => 'file_entity',
'#file' => $file,
'#view_mode' => $view_mode,
'#language' => $langcode,
);
// Add contextual links for this file, except when the file is already being
// displayed on its own page. Modules may alter this behavior (for example,
// to restrict contextual links to certain view modes) by implementing
// hook_file_view_alter().
if (!empty($file->fid) && !($view_mode == 'full' && file_entity_is_page($file))) {
$build['#contextual_links']['file'] = array('file', array($file->fid));
}
// Allow modules to modify the structured file.
$type = 'file';
drupal_alter(array('file_view', 'entity_view'), $build, $type);
return $build;
}
/**
* Builds a structured array representing the file's content.
*
* @param object $file
* A file object.
* @param string $view_mode
* View mode, e.g. 'default', 'full', etc.
* @param string $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*/
function file_build_content($file, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// Remove previously built content, if exists.
$file->content = array();
// Build the actual file display.
// @todo Figure out how to clean this crap up.
$file->content['file'] = file_view_file($file, $view_mode, $langcode);
if (isset($file->content['file'])) {
if (isset($file->content['file']['#theme']) && $file->content['file']['#theme'] != 'file_link') {
unset($file->content['file']['#file']);
}
unset($file->content['file']['#view_mode']);
unset($file->content['file']['#language']);
}
else {
unset($file->content['file']);
}
// Build fields content.
// In case of a multiple view, file_view_multiple() already ran the
// 'prepare_view' step. An internal flag prevents the operation from running
// twice.
field_attach_prepare_view('file', array($file->fid => $file), $view_mode, $langcode);
entity_prepare_view('file', array($file->fid => $file), $langcode);
$file->content += field_attach_view('file', $file, $view_mode, $langcode);
$links = array();
$file->content['links'] = array(
'#theme' => 'links__file',
'#pre_render' => array('drupal_pre_render_links'),
'#attributes' => array('class' => array('links', 'inline')),
);
$file->content['links']['file'] = array(
'#theme' => 'links__file__file',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
// Allow modules to make their own additions to the file.
module_invoke_all('file_view', $file, $view_mode, $langcode);
module_invoke_all('entity_view', $file, 'file', $view_mode, $langcode);
}
/**
* Generate an array for rendering just the file portion of a file entity.
*
* @param object $file
* A file object.
* @param string|array $displays
* Can be either:
* - the name of a view mode;
* - or an array of custom display settings, as returned by file_displays().
* @param string $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*
* @return array
* An array as expected by drupal_render().
*/
function file_view_file($file, $displays = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// Prepare incoming display specifications.
if (is_string($displays)) {
$view_mode = $displays;
$displays = file_displays($file->type, $view_mode);
}
else {
$view_mode = '_custom_display';
}
drupal_alter('file_displays', $displays, $file, $view_mode);
_file_sort_array_by_weight($displays);
// Attempt to display the file with each of the possible displays. Stop after
// the first successful one. See file_displays() for details.
$element = NULL;
foreach ($displays as $formatter_type => $display) {
if (!empty($display['status'])) {
$formatter_info = file_info_formatter_types($formatter_type);
// Under normal circumstances, the UI prevents enabling formatters for
// incompatible MIME types. In case this was somehow circumvented (for
// example, a module updated its formatter definition without updating
// existing display settings), perform an extra check here.
if (isset($formatter_info['mime types'])) {
if (!file_entity_match_mimetypes($formatter_info['mime types'], $file->filemime)) {
continue;
}
}
if (isset($formatter_info['view callback']) && ($function = $formatter_info['view callback']) && function_exists($function)) {
$display['type'] = $formatter_type;
if (!empty($formatter_info['default settings'])) {
if (empty($display['settings'])) {
$display['settings'] = array();
}
$display['settings'] += $formatter_info['default settings'];
}
$element = $function($file, $display, $langcode);
if (isset($element)) {
break;
}
}
}
}
// As a last resort, fall back to showing a link to the file.
if (!isset($element)) {
$element = array(
'#theme' => 'file_link',
'#file' => $file,
);
}
// Add defaults and return the element.
$element += array(
'#file' => $file,
'#view_mode' => $view_mode,
'#language' => $langcode,
);
return $element;
}
/**
* @defgroup file_displays File displays API
* @{
* Functions to load and save information about file displays.
*/
/**
* Returns an array of displays to use for a file type in a given view mode.
*
* It is common for a site to be configured with broadly defined file types
* (e.g., 'video'), and to have different files of this type require different
* displays (for example, the code required to display a YouTube video is
* different than the code required to display a local QuickTime video).
* Therefore, the site administrator can configure multiple displays for a given
* file type. This function returns all of the displays that the administrator
* enabled for the given file type in the given view mode. file_view_file() then
* invokes each of these, and passes the specific file to display. Each display
* implementation can inspect the file, and either return a render array (if it
* is capable of displaying the file), or return nothing (if it is incapable of
* displaying the file). The first render array returned is the one used.
*
* @param string $file_type
* The type of file.
* @param string $view_mode
* The view mode.
*
* @return array
* An array keyed by the formatter type name. Each item in the array contains
* the following key/value pairs:
* - status: Whether this display is enabled. If not TRUE, file_view_file()
* skips over it.
* - weight: An integer that determines the order of precedence within the
* returned array. The lowest weight display capable of displaying the file
* is used.
* - settings: An array of key/value pairs specific to the formatter type. See
* hook_file_formatter_info() for details.
*
* @see hook_file_formatter_info()
* @see file_view_file()
*/
function file_displays($file_type, $view_mode) {
$cache = &drupal_static(__FUNCTION__, array());
// If the requested view mode isn't configured to use a custom display for its
// fields, then don't use a custom display for its file either.
if ($view_mode != 'default') {
$view_mode_settings = field_view_mode_settings('file', $file_type);
$view_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default';
}
if (!isset($cache[$file_type][$view_mode])) {
// Load the display configurations for the file type and view mode. If none
// exist for the view mode, use the default view mode.
$displays = file_displays_load($file_type, $view_mode, TRUE);
if (empty($displays) && $view_mode != 'default') {
$cache[$file_type][$view_mode] = file_displays($file_type, 'default');
}
else {
// Convert the display objects to arrays and remove unnecessary keys.
foreach ($displays as $formatter_name => $display) {
$displays[$formatter_name] = array_intersect_key((array) $display, drupal_map_assoc(array('status', 'weight', 'settings')));
}
$cache[$file_type][$view_mode] = $displays;
}
}
return $cache[$file_type][$view_mode];
}
/**
* Returns an array of {file_display} objects for the file type and view mode.
*/
function file_displays_load($file_type, $view_mode, $key_by_formatter_name = FALSE) {
ctools_include('export');
$display_names = array();
$prefix = $file_type . '__' . $view_mode . '__';
foreach (array_keys(file_info_formatter_types()) as $formatter_name) {
$display_names[] = $prefix . $formatter_name;
}
$displays = ctools_export_load_object('file_display', 'names', $display_names);
if ($key_by_formatter_name) {
$prefix_length = strlen($prefix);
$rekeyed_displays = array();
foreach ($displays as $name => $display) {
$rekeyed_displays[substr($name, $prefix_length)] = $display;
}
$displays = $rekeyed_displays;
}
return $displays;
}
/**
* Saves a {file_display} object to the database.
*/
function file_display_save($display) {
ctools_include('export');
ctools_export_crud_save('file_display', $display);
file_info_cache_clear();
}
/**
* Creates a new {file_display} object.
*/
function file_display_new($file_type, $view_mode, $formatter_name) {
ctools_include('export');
$display = ctools_export_crud_new('file_display');
file_info_cache_clear();
$display->name = implode('__', array($file_type, $view_mode, $formatter_name));
return $display;
}
/**
* @} End of "defgroup file_displays".
*/
/**
* @defgroup file_types File types API
* @{
* Functions to load and save information about file types.
*/
/**
* Load a file type configuration object.
*
* @param string $name
* The file type machine name to load.
*
* @return object
* The file type object, or FALSE if it does not exist.
*/
function file_type_load($name) {
ctools_include('export');
$type = ctools_export_crud_load('file_type', $name);
return isset($type) ? $type : FALSE;
}
/**
* Load multiple file type configuration objects.
*
* @param array $names
* An array of file type machine names to load.
*
* @return array
* An array of file type objects, keyed by machine name.
*/
function file_type_load_multiple(array $names) {
ctools_include('export');
return ctools_export_crud_load_multiple('file_type', $names);
}
/**
* Load all file type configuration objects.
*
* This includes all enabled and disabled file types.
*
* @param bool $reset
* If TRUE, the static cache of all file types will be flushed prior to
* loading. This can be important on listing pages where file types might
* have changed on the page load.
*
* @return array
* An array of file type objects, keyed by machine name.
*
* @see file_type_get_enabled_types()
* @see file_type_get_disabled_types()
*/
function file_type_load_all($reset = FALSE) {
ctools_include('export');
return ctools_export_crud_load_all('file_type', $reset);
}
/**
* Returns an array of enabled file types.
*/
function file_type_get_enabled_types() {
$types = file_type_load_all();
return array_filter($types, 'file_type_is_enabled');
}
/**
* Returns an array of disabled file types.
*/
function file_type_get_disabled_types() {
$types = file_type_load_all();
return array_filter($types, 'file_type_is_disabled');
}
/**
* Returns TRUE if a file type is enabled, FALSE otherwise.
*/
function file_type_is_enabled($type) {
return empty($type->disabled);
}
/**
* Returns TRUE if a file type is disabled, FALSE otherwise.
*/
function file_type_is_disabled($type) {
return !empty($type->disabled);
}
/**
* Returns an array of valid file extensions.
*/
function file_type_get_valid_extensions($type) {
include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
$mapping = file_mimetype_mapping();
$type_extensions = array();
$type_ext_keys = array();
if (!empty($type->mimetypes)) {
foreach ($mapping['mimetypes'] as $ext_key => $mimetype) {
if (file_entity_match_mimetypes($mimetype, $type->mimetypes)) {
$type_ext_keys[] = $ext_key;
}
}
if ($type_ext_keys) {
$type_extensions = array_intersect($mapping['extensions'], $type_ext_keys);
$type_extensions = array_keys($type_extensions);
}
}
return $type_extensions;
}
/**
* Updates an existing file type or creates a new one.
*
* This function can be called on its own, or via the CTools exportables
* 'save callback' for {file_type} objects.
*/
function file_type_save($type) {
// Get the old type object, so we now can issue the correct insert/update
// queries.
if (!empty($type->old_type) && $type->old_type != $type->type) {
$rename_bundle = TRUE;
$old_type = file_type_load($type->old_type);
}
else {
$rename_bundle = FALSE;
$old_type = file_type_load($type->type);
}
// The type and label fields are required, but description is optional.
if (!isset($type->description)) {
$type->description = '';
}
$fields = array(
'type' => $type->type,
'label' => $type->label,
'description' => $type->description,
'mimetypes' => serialize($type->mimetypes),
);
// Update an existing type object, whether with a modified 'type' property or
// not.
if ($old_type) {
if ($old_type->export_type & EXPORT_IN_DATABASE) {
db_update('file_type')
->fields($fields)
->condition('type', $old_type->type)
->execute();
}
else {
db_insert('file_type')
->fields($fields)
->execute();
}
if ($rename_bundle) {
field_attach_rename_bundle('file', $old_type->type, $type->type);
}
module_invoke_all('file_type_update', $type);
$status = SAVED_UPDATED;
}
// Insert a new type object.
else {
db_insert('file_type')
->fields($fields)
->execute();
field_attach_create_bundle('file', $type->type);
module_invoke_all('file_type_insert', $type);
$status = SAVED_NEW;
}
// Clear the necessary caches.
file_info_cache_clear();
// Ensure the type has the correct export_type in case the $type parameter
// continues to be used by the calling function after this function completes.
if (empty($type->export_type)) {
$type->export_type = 0;
}
$type->export_type |= EXPORT_IN_DATABASE;
return $status;
}
/**
* Deletes a file type from the database.
*
* This function can be called on its own, or via the CTools exportables
* 'delete callback' for {file_type} objects.
*
* @param object|string $type
* Either a loaded file type object or the machine-name of the type.
*/
function file_type_delete($type) {
$type = is_string($type) ? file_type_load($type) : $type;
db_delete('file_type')
->condition('type', $type->type)
->execute();
// Remove this type from CToolS status variable.
$status = variable_get('default_file_type', array());
unset($status[$type->type]);
variable_set('default_file_type', $status);
file_info_cache_clear();
// After deleting from the database, check if the type still exists as a
// code-provided default type. If not, consider the type fully deleted and
// invoke the needed hooks.
if (!file_type_load($type->type)) {
field_attach_delete_bundle('file', $type->type);
module_invoke_all('file_type_delete', $type);
}
}
/**
* Enable a file type.
*
* @param string $type
* Type of the file_type to disable
*/
function file_type_enable($type) {
ctools_include('export');
ctools_export_crud_enable('file_type', $type);
}
/**
* Disable a file type.
*
* @param string $type
* Type of the file_type to disable
*/
function file_type_disable($type) {
ctools_include('export');
ctools_export_crud_disable('file_type', $type);
}
/**
* Determines file type for a given file.
*
* @param object $file
* File object.
*
* @return string
* Machine name of file type that should be used for given file.
*/
function file_get_type($file) {
$types = module_invoke_all('file_type', $file);
drupal_alter('file_type', $types, $file);
return empty($types) ? NULL : reset($types);
}
/**
* @} End of "defgroup file_types".
*/
/**
* Sorts an array by weight.
*
* Helper function to sort an array by the value of each item's 'weight' key,
* while preserving relative order of items that have equal weight.
*/
function _file_sort_array_by_weight(&$a) {
$i = 0;
foreach ($a as $key => $item) {
if (!isset($a[$key]['weight'])) {
$a[$key]['weight'] = 0;
}
$original_weight[$key] = $a[$key]['weight'];
$a[$key]['weight'] += $i / 1000;
$i++;
}
uasort($a, 'drupal_sort_weight');
foreach ($a as $key => $item) {
$a[$key]['weight'] = $original_weight[$key];
}
}
/**
* User sort function to sort by weight, then label/name.
*/
function _file_entity_sort_weight_label($a, $b) {
$a_weight = isset($a['weight']) ? $a['weight'] : 0;
$b_weight = isset($b['weight']) ? $b['weight'] : 0;
if ($a_weight == $b_weight) {
$a_label = isset($a['label']) ? $a['label'] : '';
$b_label = isset($b['label']) ? $b['label'] : '';
return strcasecmp($a_label, $b_label);
}
else {
return $a_weight < $b_weight ? -1 : 1;
}
}
/**
* Returns a file object which can be passed to file_save().
*
* @param string $uri
* A string containing the URI, path, or filename.
* @param bool $use_existing
* (Optional) If TRUE and there's an existing file in the {file_managed}
* table with the passed in URI, then that file object is returned.
* Otherwise, a new file object is returned. Default is TRUE.
*
* @return object|bool
* A file object, or FALSE on error.
*
* @todo This should probably be named
* file_load_by_uri($uri, $create_if_not_exists).
* @todo Remove this function when http://drupal.org/node/685818 is fixed.
*/
function file_uri_to_object($uri, $use_existing = TRUE) {
$file = FALSE;
$uri = file_stream_wrapper_uri_normalize($uri);
if ($use_existing) {
// We should always attempt to re-use a file if possible.
$files = entity_load('file', FALSE, array('uri' => $uri));
$file = !empty($files) ? reset($files) : FALSE;
}
if (empty($file)) {
$file = new stdClass();
$file->uid = $GLOBALS['user']->uid;
$file->filename = drupal_basename($uri);
$file->uri = $uri;
$file->filemime = file_get_mimetype($uri);
// This is gagged because some uris will not support it.
$file->filesize = @filesize($uri);
$file->timestamp = REQUEST_TIME;
$file->status = FILE_STATUS_PERMANENT;
}
return $file;
}
/**
* Delete multiple files.
*
* Unlike core's file_delete(), this function does not care about file usage
* or skip on invalid URIs. Just deletes the damn file like it should.
*
* @param array $fids
* An array of file IDs.
*/
function file_delete_multiple(array $fids) {
$transaction = db_transaction();
if (!empty($fids) && $files = file_load_multiple($fids)) {
try {
foreach ($files as $fid => $file) {
module_invoke_all('file_delete', $file);
module_invoke_all('entity_delete', $file, 'file');
// Skip calling field_attach_delete as file_entity_file_delete()
// does this.
// field_attach_delete('file', $file);
// Remove this file from the search index if needed.
// This code is implemented in file_entity module rather than in search
// module, because node module is implementing search module's API,
// not the other way around.
if (module_exists('search')) {
search_reindex($fid, 'file');
}
// Make sure the file is deleted before removing its row from the
// database, so UIs can still find the file in the database.
if (file_valid_uri($file->uri)) {
file_unmanaged_delete($file->uri);
}
}
db_delete('file_managed')
->condition('fid', $fids, 'IN')
->execute();
db_delete('file_usage')
->condition('fid', $fids, 'IN')
->execute();
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('file', $e);
throw $e;
}
// Clear the page and block and file_load_multiple caches.
entity_get_controller('file')->resetCache();
}
}
|