Version 1
[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     $dimensions = ['width' => $settings['box_width'], 'height' => $settings['box_height']];
40     if (!empty($settings['box_style'])) {
41       $box_style = ImageStyle::load($settings['box_style']);
42       $box_style->transformDimensions($dimensions, $uri);
43       $settings['box_url'] = $box_style->buildUrl($uri);
44     }
45
46     // Allows custom work to override this without image style, such as
47     // a combo of image, video, Instagram, Facebook, etc.
48     if (empty($settings['_box_width'])) {
49       $settings['box_width'] = $dimensions['width'];
50       $settings['box_height'] = $dimensions['height'];
51     }
52
53     $json = [
54       'type'   => $type,
55       'width'  => $settings['box_width'],
56       'height' => $settings['box_height'],
57     ];
58
59     if (!empty($settings['embed_url'])) {
60       $json['scheme'] = $settings['scheme'];
61       $json['width']  = 640;
62       $json['height'] = 360;
63
64       // Force autoplay for media URL on lightboxes, saving another click.
65       $url = empty($settings['autoplay_url']) ? $settings['embed_url'] : $settings['autoplay_url'];
66
67       // Provides custom lightbox media dimension, if so configured.
68       // @todo: Remove for Lightbox media style.
69       if (!empty($settings['dimension'])) {
70         list($json['width'], $json['height']) = array_pad(array_map('trim', explode("x", $settings['dimension'], 2)), 2, NULL);
71       }
72
73       // This allows PhotoSwipe with videos still swipable.
74       if (!empty($settings['box_media_style'])) {
75         $box_media_style = ImageStyle::load($settings['box_media_style']);
76         $box_media_style->transformDimensions($dimensions, $uri);
77         $settings['box_url'] = $box_media_style->buildUrl($uri);
78
79         // Allows custom work to override this without image style.
80         if (empty($settings['_box_width'])) {
81           $settings['box_width']  = $dimensions['width'];
82           $settings['box_height'] = $dimensions['height'];
83         }
84
85         $json['width']  = $settings['box_width'];
86         $json['height'] = $settings['box_height'];
87       }
88
89       if ($switch == 'photobox') {
90         $url_attributes['rel'] = 'video';
91       }
92     }
93     else {
94       $url = $settings['box_url'];
95     }
96
97     if ($switch == 'colorbox' && $multiple) {
98       $json['rel'] = empty($settings['id']) ? 'blazy_colorbox' : $settings['id'];
99     }
100
101     $url_attributes['data-media'] = Json::encode($json);
102
103     if (!empty($settings['box_caption'])) {
104       $element['#captions']['lightbox'] = self::buildCaptions($item, $settings);
105     }
106
107     $element['#url'] = $url;
108     $element['#url_attributes'] = $url_attributes;
109   }
110
111   /**
112    * Builds lightbox captions.
113    *
114    * @param object|mixed $item
115    *   The \Drupal\image\Plugin\Field\FieldType\ImageItem item, or array when
116    *   dealing with Video Embed Field.
117    * @param array $settings
118    *   The settings to work with.
119    *
120    * @return array
121    *   The renderable array of caption, or empty array.
122    */
123   public static function buildCaptions($item, array $settings = []) {
124     $title   = empty($item->title) ? '' : $item->title;
125     $alt     = empty($item->alt) ? '' : $item->alt;
126     $delta   = empty($settings['delta']) ? 0 : $settings['delta'];
127     $caption = '';
128
129     switch ($settings['box_caption']) {
130       case 'auto':
131         $caption = $alt ?: $title;
132         break;
133
134       case 'alt':
135         $caption = $alt;
136         break;
137
138       case 'title':
139         $caption = $title;
140         break;
141
142       case 'alt_title':
143       case 'title_alt':
144         $alt     = $alt ? '<p>' . $alt . '</p>' : '';
145         $title   = $title ? '<h2>' . $title . '</h2>' : '';
146         $caption = $settings['box_caption'] == 'alt_title' ? $alt . $title : $title . $alt;
147         break;
148
149       case 'entity_title':
150         $caption = ($entity = $item->getEntity()) ? $entity->label() : '';
151         break;
152
153       case 'custom':
154         $caption = '';
155         if (!empty($settings['box_caption_custom']) && ($entity = $item->getEntity())) {
156           $options = ['clear' => TRUE];
157           $caption = \Drupal::token()->replace($settings['box_caption_custom'], [$entity->getEntityTypeId() => $entity, 'file' => $item], $options);
158
159           // Checks for multi-value text fields, and maps its delta to image.
160           if (strpos($caption, ", <p>") !== FALSE) {
161             $caption = str_replace(", <p>", '| <p>', $caption);
162             $captions = explode("|", $caption);
163             $caption = isset($captions[$delta]) ? $captions[$delta] : '';
164           }
165         }
166         break;
167
168       default:
169         $caption = '';
170     }
171
172     return empty($caption) ? [] : ['#markup' => $caption];
173   }
174
175 }