-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicture.module
2522 lines (2299 loc) · 84.8 KB
/
picture.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
* Picture formatter.
*/
define('PICTURE_EMPTY_IMAGE', '_empty image_');
define('PICTURE_ORIGINAL_IMAGE', '_original image_');
/**
* Implements hook_flush_caches().
*/
function picture_flush_caches() {
// After update.php, caches are flushed. Check if file_entity has updated to
// version 2.
module_load_include('admin.inc', 'picture');
}
/**
* Implements hook_modules_enabled().
*/
function picture_modules_enabled($modules) {
if (in_array('file_entity', $modules)) {
module_load_include('admin.inc', 'picture');
module_load_include('module', 'file_entity');
}
}
/**
* Implements hook_permission().
*/
function picture_permission() {
return array(
'administer pictures' => array(
'title' => t('Administer Pictures'),
'description' => t('Administer Pictures'),
),
);
}
/**
* Implements hook_menu().
*/
function picture_menu() {
$items = array();
$items['admin/config/media/picture/settings'] = array(
'title' => 'Settings',
'type' => MENU_LOCAL_TASK,
'description' => 'Picture settings (CKEditor, File Entity, ...)',
'page callback' => 'backdrop_get_form',
'page arguments' => array('picture_admin_settings'),
'access arguments' => array('administer pictures'),
'file' => 'picture.admin.inc',
'weight' => 1,
);
return $items;
}
/**
* Load mappings.
*/
function picture_mapping_load($name) {
if ($name) {
return picture_mapping_create(config_get('picture.mapping.' . $name));
}
return FALSE;
}
/**
* Load all mappings.
*/
function picture_mapping_load_all() {
$mappings = [];
$mapping_config_names = config_get_names_with_prefix('picture.mapping.');
foreach ($mapping_config_names as $mapping_config_name) {
$mapping_name = str_replace('picture.mapping.', '', $mapping_config_name);
$mappings[$mapping_name] = picture_mapping_create(config_get($mapping_config_name));
}
return $mappings;
}
/**
* Save mappings.
*/
function picture_mapping_save(PictureMapping $mapping) {
return $mapping->save();
}
/**
* Implements hook_config_info().
*/
function picture_config_info() {
$prefixes['picture.settings'] = array(
'label' => t('Picture settings'),
'group' => t('Configuration'),
);
$prefixes['picture.mapping'] = array(
'name_key' => 'machine_name',
'label_key' => 'title',
'group' => t('Picture Mappings'),
);
return $prefixes;
}
/**
* Implements hook_library_info().
*/
function picture_library_info() {
$config = config('picture.settings');
$libraries = array();
switch ($config->get('polyfill_version')) {
case 'min':
$libraries['picturefill_head'] = array(
'title' => t('Picturefill head fix'),
'version' => '3.0.1',
'js' => array(
'document.createElement( "picture" );' => array(
'type' => 'inline',
'weight' => -20,
'group' => JS_LIBRARY,
'scope' => 'header',
'need_jquery' => FALSE,
),
),
);
$libraries['picturefill'] = array(
'title' => t('Picturefill'),
'website' => 'https://github.com/scottjehl/picturefill',
'version' => '3.0.1',
'js' => array(
backdrop_get_path('module', 'picture') . '/js/picturefill/picturefill.min.js' => array(
'type' => 'file',
'weight' => -10,
'group' => JS_DEFAULT,
'scope' => $config->get('js_scope'),
'need_jquery' => FALSE,
),
),
);
$libraries['picture.ajax'] = array(
'title' => t('Ajax support for picture'),
'version' => VERSION,
'js' => array(
backdrop_get_path('module', 'picture') . '/js/picture.min.js' => array(
'type' => 'file',
'weight' => -10,
'group' => JS_DEFAULT,
'scope' => $config->get('js_scope'),
'need_jquery' => FALSE,
),
),
);
$libraries['lazysizes'] = array(
'title' => t('Lazyload for picture element'),
'version' => '1.0.1',
'js' => array(
backdrop_get_path('module', 'picture') . '/lazysizes/lazysizes.min.js' => array(
'type' => 'file',
'weight' => -20,
'group' => JS_LIBRARY,
'need_jquery' => FALSE,
),
),
);
$libraries['lazysizes_aspect_ratio'] = array(
'title' => t('Aspect ratio plugin for lazysizes'),
'version' => '1.0.1',
'js' => array(
backdrop_get_path('module', 'picture') . '/lazysizes/plugins/aspectratio/ls.aspectratio.min.js' => array(
'type' => 'file',
'weight' => -10,
'group' => JS_LIBRARY,
'need_jquery' => FALSE,
),
),
'css' => array(
backdrop_get_path('module', 'picture') . '/lazysizes/plugins/aspectratio/ls.aspectratio.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
break;
case 'dev':
$libraries['picturefill_head'] = array(
'title' => t('Picturefill head fix'),
'version' => '3.0.1',
'js' => array(
'document.createElement( "picture" );' => array(
'type' => 'inline',
'weight' => -10,
'group' => JS_DEFAULT,
'scope' => 'header',
'need_jquery' => FALSE,
),
),
);
$libraries['picturefill'] = array(
'title' => t('Picturefill'),
'website' => 'https://github.com/scottjehl/picturefill',
'version' => '3.0.1',
'js' => array(
backdrop_get_path('module', 'picture') . '/js/picturefill/picturefill.js' => array(
'type' => 'file',
'weight' => -10,
'group' => JS_DEFAULT,
'scope' => $config->get('js_scope'),
'need_jquery' => FALSE,
),
),
);
$libraries['picture.ajax'] = array(
'title' => t('Ajax support for picture'),
'version' => VERSION,
'js' => array(
backdrop_get_path('module', 'picture') . '/js/picture.js' => array(
'type' => 'file',
'weight' => -10,
'group' => JS_DEFAULT,
'scope' => $config->get('js_scope'),
'need_jquery' => FALSE,
),
),
);
$libraries['lazysizes'] = array(
'title' => t('Lazyload for picture element'),
'version' => '1.0.1',
'js' => array(
backdrop_get_path('module', 'picture') . '/lazysizes/lazysizes.js' => array(
'type' => 'file',
// File has async, put before all files to prevent JS concatenation breaking.
'weight' => -20,
'group' => JS_LIBRARY,
'need_jquery' => FALSE,
),
),
);
$libraries['lazysizes_aspect_ratio'] = array(
'title' => t('Aspect ratio plugin for lazysizes'),
'version' => '1.0.1',
'js' => array(
backdrop_get_path('module', 'picture') . '/lazysizes/plugins/aspectratio/ls.aspectratio.js' => array(
'type' => 'file',
'weight' => -10,
'group' => JS_LIBRARY,
'need_jquery' => FALSE,
),
),
'css' => array(
backdrop_get_path('module', 'picture') . '/lazysizes/plugins/aspectratio/ls.aspectratio.css' => array(
'type' => 'file',
'media' => 'screen',
),
),
);
break;
}
return $libraries;
}
/**
* Implements hook_theme().
*/
function picture_theme() {
return array(
'picture' => array(
'variables' => array(
'style_name' => NULL,
'path' => NULL,
'uri' => NULL,
'width' => NULL,
'height' => NULL,
'alt' => '',
'title' => NULL,
'attributes' => array(),
'breakpoints' => array(),
'timestamp' => NULL,
'lazyload' => NULL,
'lazyload_aspect_ratio' => NULL,
),
),
'picture_formatter' => array(
'variables' => array(
'item' => NULL,
'path' => NULL,
'image_style' => NULL,
'breakpoints' => array(),
'lazyload' => NULL,
'lazyload_aspect_ratio' => NULL,
),
),
'picture_formatter_colorbox' => array(
'variables' => array(
'item' => NULL,
'path' => NULL,
'image_style' => NULL,
'breakpoints' => array(),
'colorbox_group' => array(),
'colorbox_image_style' => NULL,
'colorbox_group_id' => NULL,
'colorbox_caption' => NULL,
'lazyload' => NULL,
'lazyload_aspect_ratio' => NULL,
),
),
'picture_source' => array(
'variables' => array(
'srcset' => NULL,
'media' => NULL,
'mime_type' => NULL,
'sizes' => NULL,
'lazyload' => NULL,
'lazyload_aspect_ratio' => NULL,
),
),
'image_srcset' => array(
'variables' => array(
'uri' => NULL,
'path' => NULL,
'width' => NULL,
'height' => NULL,
'alt' => '',
'title' => NULL,
'attributes' => array(),
'srcset' => array(),
'sizes' => NULL,
'lazyload' => NULL,
'lazyload_aspect_ratio' => NULL,
),
),
'picture_sizes_formatter' => array(
'variables' => array(
'item' => NULL,
'path' => NULL,
'image_styles' => array(),
'fallback_image_style' => NULL,
'sizes' => NULL,
'attributes' => array(),
'lazyload_data_attributes' => NULL,
'lazyload_class' => NULL,
),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function picture_field_formatter_info() {
$formatters = array();
$options = picture_get_mapping_options();
$mappings = array_keys(picture_get_mapping_options());
if ($mappings) {
$formatters['picture'] = array(
'label' => t('Picture'),
'field types' => array('image'),
'settings' => array(
'picture_mapping' => reset($mappings),
'fallback_image_style' => '',
'lazyload' => '',
'lazyload_aspect_ratio' => '',
'image_link' => '',
'colorbox_settings' => array(
'colorbox_group' => '',
'colorbox_gallery' => 'post',
'colorbox_gallery_custom' => '',
'colorbox_caption' => 'auto',
'colorbox_caption_custom' => '',
'colorbox_multivalue_index' => NULL,
),
),
);
}
$formatters['picture_sizes_formatter'] = array(
'label' => t('Image with sizes'),
'field types' => array('image'),
'settings' => array(
'sizes' => '',
'image_styles' => array(),
'fallback_image_style' => PICTURE_EMPTY_IMAGE,
'lazyload' => '',
'lazyload_aspect_ratio' => '',
'lazyload_data_attributes' => 0,
'lazyload_class' => '',
'image_link' => '',
'colorbox_settings' => array(
'colorbox_group' => '',
'colorbox_gallery' => 'post',
'colorbox_gallery_custom' => '',
'colorbox_caption' => 'auto',
'colorbox_caption_custom' => '',
'colorbox_multivalue_index' => NULL,
),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function picture_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$function = "picture_field_formatter_settings_{$display['type']}_form";
$settings = $display['settings'];
return $function($field, $instance, $settings);
}
/**
* Helper function.
*
* @see picture_field_formatter_settings_form()
*/
function picture_field_formatter_settings_picture_form($field, $instance, $settings) {
$options = picture_get_mapping_options();
if ($options) {
$element['picture_mapping'] = array(
'#title' => t('Picture mapping'),
'#type' => 'select',
'#default_value' => isset($settings['picture_mapping']) ? $settings['picture_mapping'] : '',
'#required' => TRUE,
'#options' => picture_get_mapping_options(),
);
}
// No picture mappings.
else {
$element['picture_mapping'] = array(
'#title' => t('Picture mapping'),
'#type' => 'item',
'#markup' => t(
'There are no picture groups defined. !create_link.',
array('!create_link' => l(t('Create a picture mapping'), 'admin/config/media/picture/add'))
),
);
}
$image_styles = image_style_options(FALSE);
$element['fallback_image_style'] = array(
'#title' => t('Fallback image style'),
'#type' => 'select',
'#default_value' => $settings['fallback_image_style'],
'#empty_option' => t('Automatic'),
'#options' => $image_styles + array(
PICTURE_EMPTY_IMAGE => t('Empty image'),
PICTURE_ORIGINAL_IMAGE => t('Original image'),
),
);
$element['lazyload'] = array(
'#title' => t('Picture lazyload'),
'#description' => t('Image will be rendered when it appears in viewport, helps to optimize page load speed.'),
'#type' => 'checkbox',
'#default_value' => $settings['lazyload'] ? $settings['lazyload'] : FALSE,
);
$element['lazyload_aspect_ratio'] = array(
'#title' => t('Keep aspect ratio'),
'#type' => 'checkbox',
'#description' => t('Preserve the space for the image being lazyloaded to avoid layout reflows. <br /> Image ratio is defined per breakpoint, make sure all images from srcset have the same ratio. <br />Output example: !example',
array('!example' => htmlentities('<source media="(...)" data-srcset="image_400x200.jpg x1, image_800x400.jpg x2, image_1200x600.jpg x3" data-aspectratio="2" />'))
),
'#default_value' => !empty($settings['lazyload_aspect_ratio']),
'#states' => array(
'visible' => array(
':input[name="fields[field_image][settings_edit_form][settings][lazyload]"]' => array('checked' => TRUE),
':input[name="fields[field_image][settings_edit_form][settings][fallback_image_style]"]' => array('value' => PICTURE_EMPTY_IMAGE),
),
),
);
$link_types = picture_link_types($instance);
$element['image_link'] = array(
'#title' => t('Link image to'),
'#type' => 'select',
'#default_value' => $settings['image_link'],
'#empty_option' => t('Nothing'),
'#options' => $link_types,
'#attributes' => array(
'class' => array('picture-image-link'),
),
);
if (module_exists('colorbox')) {
// Settings for the colorbox option.
$element['colorbox_settings'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Colorbox settings'),
'#collapsed' => FALSE,
'#collapsible' => TRUE,
'#states' => array(
'visible' => array(
':input[name$="' . $field['field_name'] . '][settings_edit_form][settings][image_link]"].picture-image-link' => array('value' => 'colorbox'),
),
),
);
$element['colorbox_settings']['colorbox_group'] = array(
'#title' => t('Colorbox picture mapping'),
'#type' => 'select',
'#default_value' => $settings['colorbox_settings']['colorbox_group'],
'#required' => FALSE,
'#options' => picture_get_mapping_options(),
);
$gallery = array(
'post' => t('Per post gallery'),
'page' => t('Per page gallery'),
'field_post' => t('Per field in post gallery'),
'field_page' => t('Per field in page gallery'),
'custom' => t('Custom'),
'none' => t('No gallery'),
);
$element['colorbox_settings']['colorbox_gallery'] = array(
'#title' => t('Gallery (image grouping)'),
'#type' => 'select',
'#default_value' => $settings['colorbox_settings']['colorbox_gallery'],
'#options' => $gallery,
'#description' => t('How Colorbox should group the image galleries.'),
'#attributes' => array(
'class' => array('picture-colorbox-gallery'),
),
);
$element['colorbox_settings']['colorbox_gallery_custom'] = array(
'#title' => t('Custom gallery'),
'#type' => 'textfield',
'#maxlength' => 32,
'#default_value' => $settings['colorbox_settings']['colorbox_gallery_custom'],
'#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, hyphen and underscores.'),
'#element_validate' => array('colorbox_gallery_custom_validate'),
'#required' => FALSE,
'#states' => array(
'visible' => array(
':input[name$="[settings][colorbox_settings][colorbox_gallery]"].picture-colorbox-gallery' => array('value' => 'custom'),
),
),
);
$caption = array(
'auto' => t('Automatic'),
'title' => t('Title text'),
'alt' => t('Alt text'),
'node_title' => t('Content title'),
'custom' => t('Custom (with tokens)'),
'none' => t('None'),
);
$element['colorbox_settings']['colorbox_caption'] = array(
'#title' => t('Caption'),
'#type' => 'select',
'#default_value' => $settings['colorbox_settings']['colorbox_caption'],
'#options' => $caption,
'#description' => t('Automatic will use the first none empty value of the title, the alt text and the content title.'),
'#attributes' => array(
'class' => array('picture-colorbox-caption'),
),
);
$element['colorbox_settings']['colorbox_caption_custom'] = array(
'#title' => t('Custom caption'),
'#type' => 'textfield',
'#default_value' => $settings['colorbox_settings']['colorbox_caption_custom'],
'#states' => array(
'visible' => array(
':input[name$="[settings][colorbox_settings][colorbox_caption]"].picture-colorbox-caption' => array('value' => 'custom'),
),
),
);
// Allow users to hide or set a custom recursion limit.
// The module token_tweaks sets a global recursion limit that can not be
// bypassed.
// TODO This variable was probably removed in Backdrop without replacement.
// TODO This variable was probably removed in Backdrop without replacement.
if (module_exists('token') && $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3))) {
$element['colorbox_settings']['colorbox_token'] = array(
'#type' => 'fieldset',
'#title' => t('Replacement patterns'),
'#theme' => 'token_tree',
'#token_types' => array($instance['entity_type'], 'file'),
'#recursion_limit' => $recursion_limit,
'#dialog' => TRUE,
'#states' => array(
'visible' => array(
':input[name$="[settings][colorbox_settings][colorbox_caption]"].picture-colorbox-caption' => array('value' => 'custom'),
),
),
);
}
else {
$element['colorbox_settings']['colorbox_token'] = array(
'#type' => 'fieldset',
'#title' => t('Replacement patterns'),
'#description' => '<strong class="error">' . t('For token support the <a href="@token_url">token module</a> must be installed.', array('@token_url' => 'http://drupal.org/project/token')) . '</strong>',
'#states' => array(
'visible' => array(
':input[name$="[settings][colorbox_settings][colorbox_caption]"].picture-colorbox-caption' => array('value' => 'custom'),
),
),
);
}
}
return $element;
}
/**
* Helper function.
*
* @see picture_field_formatter_settings_form()
*/
function picture_field_formatter_settings_picture_sizes_formatter_form($field, $instance, $settings) {
$element = array();
if ($instance['entity_type'] == 'file') {
if (empty($settings['sizes'])) {
$settings['sizes'] = '(min-width: 0px)';
}
$styles = array_filter($settings['image_styles']);
if (empty($styles)) {
$settings['image_styles'][PICTURE_EMPTY_IMAGE] = PICTURE_EMPTY_IMAGE;
}
}
$element['sizes'] = array(
'#title' => t('Sizes'),
'#type' => 'textfield',
'#maxlength' => 1024,
'#description' => t(
'The value of the sizes attribute. See !link for more information.',
array(
'!link' => l(t('the spec'), 'http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content.html#introduction-3:viewport-based-selection-2'),
)
),
'#default_value' => $settings['sizes'],
'#required' => TRUE,
);
$image_styles = image_style_options(FALSE);
$image_styles[PICTURE_EMPTY_IMAGE] = t('Empty image');
$image_styles[PICTURE_ORIGINAL_IMAGE] = t('Original image');
$element['image_styles'] = array(
'#title' => t('Image styles'),
'#type' => 'checkboxes',
'#default_value' => $settings['image_styles'],
'#options' => $image_styles,
'#required' => TRUE,
);
$element['fallback_image_style'] = array(
'#title' => t('Fallback image style'),
'#type' => 'select',
'#default_value' => $settings['fallback_image_style'] ? $settings['fallback_image_style'] : PICTURE_EMPTY_IMAGE,
'#options' => $image_styles + array(
PICTURE_EMPTY_IMAGE => t('Empty image'),
PICTURE_ORIGINAL_IMAGE => t('Original image'),
),
'#required' => TRUE,
);
$link_types = picture_link_types($instance);
unset($link_types['colorbox']);
$element['image_link'] = array(
'#title' => t('Link image to'),
'#type' => 'select',
'#default_value' => $settings['image_link'],
'#empty_option' => t('Nothing'),
'#options' => $link_types,
'#attributes' => array(
'class' => array('picture-image-link'),
),
);
$element['lazyload_data_attributes'] = array(
'#title' => t('Lazyload using data attributes'),
'#type' => 'select',
'#default_value' => $settings['lazyload_data_attributes'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$element['lazyload_class'] = array(
'#title' => t('Lazyload class'),
'#type' => 'textfield',
'#default_value' => $settings['lazyload_class'],
);
return $element;
}
/**
* Returns a list of picture mappings for use in a select list.
*/
function picture_get_mapping_options() {
$picture_mapping_options = array();
$picture_mappings = picture_mapping_load_all();
if ($picture_mappings && !empty($picture_mappings)) {
foreach ($picture_mappings as $picture_mapping) {
// Exclude old mappings.
if ($picture_mapping instanceof PictureMapping && (!isset($picture_mapping->disabled) || !$picture_mapping->disabled) && $picture_mapping->hasMappings()) {
$picture_mapping_options[$picture_mapping->getMachineName()] = $picture_mapping->label();
}
}
}
return $picture_mapping_options;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function picture_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$function = "picture_field_formatter_settings_{$display['type']}_summary";
$settings = $display['settings'];
return $function($field, $instance, $settings);
}
/**
* Helper function.
*
* @see picture_field_formatter_settings_summary()
*/
function picture_field_formatter_settings_picture_summary($field, $instance, $settings) {
$summary = array();
$picture_mapping = picture_mapping_load($settings['picture_mapping']);
if ($picture_mapping) {
$summary[] = t('Picture mapping: @picture_mapping', array('@picture_mapping' => $picture_mapping->label()));
$image_styles = image_style_options(FALSE);
unset($image_styles['']);
if (isset($image_styles[$settings['fallback_image_style']])) {
$summary[] = t('Fallback Image style: @style', array('@style' => $image_styles[$settings['fallback_image_style']]));
}
else {
$summary[] = t('Automatic fallback');
}
$link_types = picture_link_types($instance);
// Display this setting only if image is linked.
if (isset($link_types[$settings['image_link']])) {
$summary[] = filter_xss_admin($link_types[$settings['image_link']]);
}
}
else {
$summary[] = t('Select a responsive image mapping.');
}
// Display the settings for the colorbox if chosen.
if ($settings['image_link'] == 'colorbox') {
$summary[] = t('Colorbox Group: @setting', array('@setting' => $settings['colorbox_settings']['colorbox_group']));
$gallery = array(
'post' => t('Per post gallery'),
'page' => t('Per page gallery'),
'field_post' => t('Per field in post gallery'),
'field_page' => t('Per field in page gallery'),
'custom' => t('Custom'),
'none' => t('No gallery'),
);
$summary[] = t('Colorbox Gallery: @setting', array('@setting' => $gallery[$settings['colorbox_settings']['colorbox_gallery']]));
if ($settings['colorbox_settings']['colorbox_gallery'] == 'custom') {
$summary[] = '→ ' . t('Colorbox Gallery Custom: @setting', array('@setting' => $settings['colorbox_settings']['colorbox_gallery_custom']));
}
$caption = array(
'auto' => t('Automatic'),
'title' => t('Title text'),
'alt' => t('Alt text'),
'node_title' => t('Content title'),
'custom' => t('Custom (with tokens)'),
'none' => t('None'),
);
$summary[] = t('Colorbox Caption: @setting', array('@setting' => $caption[$settings['colorbox_settings']['colorbox_caption']]));
if ($settings['colorbox_settings']['colorbox_caption'] == 'custom') {
$summary[] = '→ ' . t('Colorbox Caption Custom: @setting', array('@setting' => $settings['colorbox_settings']['colorbox_caption_custom']));
}
}
return implode('<br />', $summary);
}
/**
* Helper function.
*
* @see picture_field_formatter_settings_form()
*/
function picture_field_formatter_settings_picture_sizes_formatter_summary($field, $instance, $settings) {
$summary = array();
$summary[] = t('Sizes: @sizes', array('@sizes' => $settings['sizes']));
$image_styles = image_style_options(FALSE);
unset($image_styles['']);
$image_styles[PICTURE_EMPTY_IMAGE] = t('Empty image');
$image_styles[PICTURE_ORIGINAL_IMAGE] = t('Original image');
$selected_styles = array_filter($settings['image_styles']);
$summary[] = t(
'Image styles: @styles',
array(
'@styles' => implode(', ', array_intersect_key($image_styles, $selected_styles)),
)
);
$summary[] = t('Fallback image style: @style', array('@style' => isset($image_styles[$settings['fallback_image_style']]) ? $image_styles[$settings['fallback_image_style']] : $image_styles[PICTURE_EMPTY_IMAGE]));
$link_types = picture_link_types($instance);
// Display this setting only if image is linked.
if (isset($link_types[$settings['image_link']])) {
$summary[] = filter_xss_admin($link_types[$settings['image_link']]);
}
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function picture_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$function = "picture_field_formatter_{$display['type']}_view";
return $function($entity_type, $entity, $field, $instance, $langcode, $items, $display);
}
/**
* Helper function.
*
* @see picture_field_formatter_view()
*/
function picture_field_formatter_picture_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
// Check if the formatter involves a link.
$image_link = $display['settings']['image_link'];
if ($image_link == 'content') {
if (isset($entity->referencing_entity) && isset($entity->referencing_entity_type)) {
$uri = entity_uri($entity->referencing_entity_type, $entity->referencing_entity);
}
else {
$uri = entity_uri($entity_type, $entity);
}
}
elseif ($image_link == 'file') {
$link_file = TRUE;
}
elseif ($image_link) {
if (isset($entity->{$image_link})) {
// Support for field translations.
$language = field_language($entity_type, $entity, $image_link, $langcode);
$link_field = $entity->{$image_link};
if (isset($link_field[$language])) {
$link_values = $link_field[$language];
}
}
elseif (image_style_load($image_link)) {
$link_file = $image_link;
}
}
$fallback_image_style = $settings['fallback_image_style'];
// Support old display settings from 7.x-1.x.
$mapping_name = isset($display['settings']['picture_mapping']) && !empty($display['settings']['picture_mapping']) ? $display['settings']['picture_mapping'] : $display['settings']['picture_group'];
// If we haven't saved a picture mapping for this field previously and
// this isn't a field formatter screen (i.e. panels),
// load a default mapping.
$picture_mapping = FALSE;
if (isset($mapping_name) && !empty($mapping_name)) {
$picture_mapping = picture_mapping_load($mapping_name);
if (!$picture_mapping) {
trigger_error(check_plain("Unable to load picture mapping $mapping_name."), E_USER_ERROR);
return $element;
}
}
else {
$all_mappings = picture_mapping_load_all();
$picture_mapping = reset($all_mappings);
if (!$picture_mapping) {
trigger_error("No picture mappings have been defined yet.", E_USER_ERROR);
return $element;
}
}
$breakpoint_styles = picture_get_mapping_breakpoints($picture_mapping, $fallback_image_style);
// Assume regular display.
$formatter = 'picture_formatter';
$colorbox_breakpoints = array();
$colorbox_fallback_image_style = '';
// Check for colorbox link.
if (module_exists('colorbox') && $display['settings']['image_link'] == 'colorbox') {
$formatter = 'picture_formatter_colorbox';
$mappings = picture_mapping_load($display['settings']['colorbox_settings']['colorbox_group']);
if (!$mappings) {
trigger_error(check_plain("Unable to load picture mapping {$display['settings']['colorbox_settings']['colorbox_group']}."), E_USER_ERROR);
return $element;
}
$colorbox_breakpoints = picture_get_mapping_breakpoints($mappings, $colorbox_fallback_image_style);
}
foreach ($items as $delta => $item) {
if (isset($link_file)) {
$uri = array(
'path' => $link_file === TRUE ? file_create_url($item['uri']) : image_style_url($link_file, $item['uri']),
'options' => array(),
);
}
// Handle multiple link with image values.
if (isset($link_values)) {
if (isset($link_values[$delta]['url'])) {
$uri = array(
'path' => $link_values[$delta]['url'],
'options' => array('attributes' => $link_values[$delta]['attributes']),
);
// Handle query fragment if there is any.
if (!empty($link_values[$delta]['query'])) {
$uri['options']['query'] = $link_values[$delta]['query'];
}
}
// If there are more image values than link values unset the link.
else {
unset($uri);
}
}
$libraries = array(
array('picture', 'picturefill_head'),
array('picture', 'picturefill'),
array('picture', 'picture.ajax'),
);
if (!empty($settings['lazyload'])) {
$libraries[] = array('picture', 'lazysizes');
if (!empty($display['settings']['lazyload_aspect_ratio'])) {
$libraries[] = array('picture', 'lazysizes_aspect_ratio');
}
}
$element[$delta] = array(
'#theme' => $formatter,
'#attached' => array(
'library' => $libraries,
),
'#item' => $item,
'#image_style' => $fallback_image_style,
'#breakpoints' => $breakpoint_styles,
'#path' => isset($uri) ? $uri : '',
'#colorbox_group' => $colorbox_breakpoints,
'#colorbox_image_style' => $colorbox_fallback_image_style,
'#lazyload' => !empty($settings['lazyload']),
'#lazyload_aspect_ratio' => !empty($settings['lazyload_aspect_ratio']),
);
// Add css and js for colorbox.
if ($formatter == 'picture_formatter_colorbox') {
$element[$delta]['#attached']['css'][backdrop_get_path('module', 'picture') . '/css/picture_colorbox.css'] = array('type' => 'file');
$element[$delta]['#attached']['js'][backdrop_get_path('module', 'picture') . '/js/picture_colorbox.js'] = array('type' => 'file');
// TODO This variable was probably removed in Backdrop without replacement.
if (!variable_get('colorbox_inline', 0)) {
$element[$delta]['#attached']['js'][backdrop_get_path('module', 'colorbox') . '/js/colorbox_inline.js'] = array('type' => 'file');
}
$colorbox_settings = $display['settings']['colorbox_settings'];
// Add the group ID.
list($id) = entity_extract_ids($entity_type, $entity);
$entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';
// If this is a file entity field check for the referencing entity to do
// the proper grouping.
if (isset($entity->referencing_entity) && isset($entity->referencing_field)) {
// Because we don't have the entity type we use some "magic" to get a
// unique entity identifier.
$entity_id = spl_object_hash($entity->referencing_entity);
$colorbox_group_field = $entity->referencing_field;
}
else {
$colorbox_group_field = $field['field_name'];
}
switch ($colorbox_settings['colorbox_gallery']) {
case 'post':
$gallery_id = 'gallery-' . $entity_id;
break;
case 'page':
$gallery_id = 'gallery-all';
break;
case 'field_post':
$gallery_id = 'gallery-' . $entity_id . '-' . $colorbox_group_field;
break;