-
Notifications
You must be signed in to change notification settings - Fork 381
/
file.module
3428 lines (3133 loc) · 117 KB
/
file.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
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Defines a "managed_file" Form API field and a "file" field for Field module.
*/
/**
* Modules should return this value from hook_file_access() to allow
* access to a file.
*/
define('FILE_ACCESS_ALLOW', 'allow');
/**
* Modules should return this value from hook_file_access() to deny
* access to a file.
*/
define('FILE_ACCESS_DENY', 'deny');
/**
* Modules should return this value from hook_file_access() to not affect
* file access.
*/
define('FILE_ACCESS_IGNORE', NULL);
/**
* The {file_managed}.type value when the file type has not yet been determined.
*/
define('FILE_TYPE_NONE', 'undefined');
/**
* File types constant for user-defined file types.
*/
define('FILE_TYPE_STORAGE_NORMAL', 1);
/**
* File type constant for file types that override module-defined presets.
*/
define('FILE_TYPE_STORAGE_OVERRIDE', 2);
/**
* File type constant for module-defined file types.
*/
define('FILE_TYPE_STORAGE_DEFAULT', 4);
// Load all Field module hooks for File.
require_once BACKDROP_ROOT . '/core/modules/file/file.field.inc';
/**
* Implements hook_hook_info().
*/
function file_hook_info() {
$hooks = array(
// File API hooks
'file_copy',
'file_move',
'file_validate',
'file_type_info',
'file_type_info_alter',
'file_formatter_info',
'file_formatter_info_alter',
'file_download_headers_alter',
// File access
'file_download',
'file_download_access',
'file_download_access_alter',
'file_access',
'file_access_alter',
'query_file_access_alter',
// File operation hooks
'file_load',
'file_presave',
'file_insert',
'file_update',
'file_delete',
'file_view',
'file_view_alter',
// Miscellaneous hooks
'file_mimetype_mapping_alter',
'file_url_alter',
'file_operations',
// Stream wrappers
'stream_wrappers',
'stream_wrappers_alter',
);
return array_fill_keys($hooks, array('group' => 'file'));
}
/**
* Implements hook_action_info().
*/
function file_action_info() {
$actions['file_delete'] = array(
'label' => t('Delete file(s)'),
'type' => 'file',
'callback' => 'file_delete_action',
'file' => 'file.actions.inc',
);
return $actions;
}
/**
* Implements hook_autoload_info().
*/
function file_autoload_info() {
return array(
'File' => 'file.entity.inc',
'FileStorageController' => 'file.entity.inc',
// Views handlers.
'views_handler_argument_file_fid' => 'views/views_handler_argument_file_fid.inc',
'views_handler_argument_file_type' => 'views/views_handler_argument_file_type.inc',
'views_handler_field_file' => 'views/views_handler_field_file.inc',
'views_handler_field_file_link_edit' => 'views/views_handler_field_file_link_edit.inc',
'views_handler_field_file_link_delete' => 'views/views_handler_field_file_link_delete.inc',
'views_handler_field_file_extension' => 'views/views_handler_field_file_extension.inc',
'views_handler_field_file_icon' => 'views/views_handler_field_file_icon.inc',
'views_handler_field_file_filemime' => 'views/views_handler_field_file_filemime.inc',
'views_handler_field_file_uri' => 'views/views_handler_field_file_uri.inc',
'views_handler_field_file_status' => 'views/views_handler_field_file_status.inc',
'views_handler_field_file_type' => 'views/views_handler_field_file_type.inc',
'views_handler_filter_file_status' => 'views/views_handler_filter_file_status.inc',
'views_handler_filter_file_type' => 'views/views_handler_filter_file_type.inc',
'views_handler_field_file_link' => 'views/views_handler_field_file_link.inc',
'views_handler_field_file_rendered' => 'views/views_handler_field_file_rendered.inc',
);
}
/**
* Implements hook_permission().
*/
function file_permission() {
$permissions = array(
'access file overview' => array(
'title' => t('Access the manage files overview'),
'description' => t('The <a href="@url">manage files overview page</a> shows all files uploaded to the site.', array('@url' => url('admin/content/files'))),
),
'bypass file access' => array(
'title' => t('Bypass file access control'),
'description' => t('View, edit and delete all files regardless of permission restrictions.'),
'restrict access' => TRUE,
'warning' => t('Bypasses all other file permissions.'),
),
'administer file types' => array(
'title' => t('Administer file types'),
'restrict access' => TRUE,
'warning' => t('Potentially-dangerous filetypes can be allowed on the site (e.g.: <code>.zip</code>, <code>.exe</code>, etc.).'),
),
'create files' => array(
'title' => t('Add and upload new files'),
),
'view own private files' => array(
'title' => t('View own private files'),
),
'view own files' => array(
'title' => t('View own files'),
),
'view private files' => array(
'title' => t('View private files'),
'restrict access' => TRUE,
'warning' => t('Viewing files not available to the public can lead to a breach of private/sensitive information.'),
),
'view files' => array(
'title' => t('View files'),
),
'manage files' => array(
'title' => t('Manage or replace any file'),
'description' => t('Allow managing of files from the files overview page.'),
'restrict access' => TRUE,
'warning' => t('Private files can be made publicly-available.'),
),
'delete files' => array(
'title' => t('Delete any file'),
'description' => t('Allow deleting files directly from the manage files overview page.'),
'restrict access' => TRUE,
'warning' => t('Deleting files can cause irreversible data loss.'),
),
);
// Add description for the 'View file details' and 'View own private file
// details' permissions to show which stream wrappers they apply to.
$wrappers = file_get_public_and_private_stream_wrapper_names();
$wrappers += array('public' => array(t('None')), 'private' => array(t('None')));
$permissions['view files']['description'] = t('Includes the following stream wrappers: %wrappers.', array('%wrappers' => implode(', ', $wrappers['public'])));
$permissions['view own private files']['description'] = t('Includes the following stream wrappers: %wrappers.', array('%wrappers' => implode(', ', $wrappers['private'])));
// Generate standard file permissions for all applicable file types.
foreach (file_permissions_get_configured_types() as $type) {
$permissions += file_list_permissions($type);
}
return $permissions;
}
/**
* Implements hook_menu().
*/
function file_menu() {
$items = array();
// File Configuration
$items['admin/structure/file-types'] = array(
'title' => 'File types',
'description' => 'Manage settings for the type of files used on your site.',
'page callback' => 'file_list_types_page',
'access arguments' => array('administer file types'),
'file' => 'file.admin.inc',
);
// File Configuration
$items['admin/structure/file-types/list'] = array(
'title' => 'File types',
'description' => 'Manage settings for the type of files used on your site.',
'page callback' => 'file_list_types_page',
'access arguments' => array('administer file types'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'file.admin.inc',
);
$items['admin/structure/file-types/settings'] = array(
'title' => 'File settings',
'description' => 'Configure allowed file extensions and the file upload wizard.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_settings_form'),
'access arguments' => array('administer file types'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'file' => 'file.admin.inc',
);
$items['admin/structure/file-types/add'] = array(
'title' => 'Add file type',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_type_form'),
'access arguments' => array('administer file types'),
'type' => MENU_LOCAL_ACTION,
'file' => 'file.admin.inc',
);
$items['admin/structure/file-types/manage/%file_type'] = array(
'title' => 'Manage file types',
'description' => 'Manage settings for the type of files used on your site.',
);
$items['admin/structure/file-types/manage/%file_type/revert'] = array(
'title' => 'Revert',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_type_revert_confirm', 4),
'access arguments' => array('administer file types'),
'file' => 'file.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/structure/file-types/manage/%file_type/enable'] = array(
'title' => 'Enable file type',
'page callback' => 'file_type_toggle_enable',
'page arguments' => array(4, '1'),
'access arguments' => array('administer file types'),
'file' => 'file.admin.inc',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/structure/file-types/manage/%file_type/disable'] = array(
'title' => 'Disable file type',
'page callback' => 'file_type_toggle_enable',
'page arguments' => array(4, '0'),
'access arguments' => array('administer file types'),
'file' => 'file.admin.inc',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/structure/file-types/manage/%file_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_type_delete_confirm', 4),
'access arguments' => array('administer file types'),
'file' => 'file.admin.inc',
'type' => MENU_CALLBACK,
);
// Attach a "Manage file display" tab to each file type in the same way that
// Field UI attaches "Manage fields" and "Manage display" tabs. Note that
// Field UI does not have to be enabled; we're just using the same IA pattern
// here for attaching the "Manage file display" page.
$entity_info = entity_get_info('file');
foreach ($entity_info['bundles'] as $file_type => $bundle_info) {
if (isset($bundle_info['admin'])) {
// Get the base path and access.
$path = $bundle_info['admin']['path'];
$access = array_intersect_key($bundle_info['admin'], backdrop_map_assoc(array('access callback', 'access arguments')));
$access += array(
'access callback' => 'user_access',
'access arguments' => array('administer file types'),
);
// The file type must be passed to the page callbacks. It might be
// configured as a wildcard (multiple file types sharing the same menu
// router path).
$file_type_argument = isset($bundle_info['admin']['bundle argument']) ? $bundle_info['admin']['bundle argument'] : $file_type;
$items[$path] = array(
'title' => 'Configure file type',
'title callback' => 'file_type_get_name',
'title arguments' => array(4),
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_type_form', $file_type_argument),
'file' => 'file.admin.inc',
) + $access;
// Add the 'File type settings' tab.
$items["$path/configure"] = array(
'title' => 'Configure',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Add the 'Manage file display' tab.
$items["$path/file-display"] = array(
'title' => 'Manage file display',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_display_form', $file_type_argument, 'default'),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
'file' => 'file.admin.inc',
) + $access;
// Add a secondary tab for each view mode.
$weight = 0;
$view_modes = array('default' => array('label' => t('Default'))) + $entity_info['view modes'];
foreach ($view_modes as $view_mode => $view_mode_info) {
$items["$path/file-display/$view_mode"] = array(
'title' => $view_mode_info['label'],
'page arguments' => array('file_display_form', $file_type_argument, $view_mode),
'type' => ($view_mode == 'default' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK),
'weight' => ($view_mode == 'default' ? -10 : $weight++),
'file' => 'file.admin.inc',
// View modes for which the 'custom settings' flag isn't TRUE are
// disabled via this access callback. This needs to extend, rather
// than override normal $access rules.
'access callback' => '_file_view_mode_menu_access',
'access arguments' => array_merge(array($file_type_argument, $view_mode, $access['access callback']), $access['access arguments']),
);
}
}
}
$items['file/ajax'] = array(
'page callback' => 'file_ajax_upload',
'delivery callback' => 'ajax_deliver',
'access arguments' => array('access content'),
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
// @deprecated. Remove in Backdrop 2.0.
$items['file/progress'] = array(
'page callback' => 'file_ajax_progress',
'delivery callback' => 'backdrop_json_deliver',
'access arguments' => array('access content'),
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
$items['file/add'] = array(
'title' => 'Add a file',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_add_form'),
'access arguments' => array('create files'),
'type' => MENU_LOCAL_ACTION,
'file' => 'file.pages.inc',
'weight' => -10,
);
$items['file/%file'] = array(
'title callback' => 'entity_label',
'title arguments' => array('file', 1),
// The page callback also invokes backdrop_set_title() in case
// the menu router's title is overridden by a menu link.
'page callback' => 'file_view_page',
'page arguments' => array(1),
'access callback' => 'file_access',
'access arguments' => array('view', 1),
'file' => 'file.pages.inc',
);
$items['file/%file/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['file/%file/usage'] = array(
'title' => 'Usage',
'page callback' => 'file_usage_page',
'page arguments' => array(1),
'access callback' => 'file_access',
'access arguments' => array('update', 1),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE,
'file' => 'file.pages.inc',
);
$items['file/%file/download'] = array(
'title' => 'Download',
'page callback' => 'file_download_page',
'page arguments' => array(1),
'access callback' => 'file_access',
'access arguments' => array('download', 1),
'file' => 'file.pages.inc',
'type' => MENU_CALLBACK,
);
$items['file/%file/manage'] = array(
'title' => 'Manage',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_manage_form', 1),
'access callback' => 'file_access',
'access arguments' => array('update', 1),
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'file.pages.inc',
);
$items['file/%file/delete'] = array(
'title' => 'Delete',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_delete_form', 1),
'access callback' => 'file_access',
'access arguments' => array('delete', 1),
'weight' => 1,
'type' => MENU_CALLBACK,
'file' => 'file.pages.inc',
);
$items['admin/content/files/delete'] = array(
'title' => 'Confirm deleting multiple files',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_multiple_delete_confirm'),
'access arguments' => array('delete files'),
'file' => 'file.pages.inc',
);
$items['admin/structure/file-types/classify'] = array(
'title' => 'Classify file types',
'page callback' => 'backdrop_get_form',
'page arguments' => array('file_type_classify_confirm'),
'access arguments' => array('access administration pages'),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'file' => 'file.admin.inc',
);
return $items;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function file_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Add action link for 'file/add' on 'admin/content/files' page.
if ($root_path == 'admin/content/files') {
$item = menu_get_item('file/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
/**
* Implements hook_entity_info().
*/
function file_entity_info() {
$bundles = array();
file_type_cache_reset();
foreach (file_type_get_types() as $type) {
$bundles[$type->type] = array(
'label' => $type->name,
'admin' => array(
'path' => 'admin/structure/file-types/manage/%file_type',
'real path' => 'admin/structure/file-types/manage/' . str_replace('_', '-', $type->type),
'bundle argument' => 4,
'access arguments' => array('administer file types'),
),
);
}
$entity_info = array(
'file' => array(
'label' => t('File'),
'bundle label' => t('Type'),
'base table' => 'file_managed',
'controller class' => 'FileStorageController',
'fieldable' => TRUE,
'entity class' => 'File',
'entity keys' => array(
'id' => 'fid',
'bundle' => 'type'
),
'bundle keys' => array(
'bundle' => 'type'
),
'bundles' => $bundles,
'uri callback' => 'file_uri',
'view modes' => array(
'teaser' => array(
'label' => t('Teaser'),
),
'full' => array(
'label' => t('Full content'),
),
'preview' => array(
'label' => t('Preview'),
'custom settings' => TRUE,
),
),
'static cache' => FALSE,
),
);
// If the cache table has been created, then enable entity caching.
if (db_table_exists('cache_entity_file')) {
$entity_info['file']['entity cache'] = TRUE;
$entity_info['file']['field cache'] = FALSE;
}
return $entity_info;
}
/**
* URI callback for file entities.
*/
function file_uri($file) {
return $file->uri();
}
/**
* Implements hook_element_info().
*
* The managed file element may be used anywhere in Backdrop.
*/
function file_element_info() {
$file_path = backdrop_get_path('module', 'file');
$types['managed_file'] = array(
'#input' => TRUE,
'#process' => array('file_managed_file_process'),
'#value_callback' => 'file_managed_file_value',
'#element_validate' => array('file_managed_file_validate'),
'#pre_render' => array('file_managed_file_pre_render'),
'#theme' => 'file_managed_file',
'#theme_wrappers' => array('form_element'),
'#progress_indicator' => 'throbber',
'#progress_message' => NULL,
'#upload_validators' => array(),
'#upload_location' => NULL,
'#browser_view' => FALSE,
'#size' => 22,
'#extended' => FALSE,
'#attached' => array(
'css' => array($file_path . '/css/file.admin.css'),
'js' => array($file_path . '/js/file.js'),
),
);
return $types;
}
/**
* Implements hook_field_extra_fields().
*
* Adds 'file' as an extra field, so that its display and form component can be
* weighted relative to the fields that are added to file entity bundles.
*/
function file_field_extra_fields() {
$info = array();
if ($file_type_names = file_type_get_names()) {
foreach ($file_type_names as $type => $name) {
$info['file'][$type]['form']['filename'] = array(
'label' => t('Name'),
'description' => t('Name'),
'weight' => -10,
);
$info['file'][$type]['form']['preview'] = array(
'label' => t('File'),
'description' => t('File preview'),
'weight' => -5,
);
$info['file'][$type]['display']['file'] = array(
'label' => t('File'),
'description' => t('File display'),
'weight' => 0,
);
}
}
return $info;
}
/**
* Implements hook_file_formatter_info().
*/
function file_file_formatter_info() {
$formatters = array();
// Allow file field formatters to be reused for displaying the file entity's
// file pseudo-field.
foreach (field_info_formatter_types() as $key => $formatter) {
if (array_intersect($formatter['field types'], array('file', 'image'))) {
$key = 'file_field_' . $key;
$formatters[$key] = array(
'label' => $formatter['label'],
'description' => !empty($formatter['description']) ? $formatter['description'] : '',
'view callback' => 'file_file_formatter_file_field_view',
);
if (!empty($formatter['settings'])) {
$formatters[$key] += array(
'default settings' => $formatter['settings'],
'settings callback' => 'file_file_formatter_file_field_settings',
);
}
if (!empty($formatter['file formatter'])) {
$formatters[$key] += $formatter['file formatter'];
}
}
}
// Add a simple file formatter for displaying an image in a chosen style.
if (module_exists('image')) {
$formatters['file_image'] = array(
'label' => t('Image'),
'default settings' => array(
'image_style' => '',
),
'view callback' => 'file_file_formatter_file_image_view',
'settings callback' => 'file_file_formatter_file_image_settings',
'hidden' => TRUE,
'mime types' => array('image/*'),
);
}
return $formatters;
}
/**
* Implements hook_file_formatter_FORMATTER_view().
*
* This function provides a bridge to the field formatter API, so that file
* field formatters can be reused for displaying the file entity's file
* pseudo-field.
*/
function file_file_formatter_file_field_view($file, $display, $langcode) {
if (strpos($display['type'], 'file_field_') === 0) {
$field_formatter_type = substr($display['type'], strlen('file_field_'));
$field_formatter_info = field_info_formatter_types($field_formatter_type);
if (isset($field_formatter_info['module'])) {
// Set $display['type'] to what hook_field_formatter_*() expects.
$display['type'] = $field_formatter_type;
// Allow any attribute overrides (e.g. from the Media module) to be
// respected.
$item = (array) $file;
if (!empty($file->override['attributes'])) {
$item = array_merge($item, $file->override['attributes']);
}
// Set $items to what file field formatters expect. See file_field_load(),
// and note that, here, $file is already a fully loaded entity.
$items = array($item);
// Invoke hook_field_formatter_prepare_view() and
// hook_field_formatter_view(). Note that we are reusing field formatter
// functions, but we are not displaying a Field API field, so we set
// $field and $instance accordingly, and do not invoke
// hook_field_prepare_view(). This assumes that the formatter functions do
// not rely on $field or $instance. A module that implements formatter
// functions that rely on $field or $instance (and therefore, can only be
// used for real fields) can prevent this formatter from being used on the
// pseudo-field by removing it within hook_file_formatter_info_alter().
$field = $instance = NULL;
if (($function = ($field_formatter_info['module'] . '_field_formatter_prepare_view')) && function_exists($function)) {
$fid = $file->fid;
// hook_field_formatter_prepare_view() alters $items by reference.
$grouped_items = array($fid => &$items);
$function('file', array($fid => $file), $field, array($fid => $instance), $langcode, $grouped_items, array($fid => $display));
}
if (($function = ($field_formatter_info['module'] . '_field_formatter_view')) && function_exists($function)) {
$element = $function('file', $file, $field, $instance, $langcode, $items, $display);
// We passed the file as $items[0], so return the corresponding element.
if (isset($element[0])) {
return $element[0];
}
}
}
}
}
/**
* Implements hook_file_formatter_FORMATTER_settings().
*
* This function provides a bridge to the field formatter API, so that file
* field formatters can be reused for displaying the file entity's file
* pseudo-field.
*/
function file_file_formatter_file_field_settings($form, &$form_state, $settings, $formatter_type, $file_type, $view_mode) {
if (strpos($formatter_type, 'file_field_') === 0) {
$field_formatter_type = substr($formatter_type, strlen('file_field_'));
$field_formatter_info = field_info_formatter_types($field_formatter_type);
// Invoke hook_field_formatter_settings_form(). We are reusing field
// formatter functions, but we are not working with a Field API field, so
// set $field accordingly. Unfortunately, the API is for $settings to be
// transferred via the $instance parameter, so we must mock it.
if (isset($field_formatter_info['module']) && ($function = ($field_formatter_info['module'] . '_field_formatter_settings_form')) && function_exists($function)) {
$field = NULL;
$mock_instance = array(
'display' => array(
$view_mode => array(
'type' => $field_formatter_type,
'settings' => $settings,
),
),
'entity_type' => 'file',
'bundle' => $file_type,
);
return $function($field, $mock_instance, $view_mode, $form, $form_state);
}
}
}
/**
* Implements hook_file_formatter_FORMATTER_view().
*
* Returns a backdrop_render() array to display an image of the chosen style.
*
* This formatter is only capable of displaying local images. If the passed in
* file is either not local or not an image, nothing is returned, so that
* file_view_file() can try another formatter.
*/
function file_file_formatter_file_image_view($file, $display, $langcode) {
// Prevent PHP notices when trying to read empty files.
// @see http://drupal.org/node/681042
if (!$file->filesize) {
return;
}
// Do not bother proceeding if this file does not have an image mime type.
if (file_get_mimetype_type($file) != 'image') {
return;
}
if (file_file_is_readable($file)) {
// We don't sanitize here.
// @see http://drupal.org/node/1553094#comment-6257382
// Theme function will take care of escaping.
if (!isset($file->metadata)) {
$file->metadata = array();
}
$file->metadata += array('width' => NULL, 'height' => NULL);
$replace_options = array(
'clear' => TRUE,
'sanitize' => FALSE,
);
if (!empty($display['settings']['image_style'])) {
$element = array(
'#theme' => 'image_style',
'#style_name' => $display['settings']['image_style'],
'#path' => $file->uri,
'#width' => isset($file->override['attributes']['width']) ? $file->override['attributes']['width'] : $file->metadata['width'],
'#height' => isset($file->override['attributes']['height']) ? $file->override['attributes']['height'] : $file->metadata['height'],
'#alt' => token_replace($display['settings']['alt'], array('file' => $file), $replace_options),
'#title' => token_replace($display['settings']['title'], array('file' => $file), $replace_options),
);
}
else {
$element = array(
'#theme' => 'image',
'#path' => $file->uri,
'#width' => isset($file->override['attributes']['width']) ? $file->override['attributes']['width'] : $file->metadata['width'],
'#height' => isset($file->override['attributes']['height']) ? $file->override['attributes']['height'] : $file->metadata['height'],
'#alt' => token_replace($display['settings']['alt'], array('file' => $file), $replace_options),
'#title' => token_replace($display['settings']['title'], array('file' => $file), $replace_options),
);
}
return $element;
}
}
/**
* Check if a file entity is readable or not.
*
* @param object $file
* A file entity object from file_load().
*
* @return boolean
* TRUE if the file is using a readable stream wrapper, or FALSE otherwise.
*/
function file_file_is_readable($file) {
$scheme = file_uri_scheme($file->uri);
$wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_READ);
return !empty($wrappers[$scheme]);
}
/**
* Implements hook_file_formatter_FORMATTER_settings().
*
* Returns form elements for configuring the 'file_image' formatter.
*/
function file_file_formatter_file_image_settings($form, &$form_state, $settings) {
$element = array();
$element['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#options' => image_style_options(FALSE),
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
);
// For image files we allow the alt attribute (required in HTML).
$element['alt'] = array(
'#title' => t('Alt attribute'),
'#description' => t('The text to use as value for the <em>img</em> tag <em>alt</em> attribute.'),
'#type' => 'textfield',
'#default_value' => $settings['alt'],
);
// Allow the setting of the title attribute.
$element['title'] = array(
'#title' => t('Title attribute'),
'#description' => t('The text to use as value for the <em>img</em> tag <em>title</em> attribute.'),
'#type' => 'textfield',
'#default_value' => $settings['title'],
);
$element['alt']['#description'] .= t('This field supports tokens.');
$element['title']['#description'] .= t('This field supports tokens.');
$element['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => array('file'),
'#dialog' => TRUE,
);
return $element;
}
/**
* Implements hook_theme().
*/
function file_theme() {
$base = array(
'file' => 'file.theme.inc',
);
return array(
'file_entity' => array(
'render element' => 'elements',
'template' => 'templates/file',
) + $base,
'file_link' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL, 'attributes' => array()),
) + $base,
'file_icon' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL, 'alt' => ''),
) + $base,
'file_managed_file' => array(
'render element' => 'element',
) + $base,
'file_widget' => array(
'render element' => 'element',
) + $base,
'file_widget_multiple' => array(
'render element' => 'element',
) + $base,
'file_formatter_table' => array(
'variables' => array('items' => NULL),
) + $base,
'file_upload_help' => array(
'variables' => array('description' => NULL, 'upload_validators' => NULL),
) + $base,
'file_type_overview' => array(
'variables' => array('name' => NULL, 'description' => NULL),
) + $base,
'file_display_order' => array(
'render element' => 'element',
) + $base,
'file_download_link' => array(
'variables' => array('file' => NULL, 'icon_directory' => NULL, 'text' => NULL),
) + $base,
'file_audio' => array(
'variables' => array(
'files' => array(),
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'preload' => NULL,
),
) + $base,
'file_video' => array(
'variables' => array(
'files' => array(),
'controls' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'muted' => FALSE,
'width' => NULL,
'height' => NULL,
'preload' => NULL,
),
) + $base,
);
}
/**
* Returns whether the current page is the full page view of the passed-in file.
*
* @param $file
* A file object.
*/
function file_is_page($file) {
$page_file = menu_get_object('file', 1);
return (!empty($page_file) ? $page_file->fid == $file->fid : FALSE);
}
/**
* Process variables for file.tpl.php
*
* The $variables array contains the following arguments:
* - $file
* - $view_mode
*
* @see file.tpl.php
*/
function template_preprocess_file_entity(&$variables) {
$view_mode = $variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['file'] = $variables['elements']['#file'];
$file = $variables['file'];
$variables['id'] = backdrop_html_id('file-'. $file->fid);
$variables['date'] = format_date($file->timestamp);
$account = user_load($file->uid);
$variables['name'] = theme('username', array('account' => $account));
$uri = entity_uri('file', $file);
$variables['file_url'] = url($uri['path'], $uri['options']);
$label = entity_label('file', $file);
$variables['label'] = check_plain($label);
$variables['page'] = $view_mode == 'full' && file_is_page($file);
// Hide the file name from being displayed until we can figure out a better
// way to control this. We cannot simply not output the title since
// contextual links require $title_suffix to be output in the template.
// @see http://drupal.org/node/1245266
if (!$variables['page']) {
$variables['title_attributes']['class'][] = 'element-invisible';
}
// Flatten the file object's member fields.
$variables = array_merge((array) $file, $variables);
// Helpful $content variable for templates.
$variables += array('content' => array());
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Make the field variables available with the appropriate language.
field_attach_preprocess('file', $file, $variables['content'], $variables);
// Attach the file object to the content element.
$variables['content']['file']['#file'] = $file;
// Gather file classes.
$variables['classes'][] = backdrop_html_class('file-' . $file->type);
if ($file->status != FILE_STATUS_PERMANENT) {
$variables['classes'][] = 'file-temporary';
}
// Change the 'file-entity' class into 'file'
if ($variables['classes'][0] == 'file-entity') {
$variables['classes'][0] = 'file';
}
// Clean up name so there are no underscores.
$variables['theme_hook_suggestions'][] = 'file__' . $file->type;
$variables['theme_hook_suggestions'][] = 'file__' . $file->type . '__' . $view_mode;
$variables['theme_hook_suggestions'][] = 'file__' . str_replace(array('/', '-'), array('__', '_'), $file->filemime);
$variables['theme_hook_suggestions'][] = 'file__' . str_replace(array('/', '-'), array('__', '_'), $file->filemime) . '__' . $view_mode;
$variables['theme_hook_suggestions'][] = 'file__' . $file->fid;
$variables['theme_hook_suggestions'][] = 'file__' . $file->fid . '__' . $view_mode;
}
/**
* Implements hook_views_api().
*/
function file_views_api() {
return array(
'api' => '3.0',
'path' => backdrop_get_path('module', 'file') . '/views',
);
}
/**
* Implements hook_file_download().
*
* This function takes an extra parameter $field_type so that it may
* be re-used by other File-like modules, such as Image.
*/
function file_file_download($uri, $field_type = 'file') {
global $user;