You are here

public function SlickManager::preRenderSlickWrapper in Slick Carousel 8

Same name and namespace in other branches
  1. 8.2 src/SlickManager.php \Drupal\slick\SlickManager::preRenderSlickWrapper()

File

src/SlickManager.php, line 363

Class

SlickManager
Implements BlazyManagerInterface, SlickManagerInterface.

Namespace

Drupal\slick

Code

public function preRenderSlickWrapper($element) {
  $build = $element['#build'];
  unset($element['#build']);
  if (empty($build['items'])) {
    return [];
  }

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

  // 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']);
    }
  }

  // Additional settings.
  $build['optionset'] = $build['optionset'] ?: Slick::load($settings['optionset']);

  // Ensures deleted optionset while being used doesn't screw up.
  if (empty($build['optionset'])) {
    $build['optionset'] = Slick::load('default');
  }
  $settings['count'] = empty($settings['count']) ? count($build['items']) : $settings['count'];
  $settings['id'] = $id;
  $settings['nav'] = isset($settings['nav']) ? $settings['nav'] : !empty($settings['optionset_thumbnail']) && isset($build['items'][1]);
  $settings['navpos'] = !empty($settings['nav']) && !empty($settings['thumbnail_position']);
  $settings['vertical'] = $build['optionset']
    ->getSetting('vertical');
  $mousewheel = $build['optionset']
    ->getSetting('mouseWheel');
  if ($settings['nav']) {
    $options['asNavFor'] = "#{$thumb_id}-slider";
    $optionset_thumbnail = Slick::load($settings['optionset_thumbnail']);
    $mousewheel = $optionset_thumbnail
      ->getSetting('mouseWheel');
    $settings['vertical_tn'] = $optionset_thumbnail
      ->getSetting('vertical');
  }

  // Attach libraries.
  if ($switch && $switch != 'content') {
    $settings[$switch] = $switch;
  }
  $settings['mousewheel'] = $mousewheel;
  $settings['down_arrow'] = $build['optionset']
    ->getSetting('downArrow');
  $settings['lazy'] = empty($settings['lazy']) ? $build['optionset']
    ->getSetting('lazyLoad') : $settings['lazy'];
  $settings['blazy'] = empty($settings['blazy']) ? $settings['lazy'] == 'blazy' : $settings['blazy'];
  $attachments = $this
    ->attach($settings);
  $build['options'] = $options;
  $build['settings'] = $settings;

  // Build the Slick wrapper elements.
  $element['#settings'] = $settings;
  $element['#attached'] = empty($build['attached']) ? $attachments : NestedArray::mergeDeep($build['attached'], $attachments);

  // Build the main Slick.
  $slick[0] = self::slick($build);

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

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

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