You are here

public static function BlazyLightbox::build in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/BlazyLightbox.php \Drupal\blazy\BlazyLightbox::build()
  2. 7 src/BlazyLightbox.php \Drupal\blazy\BlazyLightbox::build()

Gets media switch elements: all lightboxes, not content, nor iframe.

Parameters

array $element: The element being modified.

1 call to BlazyLightbox::build()
BlazyManager::preRenderImage in src/BlazyManager.php
Builds the Blazy image as a structured array ready for ::renderer().

File

src/BlazyLightbox.php, line 20

Class

BlazyLightbox
Provides lightbox utilities.

Namespace

Drupal\blazy

Code

public static function build(array &$element = []) {
  $item = $element['#item'];
  $settings =& $element['#settings'];
  $type = empty($settings['type']) ? 'image' : $settings['type'];
  $uri = $settings['uri'];
  $switch = $settings['media_switch'];
  $switch_css = str_replace('_', '-', $switch);
  $multiple = !empty($settings['count']) && $settings['count'] > 1;

  // Provide relevant URL if it is a lightbox.
  $url_attributes = [];
  $url_attributes['class'] = [
    'blazy__' . $switch_css,
    'litebox',
  ];
  $url_attributes['data-' . $switch_css . '-trigger'] = TRUE;

  // If it is a video/audio, otherwise image to image.
  $settings['box_url'] = file_create_url($uri);
  $settings['icon'] = empty($settings['icon']) ? [
    '#markup' => '<span class="media__icon media__icon--litebox"></span>',
  ] : $settings['icon'];
  $settings['lightbox'] = $switch;
  $settings['box_width'] = isset($item->width) ? $item->width : (empty($settings['width']) ? NULL : $settings['width']);
  $settings['box_height'] = isset($item->height) ? $item->height : (empty($settings['height']) ? NULL : $settings['height']);
  $dimensions = [
    'width' => $settings['box_width'],
    'height' => $settings['box_height'],
  ];
  if (!empty($settings['box_style'])) {
    $box_style = ImageStyle::load($settings['box_style']);
    $box_style
      ->transformDimensions($dimensions, $uri);
    $settings['box_url'] = $box_style
      ->buildUrl($uri);
  }

  // Allows custom work to override this without image style, such as
  // a combo of image, video, Instagram, Facebook, etc.
  if (empty($settings['_box_width'])) {
    $settings['box_width'] = $dimensions['width'];
    $settings['box_height'] = $dimensions['height'];
  }
  $json = [
    'type' => $type,
    'width' => $settings['box_width'],
    'height' => $settings['box_height'],
  ];
  if (!empty($settings['embed_url'])) {
    $json['scheme'] = $settings['scheme'];
    $json['width'] = 640;
    $json['height'] = 360;

    // Force autoplay for media URL on lightboxes, saving another click.
    $url = empty($settings['autoplay_url']) ? $settings['embed_url'] : $settings['autoplay_url'];

    // Provides custom lightbox media dimension, if so configured.
    // @todo: Remove for Lightbox media style.
    if (!empty($settings['dimension'])) {
      list($json['width'], $json['height']) = array_pad(array_map('trim', explode("x", $settings['dimension'], 2)), 2, NULL);
    }

    // This allows PhotoSwipe with videos still swipable.
    if (!empty($settings['box_media_style'])) {
      $box_media_style = ImageStyle::load($settings['box_media_style']);
      $box_media_style
        ->transformDimensions($dimensions, $uri);
      $settings['box_url'] = $box_media_style
        ->buildUrl($uri);

      // Allows custom work to override this without image style.
      if (empty($settings['box_width'])) {
        $settings['box_width'] = $dimensions['width'];
        $settings['box_height'] = $dimensions['height'];
      }
      $json['width'] = $settings['box_width'];
      $json['height'] = $settings['box_height'];
    }
    if ($switch == 'photobox') {
      $url_attributes['rel'] = 'video';
    }
  }
  else {
    $url = $settings['box_url'];
  }
  if ($switch == 'colorbox' && $multiple) {
    $json['rel'] = empty($settings['id']) ? 'blazy_colorbox' : $settings['id'];
  }
  $url_attributes['data-media'] = Json::encode($json);
  if (!empty($settings['box_caption'])) {
    $element['#captions']['lightbox'] = self::buildCaptions($item, $settings);
  }
  $element['#url'] = $url;
  $element['#url_attributes'] = $url_attributes;
}