3 namespace Drupal\blazy;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\image\Entity\ImageStyle;
9 * Provides lightbox utilities.
14 * Gets media switch elements: all lightboxes, not content, nor iframe.
16 * @param array $element
17 * The element being modified.
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;
27 // Provide relevant URL if it is a lightbox.
29 $url_attributes['class'] = ['blazy__' . $switch, 'litebox'];
30 $url_attributes['data-' . $switch . '-trigger'] = TRUE;
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;
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);
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'];
55 'width' => $settings['box_width'],
56 'height' => $settings['box_height'],
59 if (!empty($settings['embed_url'])) {
60 $json['scheme'] = $settings['scheme'];
62 $json['height'] = 360;
64 // Force autoplay for media URL on lightboxes, saving another click.
65 $url = empty($settings['autoplay_url']) ? $settings['embed_url'] : $settings['autoplay_url'];
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);
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);
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'];
85 $json['width'] = $settings['box_width'];
86 $json['height'] = $settings['box_height'];
89 if ($switch == 'photobox') {
90 $url_attributes['rel'] = 'video';
94 $url = $settings['box_url'];
97 if ($switch == 'colorbox' && $multiple) {
98 $json['rel'] = empty($settings['id']) ? 'blazy_colorbox' : $settings['id'];
101 $url_attributes['data-media'] = Json::encode($json);
103 if (!empty($settings['box_caption'])) {
104 $element['#captions']['lightbox'] = self::buildCaptions($item, $settings);
107 $element['#url'] = $url;
108 $element['#url_attributes'] = $url_attributes;
112 * Builds lightbox captions.
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.
121 * The renderable array of caption, or empty array.
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'];
129 switch ($settings['box_caption']) {
131 $caption = $alt ?: $title;
144 $alt = $alt ? '<p>' . $alt . '</p>' : '';
145 $title = $title ? '<h2>' . $title . '</h2>' : '';
146 $caption = $settings['box_caption'] == 'alt_title' ? $alt . $title : $title . $alt;
150 $caption = ($entity = $item->getEntity()) ? $entity->label() : '';
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);
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] : '';
172 return empty($caption) ? [] : ['#markup' => $caption];