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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
|
<?php
/**
* @file
* Install, update and uninstall functions for the file_entity module.
*/
/**
* Implements hook_schema().
*/
function file_entity_schema() {
$schema['file_type'] = array(
'description' => 'Stores the settings for file types.',
'fields' => array(
'type' => array(
'description' => 'The machine name of the file type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'label' => array(
'description' => 'The human readable name of the file type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'translatable' => TRUE,
),
'description' => array(
'description' => 'A brief description of this file type.',
'type' => 'text',
'not null' => TRUE,
'size' => 'medium',
'translatable' => TRUE,
),
'mimetypes' => array(
'description' => 'Mimetypes mapped to this file type.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
),
),
'primary key' => array('type'),
'export' => array(
'key' => 'type',
'key name' => 'Type',
'primary key' => 'type',
'default hook' => 'file_default_types',
'identifier' => 'file_type',
'export type string' => 'ctools_type',
'save callback' => 'file_type_save',
'delete callback' => 'file_type_delete',
'api' => array(
'owner' => 'file_entity',
'api' => 'file_type',
'minimum_version' => 1,
'current_version' => 1,
),
),
);
$schema['file_display'] = array(
'description' => 'Stores configuration options for file displays.',
'fields' => array(
// @todo Can be refactored as a compond primary key after
// http://drupal.org/node/924236 is implemented.
'name' => array(
'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
),
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
),
),
'primary key' => array('name'),
// Exportable support via CTools.
'export' => array(
'key' => 'name',
'key name' => 'Name',
'primary key' => 'name',
// The {file_display}.status field is used to control whether the display
// is active in the display chain. CTools-level disabling is something
// different, and it's not yet clear how to interpret it for file
// displays. Until that's figured out, prevent CTools-level disabling.
'can disable' => FALSE,
'default hook' => 'file_default_displays',
'identifier' => 'file_display',
'api' => array(
'owner' => 'file_entity',
'api' => 'file_default_displays',
'minimum_version' => 1,
'current_version' => 1,
),
),
);
$schema['file_metadata'] = array(
'description' => 'Cache images dimensions.',
'fields' => array(
'fid' => array(
'description' => 'The {file_managed}.fid of the metadata.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'name' => array(
'description' => "The name of the metadata (e.g. 'width').",
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'value' => array(
'description' => "The value of the metadata (e.g. '200px').",
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
),
),
'primary key' => array('fid', 'name'),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
),
);
return $schema;
}
/**
* Implements hook_schema_alter().
*/
function file_entity_schema_alter(&$schema) {
$schema['file_managed']['fields']['type'] = array(
'description' => 'The type of this file.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
// If the FILE_TYPE_NONE constant ever changes, then change the value here
// too, and add an update function to deal with existing records. The
// constant isn't used here, because there may be cases where this function
// runs without the module file loaded.
'default' => 'undefined',
);
$schema['file_managed']['indexes']['file_type'] = array('type');
}
/**
* Implements hook_install().
*/
function file_entity_install() {
$schema = array();
file_entity_schema_alter($schema);
$spec = $schema['file_managed']['fields']['type'];
$indexes_new = array('indexes' => $schema['file_managed']['indexes']);
// If another module (e.g., Media) had added a {file_managed}.type field,
// then change it to the expected specification. Otherwise, add the field.
if (db_field_exists('file_managed', 'type')) {
// db_change_field() will fail if any records have type=NULL, so update
// them to the new default value.
db_update('file_managed')->fields(array('type' => $spec['default']))->isNull('type')->execute();
// Indexes using a field being changed must be dropped prior to calling
// db_change_field(). However, the database API doesn't provide a way to do
// this without knowing what the old indexes are. Therefore, it is the
// responsibility of the module that added them to drop them prior to
// allowing this module to be installed.
db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
}
else {
db_add_field('file_managed', 'type', $spec, $indexes_new);
}
// Set permissions.
$roles = user_roles();
foreach ($roles as $rid => $role) {
user_role_grant_permissions($rid, array('view files'));
}
// Create the title and alt text fields.
_file_entity_create_alt_title_fields();
// Configure default pathauto variables if it is currently installed.
if (module_exists('pathauto')) {
variable_set('pathauto_file_pattern', 'file/[file:name]');
}
// Classify existing files according to the currently defined file types.
// Queue all files to be classified during cron runs using the Queue API.
$queue = DrupalQueue::get('file_entity_type_determine');
$result = db_query('SELECT fid FROM {file_managed}');
foreach ($result as $record) {
$queue->createItem($record->fid);
}
// Warn users that existing files will not have a file type until the queue
// has been processed.
if ($queue->numberOfItems()) {
drupal_set_message(t('Existing files must be classified according to the currently defined file types. These files have been queued for processing and will have their file type determined during cron runs.'));
}
}
/**
* Implements hook_uninstall().
*/
function file_entity_uninstall() {
drupal_load('module', 'file_entity');
foreach (file_type_load_all(TRUE) as $type) {
file_type_delete($type);
}
// Remove the added column to the core {file_managed} table.
db_drop_field('file_managed', 'type');
// Remove variables.
variable_del('file_entity_max_filesize');
variable_del('file_entity_default_allowed_extensions');
variable_del('file_entity_alt');
variable_del('file_entity_title');
variable_del('file_entity_allow_insecure_download');
variable_del('file_entity_file_upload_wizard_skip_file_type');
variable_del('file_entity_file_upload_wizard_skip_scheme');
variable_del('file_entity_file_upload_wizard_skip_fields');
}
/**
* Create the {file_display} database table.
*/
function file_entity_update_7000() {
if (db_table_exists('file_display')) {
return t('The table {file_display} already exists.');
}
$schema['file_display'] = array(
'description' => 'Stores configuration options for file displays.',
'fields' => array(
'name' => array(
'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
),
'status' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
),
),
'primary key' => array('name'),
);
db_create_table('file_display', $schema['file_display']);
}
/**
* Move file display configurations.
*
* Move file display configurations from the 'file_displays' variable to the
* {file_display} database table.
*/
function file_entity_update_7001() {
$file_displays = variable_get('file_displays');
if (!empty($file_displays)) {
foreach ($file_displays as $file_type => $file_type_displays) {
if (!empty($file_type_displays)) {
foreach ($file_type_displays as $view_mode => $view_mode_displays) {
if (!empty($view_mode_displays)) {
foreach ($view_mode_displays as $formatter_name => $display) {
if (!empty($display)) {
db_merge('file_display')
->key(array(
'name' => implode('__', array($file_type, $view_mode, $formatter_name)),
))
->fields(array(
'status' => isset($display['status']) ? $display['status'] : 0,
'weight' => isset($display['weight']) ? $display['weight'] : 0,
'settings' => isset($display['settings']) ? serialize($display['settings']) : NULL,
))
->execute();
}
}
}
}
}
}
}
variable_del('file_displays');
}
/**
* Empty update function to trigger a theme registry rebuild.
*/
function file_entity_update_7100() { }
/**
* Update all files with empty types to use the first part of filemime.
*
* For example, an png image with filemime 'image/png' will be assigned a file
* type of 'image'.
*/
function file_entity_update_7101() {
db_update('file_managed')
->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)")
->condition('type', '')
->execute();
}
/**
* Empty update function to trigger an entity cache rebuild.
*/
function file_entity_update_7102() {
}
/**
* Empty update function.
*/
function file_entity_update_7103() {
}
/**
* Assign view file permission when updating without the Media module.
*/
function file_entity_update_7104() {
if (!module_exists('media')) {
$roles = user_roles(FALSE, 'view file');
if (empty($roles)) {
// Set permissions.
$roles = user_roles();
foreach ($roles as $rid => $role) {
// Do not use user_role_grant_permission() since it relies on
// hook_permission(), which will not run for file entity module if it
// is disabled or the permission is renamed or removed.
db_merge('role_permission')
->fields(array(
'rid' => $rid,
'permission' => 'view file',
'module' => 'file_entity',
))
->condition('rid', $rid)
->condition('permission', 'view file')
->execute();
}
}
}
}
/**
* Create the {image_dimensions} database table.
*/
function file_entity_update_7200() {
if (db_table_exists('image_dimensions')) {
return t('The table {image_dimensions} already exists.');
}
$schema['image_dimensions'] = array(
'description' => 'Cache images dimensions.',
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'height' => array(
'description' => 'The height of the image in pixels.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'width' => array(
'description' => 'The width of the image in pixels..',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('fid'),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
),
);
db_create_table('image_dimensions', $schema['image_dimensions']);
}
/**
* Add the {file_type}, {file_type_mimetypes} tables.
*/
function file_entity_update_7201() {
$schema = array(
'description' => 'Stores the settings for file types.',
'fields' => array(
'type' => array(
'description' => 'The machine name of the file type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'label' => array(
'description' => 'The human readable name of the file type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'translatable' => TRUE,
),
'description' => array(
'description' => 'A brief description of this file type.',
'type' => 'text',
'not null' => TRUE,
'size' => 'medium',
'translatable' => TRUE,
),
),
'primary key' => array('type'),
'export' => array(
'key' => 'type',
'key name' => 'Type',
'primary key' => 'type',
'default hook' => 'file_default_types',
'identifier' => 'file_type',
'export type string' => 'ctools_type',
'subrecords callback' => 'file_type_load_subrecords',
'save callback' => 'file_type_save',
'delete callback' => 'file_type_delete',
'api' => array(
'owner' => 'file_entity',
'api' => 'file_type',
'minimum_version' => 1,
'current_version' => 1,
),
),
);
if (!db_table_exists('file_type')) {
db_create_table('file_type', $schema);
}
$schema = array(
'description' => 'Maps mimetypes to file types.',
'fields' => array(
'type' => array(
'description' => 'The machine name of the file type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'mimetype' => array(
'description' => 'Mimetypes mapped to this file type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'indexes' => array(
'file_type' => array('type'),
'file_type_mimetype' => array('mimetype'),
),
);
if (!db_table_exists('file_type_mimetypes')) {
db_create_table('file_type_mimetypes', $schema);
}
}
/**
* Update empty {file_managed}.type records to 'undefined'.
*
* Drupal 7.8 disallows empty string as the value for a bundle key, so update
* empty {file_managed}.type records to 'undefined' instead.
*/
function file_entity_update_7202() {
db_update('file_managed')
// Using 'undefined' instead of FILE_TYPE_NONE, because update functions can
// run for disabled modules.
->fields(array('type' => 'undefined'))
->condition('type', '')
->execute();
}
/**
* Update permission names.
*/
function file_entity_update_7203() {
$permissions = array(
'view file' => 'view files',
'edit file' => 'edit any files',
);
foreach ($permissions as $old => $new) {
db_update('role_permission')
->fields(array('permission' => $new))
->condition('permission', $old)
->execute();
}
}
/**
* Add title and alt text to image file types.
*/
function file_entity_update_7204() {
_file_entity_create_alt_title_fields();
}
/**
* Function to create the title and alt text fields and instances.
*/
function _file_entity_create_alt_title_fields() {
$t = get_t();
// Create the alt text field and instance.
// Define the alt text field.
$alt_text_field = array(
'active' => '1',
'cardinality' => '1',
'deleted' => '0',
'entity_types' => array(),
'field_name' => 'field_file_image_alt_text',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'module' => 'text',
'settings' => array(
'max_length' => '255',
),
'translatable' => '0',
'type' => 'text',
);
// As long as the alt text field doesn't already exist create it.
if (!field_info_field($alt_text_field['field_name'])) {
field_create_field($alt_text_field);
}
// Define the alt text instance.
$alt_text_instance = array(
'bundle' => 'image',
'default_value' => NULL,
'deleted' => '0',
'description' => $t('Alternative text is used by screen readers, search engines, and when the image cannot be loaded. By adding alt text you improve accessibility and search engine optimization.'),
'display' => array(
'default' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'full' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'preview' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'file',
'field_name' => 'field_file_image_alt_text',
'label' => 'Alt Text',
'required' => 0,
'settings' => array(
'text_processing' => '0',
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'size' => '60',
),
'type' => 'text_textfield',
'weight' => '-4',
),
);
// For sites that updated from Media 1.x, continue to provide these deprecated
// view modes.
// @see http://drupal.org/node/1051090
if (variable_get('media__show_deprecated_view_modes')) {
$alt_text_instance['display'] += array(
'media_link' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'media_original' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
);
}
// As long as the alt text instance doesn't already exist create it.
if (!field_info_instance($alt_text_instance['entity_type'], $alt_text_instance['field_name'], $alt_text_instance['bundle'])) {
field_create_instance($alt_text_instance);
}
// Create the title text field and instance.
// Define the title text field.
$title_text_field = array(
'active' => '1',
'cardinality' => '1',
'deleted' => '0',
'entity_types' => array(),
'field_name' => 'field_file_image_title_text',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'module' => 'text',
'settings' => array(
'max_length' => '255',
),
'translatable' => '0',
'type' => 'text',
);
// As long as the title text field doesn't exist create it.
if (!field_info_field($title_text_field['field_name'])) {
field_create_field($title_text_field);
}
// Define the title text instance.
$title_text_instance = array(
'bundle' => 'image',
'default_value' => NULL,
'deleted' => '0',
'description' => $t('Title text is used in the tool tip when a user hovers their mouse over the image. Adding title text makes it easier to understand the context of an image and improves usability.'),
'display' => array(
'default' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 1,
),
'full' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'preview' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'file',
'field_name' => 'field_file_image_title_text',
'label' => 'Title Text',
'required' => 0,
'settings' => array(
'text_processing' => '0',
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'size' => '60',
),
'type' => 'text_textfield',
'weight' => '-3',
),
);
// For sites that updated from Media 1.x, continue to provide these deprecated
// view modes.
// @see http://drupal.org/node/1051090
if (variable_get('media__show_deprecated_view_modes')) {
$title_text_instance['display'] += array(
'media_link' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'media_original' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
);
}
// As long as the title text instance doesn't already exist create it.
if (!field_info_instance($title_text_instance['entity_type'], $title_text_instance['field_name'], $title_text_instance['bundle'])) {
field_create_instance($title_text_instance);
}
}
/**
* Fix broken indexes caused by incorrect index definitions in update 7201.
*/
function file_entity_update_7205() {
// Drop broken file type indexes. These may not exist if the broken version
// of update 7201 was never run.
if (db_index_exists('file_type_mimetypes', 0)) {
db_drop_index('file_type_mimetypes', 0);
}
if (db_index_exists('file_type_mimetypes', 1)) {
db_drop_index('file_type_mimetypes', 1);
}
// Add file type indexes. These may already exist if the fixed version of
// update 7201 was run.
if (!db_index_exists('file_type_mimetypes', 'file_type')) {
db_add_index('file_type_mimetypes', 'file_type', array('type'));
}
if (!db_index_exists('file_type_mimetypes', 'file_type_mimetype')) {
db_add_index('file_type_mimetypes', 'file_type_mimetype', array('mimetype'));
}
}
/**
* Configure default pathauto variables if it is currently installed.
*/
function file_entity_update_7206() {
if (module_exists('pathauto')) {
variable_set('pathauto_file_pattern', 'file/[file:name]');
}
}
/**
* Remove the administration files limit variable.
*/
function file_entity_update_7207() {
variable_del('file_entity_admin_files_limit');
}
/**
* Add expanded file type permissions to roles with existing file permissions.
*/
function file_entity_update_7208() {
foreach (array('edit own files', 'edit any files', 'delete own files', 'delete any files', 'download own files', 'download any files') as $old_permission) {
$roles = user_roles(FALSE, $old_permission);
foreach ($roles as $rid => $name) {
$new_permissions = array();
foreach (file_type_get_enabled_types() as $type => $info) {
switch ($old_permission) {
case 'edit own files':
$new_permissions[] = 'edit own ' . $type . ' files';
break;
case 'edit any files':
$new_permissions[] = 'edit any ' . $type . ' files';
break;
case 'delete own files':
$new_permissions[] = 'delete own ' . $type . ' files';
break;
case 'delete any files':
$new_permissions[] = 'delete any ' . $type . ' files';
break;
case 'download own files':
$new_permissions[] = 'download own ' . $type . ' files';
break;
case 'download any files':
$new_permissions[] = 'download any ' . $type . ' files';
break;
}
}
if (!empty($new_permissions)) {
// Grant new permissions for the role.
foreach ($new_permissions as $name) {
db_merge('role_permission')
->key(array(
'rid' => $rid,
'permission' => $name,
))
->fields(array(
'module' => 'file_entity',
))
->execute();
}
}
// Remove old permission from the role.
db_delete('role_permission')
->condition('rid', $rid)
->condition('permission', $old_permission)
->condition('module', 'file_entity')
->execute();
}
}
}
/**
* Remove the {file_type_streams} table if it exists.
*/
function file_entity_update_7209() {
if (db_table_exists('file_type_streams')) {
db_drop_table('file_type_streams');
}
}
/**
* Merge MIME types into the {file_type} table.
*/
function file_entity_update_7210() {
// Add the new mimetypes field if it doesn't already exist.
if (!db_field_exists('file_type', 'mimetypes')) {
$field = array(
'description' => 'Mimetypes mapped to this file type.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
);
db_add_field('file_type', 'mimetypes', $field);
}
// Migrate any existing MIME type information into {file_type}.
if (db_table_exists('file_type_mimetypes')) {
module_load_include('inc', 'file_entity', 'file_entity.file_api');
$types = file_type_load_all(TRUE);
foreach ($types as $type) {
$mimetypes = db_select('file_type_mimetypes', 'ftm')
->fields('ftm', array('mimetype'))
->condition('type', $type->type)
->execute()->fetchCol();
if (!empty($mimetypes)) {
$type->mimetypes = $mimetypes;
file_type_save($type);
}
}
// Remove {file_type_mimetypes} after the information is migrated.
db_drop_table('file_type_mimetypes');
}
}
/**
* Create the {file_metadata} table.
*/
function file_entity_update_7211() {
$schema = array(
'description' => 'Stores file metadata in a key/value store.',
'fields' => array(
'fid' => array(
'description' => 'The {file_managed}.fid of the metadata.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'name' => array(
'description' => "The name of the metadata (e.g. 'width').",
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'value' => array(
'description' => "The value of the metadata (e.g. '200px').",
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
),
),
'primary key' => array('fid', 'name'),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
),
);
db_create_table('file_metadata', $schema);
}
/**
* Migrate the image_dimensions table to the new file_metadata table.
*/
function file_entity_update_7212(&$sandbox) {
if (!db_table_exists('image_dimensions')) {
return;
}
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_fid'] = 0;
$sandbox['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {image_dimensions}')->fetchField();
}
$results = db_query_range("SELECT fid, width, height FROM {image_dimensions} WHERE fid > :fid ORDER BY fid ASC", 0, 20, array(':fid' => $sandbox['current_fid']))->fetchAllAssoc('fid');
// Clear any existing records in the metadata table in case they exist
// because we only want to do one insert.
if (!empty($results)) {
db_delete('file_metadata')
->condition('fid', array_keys($results), 'IN')
->condition(db_or()
->condition('name', 'width')
->condition('name', 'height')
)
->execute();
}
$values = array();
foreach ($results as $result) {
foreach (array('width', 'height') as $key) {
$values[] = array(
'fid' => $result->fid,
'name' => $key,
'value' => serialize((int) $result->{$key}),
);
}
$sandbox['progress'] += count($results);
$sandbox['current_fid'] = $result->fid;
}
if (!empty($values)) {
$query = db_insert('file_metadata');
$query->fields(array('fid', 'name', 'value'));
foreach ($values as $value) {
$query->values($value);
}
$query->execute();
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
if ($sandbox['#finished'] >= 1) {
db_drop_table('image_dimensions');
}
}
/**
* Update default alt text and title image field descriptions.
*/
function file_entity_update_7213() {
if ($title_text_instance = field_info_instance('file', 'field_file_image_title_text', 'image')) {
if ($title_text_instance['description'] == 'Title text attribute') {
$title_text_instance['description'] = t('Title text is used in the tool tip when a user hovers their mouse over the image. Adding title text makes it easier to understand the context of an image and improves usability.');
field_update_instance($title_text_instance);
}
}
if ($alt_text_instance = field_info_instance('file', 'field_file_image_alt_text', 'image')) {
if ($alt_text_instance['description'] == '') {
$alt_text_instance['description'] = t('Alternative text is used by screen readers, search engines, and when the image cannot be loaded. By adding alt text you improve accessibility and search engine optimization.');
field_update_instance($alt_text_instance);
}
}
}
/**
* Fix the default value in {file_managed}.type to match the field schema.
*/
function file_entity_update_7214() {
db_drop_index('file_managed', 'file_type');
db_change_field('file_managed', 'type', 'type', array(
'description' => 'The type of this file.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => 'undefined',
));
db_add_index('file_managed', 'file_type', array('type'));
}
/**
* Fix the {file_metadata}.fid schema.
*/
function file_entity_update_7215() {
// When changing a primary key serial field to an int, we need to add a
// temporary index to make this update work.
// @see https://drupal.org/node/190027
db_add_index('file_metadata', 'temp', array('fid'));
db_drop_primary_key('file_metadata');
db_change_field('file_metadata', 'fid', 'fid', array(
'description' => 'The {file_managed}.fid of the metadata.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
));
db_add_primary_key('file_metadata', array('fid', 'name'));
db_drop_index('file_metadata', 'temp');
}
/**
* This update has been removed and will not run.
*/
function file_entity_update_7216() {
// This update function previously saved default file displays into the
// database. It has been removed due to reported problems and is better
// addressed by adding support for ctools default content to features.
}
|