You are here

public static function BlazyLightbox::build in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyLightbox.php \Drupal\blazy\BlazyLightbox::build()
  2. 8 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::preRender in src/BlazyManager.php
Builds the Blazy as a structured array ready for ::renderer().

File

src/BlazyLightbox.php, line 16

Class

BlazyLightbox
Provides lightbox utilities.

Namespace

Drupal\blazy

Code

public static function build(array &$element = []) {
  $item = $element['#item'];
  $settings =& $element['#settings'];
  $type = $settings['type'];
  $uri = $settings['uri'];
  $switch = $settings['media_switch'];
  $switch_css = str_replace('_', '-', $switch);

  // Provide relevant URL if it is a lightbox.
  $url_attributes =& $element['#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['id'] = isset($settings['id']) ? $settings['id'] : 'blazy-' . $switch_css;
  $settings['gallery_id'] = empty($settings['gallery_id']) ? $settings['id'] : $settings['gallery_id'];
  $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'])) {
    image_style_transform_dimensions($settings['box_style'], $dimensions);
    $settings['box_url'] = image_style_url($settings['box_style'], $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'],
  ];

  // This allows PhotoSwipe with videos still swipable.
  if (!empty($settings['box_media_style'])) {
    image_style_transform_dimensions($settings['box_media_style'], $dimensions);
    $settings['box_media_url'] = image_style_url($settings['box_media_style'], $uri);
  }
  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'];

    // This allows PhotoSwipe with videos still swipable.
    if (!empty($settings['box_media_style'])) {
      $settings['box_url'] = $settings['box_media_url'];

      // Allows custom work to override this video size 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') {

    // @todo make Blazy Grid without Blazy Views fields support multiple
    // fields and entities as a gallery group, likely via a class at Views UI.
    // Must use consistent key for multiple entities, hence cannot use id.
    $json['rel'] = 'blazy-' . $settings['gallery_id'];
  }
  $url_attributes['data-media'] = drupal_json_encode($json);
  if (!empty($settings['box_caption'])) {
    $element['#captions']['lightbox'] = self::buildCaptions($item, $settings);
  }
  $element['#url'] = $url;
}