You are here

public function SlickManager::preRenderWrapper in Slick Carousel 7.3

File

src/SlickManager.php, line 507

Class

SlickManager
Implements SlickManagerInterface.

Namespace

Drupal\slick

Code

public function preRenderWrapper($element) {
  $build = $element['#build'];
  unset($element['#build']);

  // One slick_theme() to serve multiple displays: main, overlay, thumbnail.
  $settings = array_merge(SlickDefault::htmlSettings(), $build['settings']);
  $id = $settings['id'] ?: Blazy::getHtmlId('slick');
  $thumb_id = $id . '-thumbnail';
  $options = $build['options'];
  $switch = $settings['media_switch'];
  $thumbs = isset($build['thumb']) ? $build['thumb'] : [];

  // Prevents unused thumb going through the main display.
  unset($build['thumb']);

  // Supports programmatic options defined within skin definitions to allow
  // addition of options with other libraries integrated with Slick without
  // modifying optionset such as for Zoom, Reflection, Slicebox, Transit, etc.
  if (!empty($settings['skin']) && ($skins = $this
    ->getSkinsByGroup('main'))) {
    if (isset($skins[$settings['skin']]['options'])) {
      $options = array_merge($options, $skins[$settings['skin']]['options']);
    }
  }

  // Load the optionset to work with.
  $optionset = $build['optionset'] ?: Slick::loadWithFallback($settings['optionset']);
  $settings['count'] = empty($settings['count']) ? count($build['items']) : $settings['count'];
  $settings['id'] = $id;
  $settings['nav'] = $settings['nav'] ?: !empty($settings['optionset_thumbnail']) && isset($build['items'][1]);
  $settings['navpos'] = $settings['nav'] && !empty($settings['thumbnail_position']);
  $settings['vertical'] = $optionset
    ->getSetting('vertical');
  $mousewheel = $optionset
    ->getSetting('mouseWheel');

  // If thumbnail navigation is required, build one.
  if ($settings['nav']) {
    $options['asNavFor'] = "#{$thumb_id}-slider";
    $optionset_thumbnail = Slick::loadWithFallback($settings['optionset_thumbnail']);
    $mousewheel = $optionset_thumbnail
      ->getSetting('mouseWheel');
    $settings['vertical_tn'] = $optionset_thumbnail
      ->getSetting('vertical');
  }
  else {

    // Pass extra attributes such as those from Commerce product variations to
    // theme_slick() since we have no asNavFor wrapper here.
    if (isset($element['#attributes'])) {
      $build['attributes'] = empty($build['attributes']) ? $element['#attributes'] : NestedArray::mergeDeep($build['attributes'], $element['#attributes']);
    }
  }

  // Attach libraries.
  if ($switch && $switch != 'content') {
    $settings[$switch] = empty($settings[$switch]) ? $switch : $settings[$switch];
  }

  // Supports Blazy multi-breakpoint or lightbox images if provided.
  // Cases: Blazy within Views gallery, or references without direct image.
  if (!empty($settings['check_blazy']) && !empty($settings['first_image'])) {
    $this
      ->isBlazy($settings, $settings['first_image']);
  }

  // Pass needed build items into slick.
  $settings['mousewheel'] = $mousewheel;
  $settings['down_arrow'] = $optionset
    ->getSetting('downArrow');
  $settings['lazy'] = $settings['lazy'] ?: $optionset
    ->getSetting('lazyLoad');
  $settings['blazy'] = empty($settings['blazy']) ? $settings['lazy'] == 'blazy' : $settings['blazy'];
  $settings = array_filter($settings);
  $build['options'] = $options;
  $build['optionset'] = $optionset;
  $build['settings'] = $settings;

  // Build the Slick wrapper elements, and add attachments.
  $attachments = $this
    ->attach($settings);
  $element['#settings'] = $settings;
  $element['#attached'] = empty($build['attached']) ? $attachments : NestedArray::mergeDeep($build['attached'], $attachments);
  $element['#attributes'] = $this
    ->prepareWrapperAttributes($settings);

  // Build the main Slick.
  $slick[0] = $this
    ->slick($build);

  // Build the thumbnail Slick.
  if (!empty($settings['nav']) && $thumbs) {
    $build = [];
    foreach ([
      'items',
      'options',
      'settings',
    ] as $key) {
      $build[$key] = isset($thumbs[$key]) ? $thumbs[$key] : [];
    }
    $settings = array_merge($settings, $build['settings']);
    $settings['optionset'] = $settings['optionset_thumbnail'];
    $settings['skin'] = $settings['skin_thumbnail'];
    $settings['display'] = 'thumbnail';
    $build['optionset'] = $optionset_thumbnail;
    $build['settings'] = array_filter($settings);
    $build['options']['asNavFor'] = "#{$id}-slider";
    $slick[1] = $this
      ->slick($build);
  }

  // Reverse slicks if thumbnail position is provided to get CSS float work.
  if (!empty($settings['navpos'])) {
    $slick = array_reverse($slick);
  }

  // Collect the slick instances.
  $element['#items'] = $slick;
  unset($build);
  return $element;
}