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 $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'];
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);
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'];
58 'width' => $settings['box_width'],
59 'height' => $settings['box_height'],
62 if (!empty($settings['embed_url'])) {
63 $json['scheme'] = $settings['scheme'];
65 $json['height'] = 360;
67 // Force autoplay for media URL on lightboxes, saving another click.
68 $url = empty($settings['autoplay_url']) ? $settings['embed_url'] : $settings['autoplay_url'];
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);
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);
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'];
88 $json['width'] = $settings['box_width'];
89 $json['height'] = $settings['box_height'];
92 if ($switch == 'photobox') {
93 $url_attributes['rel'] = 'video';
97 $url = $settings['box_url'];
100 if ($switch == 'colorbox' && $multiple) {
101 $json['rel'] = empty($settings['id']) ? 'blazy_colorbox' : $settings['id'];
104 $url_attributes['data-media'] = Json::encode($json);
106 if (!empty($settings['box_caption'])) {
107 $element['#captions']['lightbox'] = self::buildCaptions($item, $settings);
110 $element['#url'] = $url;
111 $element['#url_attributes'] = $url_attributes;
115 * Builds lightbox captions.
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.
124 * The renderable array of caption, or empty array.
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'];
132 switch ($settings['box_caption']) {
134 $caption = $alt ?: $title;
147 $alt = $alt ? '<p>' . $alt . '</p>' : '';
148 $title = $title ? '<h2>' . $title . '</h2>' : '';
149 $caption = $settings['box_caption'] == 'alt_title' ? $alt . $title : $title . $alt;
153 $caption = ($entity = $item->getEntity()) ? $entity->label() : '';
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);
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] : '';
172 return empty($caption) ? [] : ['#markup' => $caption];