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
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
|
<?php
/**
* @file
* Install file for media_gallery. Includes field and instance definitions.
*/
/**
* Implements hook_enable().
*/
function media_gallery_enable() {
// Work around http://drupal.org/node/727876. (See also
// http://drupal.org/node/882364.)
//field_associate_fields('taxonomy');
}
/**
* Implements hook_install().
*/
function media_gallery_install() {
// Adjust bundle settings for the media gallery content type.
$bundle_settings = field_bundle_settings('node', 'media_gallery');
// Enable the gallery block and full view modes.
$bundle_settings['view_modes']['media_gallery_block']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['full']['custom_settings'] = TRUE;
// Adjust the "Add media link" extra field so that it only displays on the
// full node view, with the correct weight relative to the other fields. In
// particular, we put it directly after the 'media_gallery_description'
// field; see _media_gallery_controlled_instances().
$bundle_settings['extra_fields']['display']['add_media_link']['full']['weight'] = 1;
$bundle_settings['extra_fields']['display']['add_media_link']['full']['visible'] = TRUE;
$bundle_settings['extra_fields']['display']['add_media_link']['default']['visible'] = FALSE;
$bundle_settings['extra_fields']['display']['add_media_link']['teaser']['visible'] = FALSE;
$bundle_settings['extra_fields']['display']['add_media_link']['media_gallery_block']['visible'] = FALSE;
// _field_extra_fields_pre_render() requires an explicit 'weight' value for
// all extra field settings stored in the database.
foreach ($bundle_settings['extra_fields']['display']['add_media_link'] as $name => $view_mode) {
$bundle_settings['extra_fields']['display']['add_media_link'][$name]['weight'] = 1;
}
// Save the new bundle settings.
field_bundle_settings('node', 'media_gallery', $bundle_settings);
// Enable custom display settings for gallery context view modes for file
// types that can be in a gallery.
foreach (array('audio', 'image', 'video') as $bundle) {
$bundle_settings = field_bundle_settings('file', $bundle);
foreach (media_gallery_file_view_modes() as $view_mode => $label) {
$bundle_settings['view_modes'][$view_mode]['custom_settings'] = TRUE;
}
field_bundle_settings('file', $bundle, $bundle_settings);
}
// Clear caches so that our implementation of hook_image_default_styles() is
// correctly used when all the fields created below need it to be.
// @todo There should obviously be a cleaner way to do this.
cache_clear_all('*', 'cache', TRUE);
drupal_static_reset('image_styles');
drupal_static_reset('image_effects');
if (module_exists('styles') && function_exists('styles_style_flush')) {
styles_style_flush();
}
// Add the taxonomy vocabulary for media gallery collections.
$vocabulary = media_gallery_create_taxonomy_vocab();
// Make sure the standard 'field_tags' field exists.
_media_gallery_ensure_field_tags();
// Create fields (but not instances yet) for media_gallery nodes and
// for the gallery collection vocabulary.
foreach (_media_gallery_controlled_fields() as $field) {
_media_gallery_ensure_field($field);
}
// Attach fields to gallery_collection taxonomy terms.
foreach (_media_gallery_controlled_instances('taxonomy_term') as $instance) {
_media_gallery_ensure_instance($instance);
}
// Now that the gallery_collection vocabulary exists and has fields attached,
// create an "All galleries" term for galleries to belong to by default.
media_gallery_create_taxonomy_term($vocabulary);
// Attach fields to the media gallery node type (including a term reference
// for the default collection).
foreach (_media_gallery_controlled_instances('node') as $instance) {
_media_gallery_ensure_instance($instance);
}
// Make sure all media bundles have the instances we expect.
_media_gallery_ensure_media_instances();
// Set variables for the media gallery node type.
variable_set('node_submitted_media_gallery', FALSE);
variable_set('node_options_media_gallery', array('status'));
variable_set('comment_media_gallery', 0);
}
/**
* Implements hook_requirements().
*/
function media_gallery_requirements() {
$requirements = array();
// If this module is part of an install profile, its requirements will be
// checked before the field system is available. The rest of this function is
// unneeded anyway in that case, so bail out here to avoid fatal errors.
if (!module_exists('field')) {
return $requirements;
}
$t = get_t();
$required_fields = _media_gallery_controlled_fields();
// In addition to the fields we control, we also need the standard field_tags
// that most sites will have gotten from their install profile.
$required_fields['field_tags'] = array('type' => 'taxonomy_term_reference');
foreach ($required_fields as $field_name => $field_definition) {
$field = field_info_field($field_name);
// If the field doesn't exist, we will create it on install.
if (!$field) {
continue;
}
// Between Media Gallery beta2 and beta3, field definitions were changed
// from list_number to list_float, to keep up with Drupal core changes.
// list_update_7001() handles the updating of existing fields, but
// update.php checks all requirements prior to running any update function.
if ($field['type'] == 'list_number' && $field_definition['type'] == 'list_float') {
continue;
}
if ($field['type'] != $field_definition['type']) {
$requirements['existing_field_' . $field_name] = array(
'description' => $t("%field_name already exists and is not of type %type. Installation cannot continue. Please remove this field or change its type.", array('%field_name' => $field_name, '%type' => $field_definition['type'])),
'severity' => REQUIREMENT_ERROR,
);
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function media_gallery_schema() {
$schema['media_gallery_weight'] = array(
'description' => 'The weight of media galleries within a given collection.',
'fields' => array(
'tid' => array(
'description' => 'The taxonomy term id corresponding to a media gallery collection.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'nid' => array(
'description' => 'The node id of the media gallery.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'weight' => array(
'description' => 'The weight of the media gallery within the collection.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('tid', 'nid'),
);
return $schema;
}
/**
* Returns definitions for fields this module both creates and deletes.
*/
function _media_gallery_controlled_fields() {
$fields = array(
// The media items that make up the gallery.
'media_gallery_file' => array(
'field_name' => 'media_gallery_file',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'type' => 'file',
),
// The gallery description.
'media_gallery_description' => array(
'field_name' => 'media_gallery_description',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'text_long',
),
// How to format the gallery (if links go to lightbox or node display).
'media_gallery_format' => array(
'field_name' => 'media_gallery_format',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_text',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_format_values',
),
),
// Whether or not the lightbox should show extra fields.
'media_gallery_lightbox_extras' => array(
'field_name' => 'media_gallery_lightbox_extras',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_boolean',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_lightbox_extras_values',
),
),
// How many columns of thumbnails to show.
'media_gallery_columns' => array(
'field_name' => 'media_gallery_columns',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_float',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_columns_values',
),
),
// How many rows of thumbnails to show.
'media_gallery_rows' => array(
'field_name' => 'media_gallery_rows',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'number_integer',
),
// Whether to show title/license on hover or below thumbnail.
'media_gallery_image_info_where' => array(
'field_name' => 'media_gallery_image_info_where',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_text',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_image_info_placement_values',
),
),
// Whether to show a "Download original image" checkbox.
'media_gallery_allow_download' => array(
'field_name' => 'media_gallery_allow_download',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_boolean',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_allow_download_values',
),
),
// Whether to expose a block for this gallery.
'media_gallery_expose_block' => array(
'field_name' => 'media_gallery_expose_block',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_boolean',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_expose_block_values',
),
),
// How many columns of thumbnails to show in the block.
'media_gallery_block_columns' => array(
'field_name' => 'media_gallery_block_columns',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_float',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_block_columns_values',
),
),
// How many rows of thumbnails to show in the block.
'media_gallery_block_rows' => array(
'field_name' => 'media_gallery_block_rows',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'number_integer',
),
'media_gallery_collection' => array(
'field_name' => 'media_gallery_collection',
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'gallery_collections',
'parent' => 0,
),
),
),
),
// Fields to create on media items.
'media_description' => array(
'field_name' => 'media_description',
'locked' => TRUE,
'type' => 'text_long',
),
'media_title' => array(
'field_name' => 'media_title',
'locked' => TRUE,
'type' => 'text',
),
'field_license' => array(
'field_name' => 'field_license',
'locked' => TRUE,
'settings' => array(
'allowed_values_function' => '_media_gallery_get_field_license_values',
),
'type' => 'list_text',
'active' => TRUE,
'cardinality' => 1,
),
);
return $fields;
}
/**
* Returns definitions for instances this modules both creates and deletes.
*
* @param $group
* Optional. The group of instances to return. May be 'node' or
* 'taxonomy_term'. If omitted, returns all instances.
*
* @return
* A structured array of instances.
*/
function _media_gallery_controlled_instances($group = NULL) {
$t = get_t();
$node_instances = array(
// The gallery description.
'media_gallery_description' => array(
'field_name' => 'media_gallery_description',
'label' => $t('Description'),
'widget' => array(
'type' => 'text_textarea',
'settings' => array('rows' => 4),
),
'settings' => array(
'text_processing' => 1,
),
'display' => array(
'default' => array(
'type' => 'text_default',
'label' => 'hidden',
'weight' => 0,
),
'full' => array(
'type' => 'text_default',
'label' => 'hidden',
'weight' => 0,
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
'weight' => 0,
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
'weight' => 0,
),
),
),
'media_gallery_file' => array(
'field_name' => 'media_gallery_file',
'label' => $t('Gallery media'),
'widget' => array(
'type' => 'media_generic',
'settings' => array(
// Eventually other media types will be allowed.
'allowed_types' => array('audio' => 'audio', 'image' => 'image', 'video' => 'video'),
'allowed_schemes' => array('public' => 'public'),
),
),
'display' => array(
'default' => array(
'type' => 'media_gallery',
'settings' => array('file_view_mode' => 'media_gallery_thumbnail'),
'label' => 'hidden',
'weight' => 2,
),
'full' => array(
'type' => 'media_gallery',
'settings' => array('file_view_mode' => 'media_gallery_thumbnail'),
'label' => 'hidden',
'weight' => 2,
),
'teaser' => array(
'type' => 'media_gallery',
'settings' => array('file_view_mode' => 'media_gallery_collection_thumbnail'),
'label' => 'hidden',
'weight' => 2,
),
'media_gallery_block' => array(
'type' => 'media_gallery',
'settings' => array('file_view_mode' => 'media_gallery_block_thumbnail'),
'label' => 'hidden',
'weight' => 2,
),
),
),
// How to format the gallery (if links go to lightbox or node display).
'media_gallery_format' => array(
'field_name' => 'media_gallery_format',
'label' => $t('Gallery format'),
'required' => TRUE,
'default_value' => array(array('value' => 'lightbox')),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
'widget' => array(
'type' => 'options_buttons',
),
),
// Whether to show a "Exclude title and description" checkbox.
'media_gallery_lightbox_extras' => array(
'field_name' => 'media_gallery_lightbox_extras',
'label' => 'Lightbox title and description',
'description' => $t('Show title and description'),
'default_value' => array(array('value' => 0)),
'widget' => array(
'type' => 'options_onoff',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// How many columns to show.
'media_gallery_columns' => array(
'field_name' => 'media_gallery_columns',
'label' => $t('Number of columns'),
'default_value' => array(array('value' => 4)),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// How many rows to show.
'media_gallery_rows' => array(
'field_name' => 'media_gallery_rows',
'label' => $t('Number of rows'),
'default_value' => array(array('value' => 3)),
'settings' => array(
'min' => '1',
),
'required' => TRUE,
'widget' => array(
'type' => 'number',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// Whether to show title/license on hover or below thumbnail.
'media_gallery_image_info_where' => array(
'field_name' => 'media_gallery_image_info_where',
'label' => $t('Media information'),
'required' => TRUE,
'default_value' => array(array('value' => 'hover')),
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// Whether to show a "Download original image" checkbox.
'media_gallery_allow_download' => array(
'field_name' => 'media_gallery_allow_download',
'label' => $t('Allow downloading of the original image'),
'description' => $t('Display a "download original image" link'),
'default_value' => array(array('value' => 1)),
'widget' => array(
'type' => 'options_onoff',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// Whether to expose a block for this gallery.
'media_gallery_expose_block' => array(
'field_name' => 'media_gallery_expose_block',
'label' => $t('Create a block of most recently added media'),
'default_value' => array(array('value' => 0)),
'widget' => array(
'type' => 'options_onoff',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// How many columns to show in the block.
'media_gallery_block_columns' => array(
'field_name' => 'media_gallery_block_columns',
'label' => $t('Number of columns'),
'default_value' => array(array('value' => 2)),
'required' => TRUE,
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// How many rows to show in the block.
'media_gallery_block_rows' => array(
'field_name' => 'media_gallery_block_rows',
'label' => $t('Number of rows'),
'default_value' => array(array('value' => 3)),
'required' => TRUE,
'settings' => array(
'min' => 1,
),
'widget' => array(
'type' => 'number',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
// The 'collection' tag field on media gallery nodes.
'media_gallery_collection' => array(
'field_name' => 'media_gallery_collection',
'label' => $t('Gallery collection'),
'default_value' => array(
array(
'tid' => variable_get('media_gallery_default_collection_tid'),
),
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
),
);
foreach ($node_instances as &$instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = 'media_gallery';
}
unset($instance);
$instances = array_intersect_key($node_instances, array_flip(array('media_gallery_columns', 'media_gallery_rows', 'media_gallery_image_info_where')));
$instances['media_gallery_image_info_where']['label'] = $t('Gallery information');
$instances['field_license'] = array(
'field_name' => 'field_license',
'label' => $t('Default license settings'),
'required' => TRUE,
'default_value' => array(
array('value' => 'nothing'),
),
'description' => $t('Choose a default <a href="http://creativecommons.org">@cc_link</a> license for all Gallery media. Later you can change the license for each piece of media.', array('@cc_link' => 'Creative Commons')),
'weight' => 14,
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
);
foreach ($instances as $key => $instance) {
// Since we are re-using fields which are defined for the node, we need to
// remove any additional view modes which don't belong to avoid E_NOTICE errors.
$instance['display'] = array_intersect_key($instance['display'], array_flip(array('default', 'full')));
$instance['entity_type'] = 'taxonomy_term';
$instance['bundle'] = 'gallery_collections';
$taxonomy_instances['taxo_term_' . $key] = $instance;
}
switch ($group) {
case 'node':
return $node_instances;
case 'taxonomy_term':
return $taxonomy_instances;
default:
return $node_instances + $taxonomy_instances;
}
}
/**
* Create a field, unless it exists already.
*
* Note that it's not necessary to check field type here, as that's done in the
* requirements step.
*
* @param $field
* The field definition.
*/
function _media_gallery_ensure_field($field) {
$existing_field = field_read_field($field['field_name'], array('include_inactive' => TRUE));
if (empty($existing_field)) {
field_create_field($field);
}
}
function _media_gallery_ensure_instance($instance) {
$existing_instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
if (empty($existing_instance)) {
field_create_instance($instance);
}
}
/**
* Returns definitions for instances this module requires on media bundles.
*/
function _media_required_instances() {
$t = get_t();
$media_instances = array(
'media_title' => array(
'field_name' => 'media_title',
'label' => $t('Title'),
'display' => array(
'default' => array('type' => 'hidden'),
'media_gallery_thumbnail' => array('type' => 'text_default', 'label' => 'hidden'),
'media_gallery_lightbox' => array('type' => 'text_default', 'label' => 'hidden'),
'media_gallery_detail' => array('type' => 'text_default', 'label' => 'hidden'),
),
),
'media_description' => array(
'field_name' => 'media_description',
'label' => $t('Description'),
'widget' => array(
'type' => 'text_textarea',
'settings' => array('rows' => 4),
),
'settings' => array(
'text_processing' => 1,
),
'display' => array(
'default' => array('type' => 'text_default', 'label' => 'above'),
'media_gallery_thumbnail' => array('type' => 'text_default', 'label' => 'above'),
'media_gallery_lightbox' => array('type' => 'text_default', 'label' => 'above'),
'media_gallery_detail' => array('type' => 'text_default', 'label' => 'above'),
),
),
'field_tags' => array(
'field_name' => 'field_tags',
'label' => $t('Tags'),
'widget' => array(
'type' => 'taxonomy_autocomplete',
),
'display' => array(
'default' => array('type' => 'hidden'),
),
),
'field_license' => array(
'field_name' => 'field_license',
'label' => $t('License settings for this media'),
'required' => TRUE,
'default_value' => array(
array('value' => 'nothing'),
),
'description' => $t('Select a <a href="http://creativecommons.org" target="_new">Creative Commons</a> license for others who use this media.'),
'display' => array(
'default' => array('type' => 'hidden'),
'media_gallery_thumbnail' => array('type' => 'list_default', 'label' => 'hidden'),
'media_gallery_lightbox' => array('type' => 'list_default', 'label' => 'hidden'),
'media_gallery_detail' => array('type' => 'list_default', 'label' => 'hidden'),
),
),
);
return $media_instances;
}
/**
* Make sure the field_tags field exists and is of the right type.
*/
function _media_gallery_ensure_field_tags() {
// Make sure the 'tags' vocabulary exists.
$vocabulary = taxonomy_vocabulary_machine_name_load('tags');
if (!$vocabulary) {
$description = st('Use tags to group articles on similar topics into categories.');
$help = st('Enter a comma-separated list of words to describe your content.');
$vocabulary = (object) array(
'name' => 'Tags',
'description' => $description,
'machine_name' => 'tags',
'help' => $help,
);
taxonomy_vocabulary_save($vocabulary);
}
$field = array(
'field_name' => 'field_tags',
'type' => 'taxonomy_term_reference',
// Set cardinality to unlimited for tagging.
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => 0,
),
),
),
);
_media_gallery_ensure_field($field);
}
/**
* Makes sure media entities have the fields media gallery requires.
*/
function _media_gallery_ensure_media_instances() {
$t = get_t();
$instances = _media_required_instances();
foreach (file_type_get_enabled_types() as $bundle => $type) {
foreach ($instances as $instance) {
$instance_copy = $instance;
$instance_copy += array(
'entity_type' => 'file',
'bundle' => $bundle,
);
$label = in_array($bundle, array('image', 'audio', 'video')) ? $bundle : 'file';
if ($instance_copy['field_name'] == 'field_tags' && !isset($instance_copy['description'])) {
$instance_copy['description'] = $t("Enter a comma-separated list of words to describe your $label.");
}
if ($instance_copy['field_name'] == 'field_license') {
$instance_copy['label'] = $t("License settings for this $label");
$instance_copy['description'] = $t('Select a <a href="http://creativecommons.org" target="_new">Creative Commons</a> license for others who use this ' . $label . '.');
}
_media_gallery_ensure_instance($instance_copy);
}
}
}
/**
* Helper function to create required taxonomy vocabulary.
*/
function media_gallery_create_taxonomy_vocab() {
$t = get_t();
$vocabulary = (object) array(
'name' => 'Gallery collections',
'description' => $t('Groups of rich media galleries'),
'machine_name' => 'gallery_collections',
);
taxonomy_vocabulary_save($vocabulary);
variable_set('media_gallery_collection_vid', $vocabulary->vid);
return $vocabulary;
}
/**
* Helper function to create required taxonomy term.
*/
function media_gallery_create_taxonomy_term($vocabulary) {
// Create a taxonomy term for the "All Galleries" collection.
$term = new stdClass();
$term->vid = $vocabulary->vid;
$term->name = 'Galleries';
$term->description = '';
// Choose a text format that will prevent WYSIWYGs from appearing by default.
// When we allow people to create new gallery collections we'll have to
// (carefully) modify the form for adding new ones also.
$term->format = filter_fallback_format();
$term->path = array('alias' => 'galleries');
// Save the term, preventing Pathauto from aliasing it incorrectly.
_media_gallery_prevent_unwanted_pathauto_aliases();
taxonomy_term_save($term);
_media_gallery_allow_all_pathauto_aliases();
// Create a menu link for this taxonomy term. We set the link title to
// 'Taxonomy term' in order to match the title of the corresponding router
// item, since this is what triggers the menu system to display a dynamic
// title for the link.
$menu_item = array(
'menu_name' => 'main-menu',
'weight' => 10,
'link_title' => 'Taxonomy term',
'link_path' => 'taxonomy/term/' . $term->tid,
);
// If the router item doesn't exist yet (for example, if we are installing
// either the Taxonomy module or Drupal itself at the same time as Media
// Gallery), rebuild the menu before saving, to avoid errors.
$router_item_exists = (bool) db_query_range('SELECT 1 FROM {menu_router} WHERE path = :path', 0, 1, array(':path' => 'taxonomy/term/%'))->fetchField();
if (!$router_item_exists) {
menu_rebuild();
}
menu_link_save($menu_item);
// Save the term ID for future use.
variable_set('media_gallery_default_collection_tid', $term->tid);
}
/**
* Implements hook_uninstall().
*/
function media_gallery_uninstall() {
// Delete all existing galleries.
$nids = db_query('SELECT nid FROM {node} n WHERE n.type = :type', array(':type' => 'media_gallery'))->fetchCol();
node_delete_multiple($nids);
// Delete fields and instances.
foreach (array_keys(_media_gallery_controlled_fields()) as $field) {
field_delete_field($field);
}
$instances = _media_gallery_controlled_instances();
$instances += field_info_instances('node', 'media_gallery');
foreach ($instances as $instance_name => $instance) {
field_delete_instance($instance);
}
// Delete the content type itself.
// @todo: We can't run this since the content type already ceased to exist
// when the module was disabled. But we'd like to be able to, so that the
// proper hooks within node_type_delete() get fired.
// node_type_delete('media_gallery');
// Delete the taxonomy vocabulary.
$vid = variable_get('media_gallery_collection_vid');
if ($vid && function_exists('taxonomy_vocabulary_delete')) {
taxonomy_vocabulary_delete($vid);
}
// Delete variables for the media gallery node type.
variable_del('node_submitted_media_gallery');
variable_del('node_options_media_gallery');
variable_del('comment_media_gallery');
variable_del('media_gallery_collection_vid');
variable_del('media_gallery_default_collection_tid');
}
/**
* Set the default value of media_gallery_expose_block to zero
* In order to have the expose block checkbox appear above the column and row number dropdowns
* it was necessary to update those field instances with their initial values, essentially
* refreshing them.
*/
function media_gallery_update_7000() {
$t = get_t();
// Add a check to make sure the instance exists if we want to update it.
$instance = field_info_instance('node', 'media_gallery_expose_block', 'media_gallery');
if ($instance) {
$instance['default_value'] = array(array('value' => 0));
field_update_instance($instance);
}
// Update the media_display field so that it shows the label
foreach (array('video', 'image') as $bundle) {
field_update_instance( array(
'field_name' => 'media_description',
'bundle' => $bundle,
'entity_type' => 'media',
'display' => array(
'default' => array('type' => 'text_default', 'label' => 'above'),
'media_gallery_thumbnail' => array('type' => 'text_default', 'label' => 'above'),
'media_gallery_lightbox' => array('type' => 'text_default', 'label' => 'above'),
'media_gallery_detail' => array('type' => 'text_default', 'label' => 'above'),
),
)
);
}
// Remove the media_gallery_gallery_info field
field_delete_field('media_gallery_gallery_info');
field_delete_field('media_gallery_image_info');
// Ensure that the new field exists
$extra_field = array(
'field_name' => 'media_gallery_lightbox_extras',
'cardinality' => 1,
'locked' => TRUE,
'type' => 'list_boolean',
'settings' => array(
'allowed_values_function' => '_media_gallery_get_lightbox_extras_values',
),
);
_media_gallery_ensure_field($extra_field);
$new_instance = array(
'field_name' => 'media_gallery_lightbox_extras',
'label' => 'Lightbox title and description',
'description' => $t('Show title and description'),
'default_value' => array(array('value' => 0)),
'entity_type' => 'node',
'bundle' => 'media_gallery',
'widget' => array(
'type' => 'options_onoff',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
);
_media_gallery_ensure_instance($new_instance);
}
/**
* Updates all gallery nodes to use new default settings.
*/
function media_gallery_update_7001() {
$result = db_query("SELECT nid FROM {node} WHERE type = 'media_gallery'");
foreach ($result as $record) {
$node = node_load($record->nid);
if ($node->media_gallery_format[LANGUAGE_NONE][0]['value'] == 'lightbox_text') {
$node->media_gallery_format[LANGUAGE_NONE][0]['value'] = 'lightbox';
$node->media_gallery_lightbox_extras[LANGUAGE_NONE][0]['value'] = 0;
node_save($node);
}
}
}
/**
* Configure video formatters to desired defaults for gallery view modes.
*/
function media_gallery_update_7002() {
drupal_load('module', 'field');
$bundle = 'video';
$bundle_settings = field_bundle_settings('media', $bundle);
$bundle_settings['view_modes']['media_gallery_thumbnail']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['media_gallery_lightbox']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['media_gallery_detail']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['media_gallery_block_thumbnail']['custom_settings'] = TRUE;
$bundle_settings['view_modes']['media_gallery_collection_thumbnail']['custom_settings'] = TRUE;
field_bundle_settings('media', $bundle, $bundle_settings);
$instance = field_info_instance('media', 'file', $bundle);
$instance['display']['media_gallery_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden');
$instance['display']['media_gallery_lightbox'] = array('type' => 'styles_file_media_gallery_large', 'label' => 'hidden');
$instance['display']['media_gallery_detail'] = array('type' => 'styles_file_media_gallery_large', 'label' => 'hidden');
$instance['display']['media_gallery_block_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden');
$instance['display']['media_gallery_collection_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden');
field_update_instance($instance);
}
/**
* Configure gallery nodes to allow video as well as image media.
*/
function media_gallery_update_7003() {
drupal_load('module', 'field');
$instance = field_info_instance('node', 'media_gallery_media', 'media_gallery');
// If the instance doesn't exist, we can't update it.
if ($instance) {
$instance['widget']['settings']['allowed_types'] = array('image' => 'image', 'video' => 'video');
field_update_instance($instance);
}
}
/**
* Configure media license field to have a per-type label and description.
*/
function media_gallery_update_7004() {
drupal_load('module', 'field');
drupal_load('module', 'media');
$t = get_t();
foreach (media_type_get_types() as $bundle => $type) {
$instance = field_info_instance('media', 'field_license', $bundle);
// If the instance doesn't exist, we can't update it.
if ($instance) {
$label = in_array($bundle, array('image', 'audio', 'video')) ? $bundle : 'file';
$instance['label'] = $t("License settings for this $label");
$instance['description'] = $t('Select a <a href="http://creativecommons.org" target="_new">Creative Commons</a> license for others who use this ' . $label . '.');
field_update_instance($instance);
}
}
}
/**
* There was an odd case where galleries created previous to youtube support
* lost some display settings on update. This update fixes those display settings.
*/
function media_gallery_update_7005() {
// Ensure that the media_description field has the proper label
foreach (array('video', 'image') as $bundle) {
$instance = field_info_instance('media', 'media_description', $bundle);
if ($instance) {
$instance['label'] = t('Description');
field_update_instance($instance);
}
}
// Ensure that media videos have the proper display formatters
$instance = field_info_instance('media', 'file', 'video');
if ($instance) {
$instance['display']['media_gallery_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden');
$instance['display']['media_gallery_lightbox'] = array('type' => 'styles_file_media_gallery_large', 'label' => 'hidden');
$instance['display']['media_gallery_detail'] = array('type' => 'styles_file_media_gallery_large', 'label' => 'hidden');
$instance['display']['media_gallery_block_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden');
$instance['display']['media_gallery_collection_thumbnail'] = array('type' => 'styles_file_media_gallery_thumbnail', 'label' => 'hidden');
field_update_instance($instance);
}
// Remove the old add_images_link extra field if it still exists
$bundle_settings = field_bundle_settings('node', 'media_gallery');
unset($bundle_settings['extra_fields']['display']['add_images_link']);
$bundle_settings['extra_fields']['display']['add_media_link']['full']['weight'] = 1;
$bundle_settings['extra_fields']['display']['add_media_link']['full']['visible'] = TRUE;
$bundle_settings['extra_fields']['display']['add_media_link']['default']['visible'] = FALSE;
$bundle_settings['extra_fields']['display']['add_media_link']['teaser']['visible'] = FALSE;
$bundle_settings['extra_fields']['display']['add_media_link']['media_gallery_block']['visible'] = FALSE;
field_bundle_settings('node', 'media_gallery', $bundle_settings);
}
/**
* Make sure the gallery node form includes the media_gallery_lightbox_extras
* checkbox.
*/
function media_gallery_update_7006() {
// Whether to show a "Exclude title and description" checkbox.
$field = array(
'field_name' => 'media_gallery_lightbox_extras',
'label' => 'Lightbox title and description',
'description' => 'Show title and description',
'default_value' => array(array('value' => 0)),
'widget' => array(
'type' => 'options_onoff',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
);
$instance = array(
'field_name' => 'media_gallery_lightbox_extras',
'label' => 'Lightbox title and description',
'description' => 'Show title and description',
'default_value' => array(array('value' => 0)),
'entity_type' => 'node',
'bundle' => 'media_gallery',
'widget' => array(
'type' => 'options_onoff',
),
'display' => array(
'default' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'full' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
'label' => 'hidden',
),
'media_gallery_block' => array(
'type' => 'hidden',
'label' => 'hidden',
),
),
);
_media_gallery_ensure_field($field);
_media_gallery_ensure_instance($instance);
}
/**
* Fix stored display settings for the 'add_media_link' extra field to include 'weight'.
*/
function media_gallery_update_7007() {
// _field_extra_fields_pre_render() requires an explicit 'weight' value for
// all extra field settings stored in the database. Early versions of this
// module failed to include that.
$bundle_settings = field_bundle_settings('node', 'media_gallery');
if (isset($bundle_settings['extra_fields']['display']['add_media_link'])) {
foreach ($bundle_settings['extra_fields']['display']['add_media_link'] as $view_mode => &$settings) {
$settings += array('weight' => 1);
}
}
field_bundle_settings('node', 'media_gallery', $bundle_settings);
}
/**
* Ensure that the description fields for galleries and media allow filtered text.
*/
function media_gallery_update_7008() {
// Ensure that the media_description field is filtered text.
foreach (array('video', 'image') as $bundle) {
$instance = field_info_instance('media', 'media_description', $bundle);
if ($instance) {
$instance['settings']['text_processing'] = 1;
field_update_instance($instance);
}
}
// Ensure that the media_gallery_description field is filtered text.
$instance = field_info_instance('node', 'media_gallery_description', 'media_gallery');
if ($instance) {
$instance['settings']['text_processing'] = 1;
field_update_instance($instance);
}
}
/**
* Update old per-view-mode media_gallery_* field formatters to the generic media_gallery formatter with a setting.
*/
function media_gallery_update_7009() {
$instances = array();
$fields = field_read_fields(array('type' => 'media'), array('include_inactive' => TRUE));
foreach ($fields as $field) {
$instances = array_merge($instances, field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE)));
}
foreach ($instances as $instance) {
$update_instance = FALSE;
foreach ($instance['display'] as $view_mode => $display) {
if (in_array($display['type'], array('media_gallery_thumbnail', 'media_gallery_lightbox', 'media_gallery_detail', 'media_gallery_block_thumbnail', 'media_gallery_collection_thumbnail'))) {
$update_instance = TRUE;
$instance['display'][$view_mode]['type'] = 'media_gallery';
$instance['display'][$view_mode]['settings'] = array('file_view_mode' => $display['type']);
}
}
if ($update_instance) {
field_update_instance($instance);
}
}
}
/**
* Configure galleries to allow audio.
*/
function media_gallery_update_7010() {
drupal_load('module', 'field');
$bundle = 'audio';
// Enable audio for the node's media_gallery field.
if ($instance = field_info_instance('node', 'media_gallery_media', 'media_gallery')) {
$instance['widget']['settings']['allowed_types'][$bundle] = $bundle;
field_update_instance($instance);
}
// Enable gallery view modes for audio entities.
$bundle_settings = field_bundle_settings('file', $bundle);
foreach (array('media_gallery_thumbnail', 'media_gallery_lightbox', 'media_gallery_detail', 'media_gallery_block_thumbnail', 'media_gallery_collection_thumbnail') as $view_mode) {
$bundle_settings['view_modes'][$view_mode]['custom_settings'] = TRUE;
}
field_bundle_settings('file', $bundle, $bundle_settings);
}
/**
* Empty update function.
*/
function media_gallery_update_7011() {
}
/**
* Deprecated update function. Moved to 7200.
*/
function media_gallery_update_7012() {
}
/**
* Converts old media_gallery_media field to media_gallery_file field.
*/
function media_gallery_update_7200() {
// Create fields (but not instances yet) for media_gallery nodes and
// for the gallery collection vocabulary.
foreach (_media_gallery_controlled_fields() as $field) {
_media_gallery_ensure_field($field);
}
// Attach fields to the media gallery node type (including a term reference
// for the default collection).
foreach (_media_gallery_controlled_instances('node') as $instance) {
_media_gallery_ensure_instance($instance);
}
// Make sure all media bundles have the instances we expect.
_media_gallery_ensure_media_instances();
// Move content from media_gallery_media to media_gallery_file.
$result = db_query("SELECT nid FROM {node} WHERE type = 'media_gallery'");
foreach ($result as $record) {
$node = node_load($record->nid);
$change = FALSE;
// Check all languages.
foreach ($node->media_gallery_media as $langcode => $media) {
// Only update if the media_gallery_file field is not in use, but
// media_gallery_media is in use.
if (empty($node->media_gallery_file[$langcode]) && !empty($node->media_gallery_media[$langcode])) {
foreach ($media as $file) {
// Copy the file contents over.
$node->media_gallery_file[$langcode][] = array(
'fid' => $file['fid'],
'description' => $file['title'],
'display' => 1,
);
$change = TRUE;
}
}
}
if ($change) {
// A change has occur, so we need to save the node.
node_save($node);
}
}
$instance = array(
'field_name' => 'media_gallery_media',
'entity_type' => 'node',
'bundle' => 'media_gallery',
);
// Remove the old media_gallery_media field.
field_delete_instance($instance, TRUE);
field_delete_field($instance['field_name']);
}
|