You are here

public static function SlickManager::preRenderSlick in Slick Carousel 8

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

Builds the Slick instance as a structured array ready for ::renderer().

File

src/SlickManager.php, line 243

Class

SlickManager
Implements BlazyManagerInterface, SlickManagerInterface.

Namespace

Drupal\slick

Code

public static function preRenderSlick(array $element) {
  $build = $element['#build'];
  unset($element['#build']);
  $settings =& $build['settings'];
  if (empty($build['items'])) {
    return [];
  }

  // Adds helper class if thumbnail on dots hover provided.
  $dots_class = [];
  if (!empty($settings['thumbnail_effect']) && (!empty($settings['thumbnail_style']) || !empty($settings['thumbnail']))) {
    $dots_class[] = 'slick-dots--thumbnail-' . $settings['thumbnail_effect'];
  }

  // Adds dots skin modifier class if provided.
  if (!empty($settings['skin_dots'])) {
    $dots_class[] = Html::cleanCssIdentifier('slick-dots--' . $settings['skin_dots']);
  }
  if ($dots_class && !empty($build['optionset'])) {
    $dots_class[] = $build['optionset']
      ->getSetting('dotsClass') ?: 'slick-dots';
    $js['dotsClass'] = implode(" ", $dots_class);
  }

  // Overrides common options to re-use an optionset.
  if ($settings['display'] == 'main') {
    if (!empty($settings['override'])) {
      foreach ($settings['overridables'] as $key => $override) {
        $js[$key] = empty($override) ? FALSE : TRUE;
      }
    }

    // Build the Slick grid if provided.
    if (!empty($settings['grid']) && !empty($settings['visible_items'])) {
      $build['items'] = self::buildGrid($build['items'], $settings);
    }
  }
  $build['options'] = isset($js) ? array_merge($build['options'], $js) : $build['options'];
  \Drupal::moduleHandler()
    ->alter('slick_optionset', $build['optionset'], $build['settings']);
  foreach ([
    'items',
    'options',
    'optionset',
    'settings',
  ] as $key) {
    $element["#{$key}"] = $build[$key];
  }
  return $element;
}