Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / blazy / src / BlazyLightbox.php
1 <?php
2
3 namespace Drupal\blazy;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\image\Entity\ImageStyle;
7
8 /**
9  * Provides lightbox utilities.
10  */
11 class BlazyLightbox {
12
13   /**
14    * Gets media switch elements: all lightboxes, not content, nor iframe.
15    *
16    * @param array $element
17    *   The element being modified.
18    */
19   public static function build(array &$element = []) {
20     $item     = $element['#item'];
21     $settings = &$element['#settings'];
22     $type     = empty($settings['type']) ? 'image' : $settings['type'];
23     $uri      = $settings['uri'];
24     $switch   = $settings['media_switch'];
25     $multiple = !empty($settings['count']) && $settings['count'] > 1;
26
27     // Provide relevant URL if it is a lightbox.
28     $url_attributes = [];
29     $url_attributes['class'] = ['blazy__' . $switch, 'litebox'];
30     $url_attributes['data-' . $switch . '-trigger'] = TRUE;
31
32     // If it is a video/audio, otherwise image to image.
33     $settings['box_url']    = file_create_url($uri);
34     $settings['icon']       = empty($settings['icon']) ? ['#markup' => '<span class="media__icon media__icon--litebox"></span>'] : $settings['icon'];
35     $settings['lightbox']   = $switch;
36     $settings['box_width']  = isset($item->width) ? $item->width : NULL;
37     $settings['box_height'] = isset($item->height) ? $item->height : NULL;
38
39     $settings['box_width']  = isset($settings['box_width']) ? $settings['box_width'] : $settings['width'];
40     $settings['box_height'] = isset($settings['box_height']) ? $settings['box_height'] : $settings['height'];
41
42     $dimensions = ['width' => $settings['box_width'], 'height' => $settings['box_height']];
43     if (!empty($settings['box_style'])) {
44       $box_style = ImageStyle::load($settings['box_style']);
45       $box_style->transformDimensions($dimensions, $uri);
46       $settings['box_url'] = $box_style->buildUrl($uri);
47     }
48
49     // Allows custom work to override this without image style, such as
50     // a combo of image, video, Instagram, Facebook, etc.
51     if (empty($settings['_box_width'])) {
52       $settings['box_width'] = $dimensions['width'];
53       $settings['box_height'] = $dimensions['height'];
54     }
55
56     $json = [
57       'type'   => $type,
58       'width'  => $settings['box_width'],
59       'height' => $settings['box_height'],
60     ];
61
62     if (!empty($settings['embed_url'])) {
63       $json['scheme'] = $settings['scheme'];
64       $json['width']  = 640;
65       $json['height'] = 360;
66
67       // Force autoplay for media URL on lightboxes, saving another click.
68       $url = empty($settings['autoplay_url']) ? $settings['embed_url'] : $settings['autoplay_url'];
69
70       // Provides custom lightbox media dimension, if so configured.
71       // @todo: Remove for Lightbox media style.
72       if (!empty($settings['dimension'])) {
73         list($json['width'], $json['height']) = array_pad(array_map('trim', explode("x", $settings['dimension'], 2)), 2, NULL);
74       }
75
76       // This allows PhotoSwipe with videos still swipable.
77       if (!empty($settings['box_media_style'])) {
78         $box_media_style = ImageStyle::load($settings['box_media_style']);
79         $box_media_style->transformDimensions($dimensions, $uri);
80         $settings['box_url'] = $box_media_style->buildUrl($uri);
81
82         // Allows custom work to override this without image style.
83         if (empty($settings['box_width'])) {
84           $settings['box_width']  = $dimensions['width'];
85           $settings['box_height'] = $dimensions['height'];
86         }
87
88         $json['width']  = $settings['box_width'];
89         $json['height'] = $settings['box_height'];
90       }
91
92       if ($switch == 'photobox') {
93         $url_attributes['rel'] = 'video';
94       }
95     }
96     else {
97       $url = $settings['box_url'];
98     }
99
100     if ($switch == 'colorbox' && $multiple) {
101       $json['rel'] = empty($settings['id']) ? 'blazy_colorbox' : $settings['id'];
102     }
103
104     $url_attributes['data-media'] = Json::encode($json);
105
106     if (!empty($settings['box_caption'])) {
107       $element['#captions']['lightbox'] = self::buildCaptions($item, $settings);
108     }
109
110     $element['#url'] = $url;
111     $element['#url_attributes'] = $url_attributes;
112   }
113
114   /**
115    * Builds lightbox captions.
116    *
117    * @param object|mixed $item
118    *   The \Drupal\image\Plugin\Field\FieldType\ImageItem item, or array when
119    *   dealing with Video Embed Field.
120    * @param array $settings
121    *   The settings to work with.
122    *
123    * @return array
124    *   The renderable array of caption, or empty array.
125    */
126   public static function buildCaptions($item, array $settings = []) {
127     $title   = empty($item->title) ? '' : $item->title;
128     $alt     = empty($item->alt) ? '' : $item->alt;
129     $delta   = empty($settings['delta']) ? 0 : $settings['delta'];
130     $caption = '';
131
132     switch ($settings['box_caption']) {
133       case 'auto':
134         $caption = $alt ?: $title;
135         break;
136
137       case 'alt':
138         $caption = $alt;
139         break;
140
141       case 'title':
142         $caption = $title;
143         break;
144
145       case 'alt_title':
146       case 'title_alt':
147         $alt     = $alt ? '<p>' . $alt . '</p>' : '';
148         $title   = $title ? '<h2>' . $title . '</h2>' : '';
149         $caption = $settings['box_caption'] == 'alt_title' ? $alt . $title : $title . $alt;
150         break;
151
152       case 'entity_title':
153         $caption = ($entity = $item->getEntity()) ? $entity->label() : '';
154         break;
155
156       case 'custom':
157         $caption = '';
158         if (!empty($settings['box_caption_custom']) && ($entity = $item->getEntity())) {
159           $options = ['clear' => TRUE];
160           $caption = \Drupal::token()->replace($settings['box_caption_custom'], [$entity->getEntityTypeId() => $entity, 'file' => $item], $options);
161
162           // Checks for multi-value text fields, and maps its delta to image.
163           if (!empty($caption) && strpos($caption, ", <p>") !== FALSE) {
164             $caption = str_replace(", <p>", '| <p>', $caption);
165             $captions = explode("|", $caption);
166             $caption = isset($captions[$delta]) ? $captions[$delta] : '';
167           }
168         }
169         break;
170     }
171
172     return empty($caption) ? [] : ['#markup' => $caption];
173   }
174
175 }