You are here

function template_preprocess_slick in Slick Carousel 8.2

Same name and namespace in other branches
  1. 8 templates/slick.theme.inc \template_preprocess_slick()
  2. 7.3 templates/slick.theme.inc \template_preprocess_slick()
  3. 7 theme/slick.theme.inc \template_preprocess_slick()
  4. 7.2 templates/slick.theme.inc \template_preprocess_slick()

Prepares variables for slick.html.twig templates.

File

templates/slick.theme.inc, line 17
Hooks and preprocess functions for the Slick module.

Code

function template_preprocess_slick(&$variables) {
  $element = $variables['element'];
  $defaults = SlickDefault::htmlSettings();
  $settings = isset($element['#settings']) ? array_merge($defaults, $element['#settings']) : $defaults;
  $customs = isset($element['#options']) ? $element['#options'] : [];
  $optionset = isset($element['#optionset']) ? $element['#optionset'] : Slick::loadWithFallback($settings['optionset']);
  $js = $customs ? array_merge($optionset
    ->getSettings(), $customs) : $optionset
    ->getSettings();

  // Prepare attributes.
  $attributes =& $variables['attributes'];
  $custom_classes = empty($attributes['class']) ? [] : $attributes['class'];
  $attributes['class'] = array_merge([
    'slick',
  ], $custom_classes);
  $content_attributes = new Attribute();
  $display = $variables['display'] = $settings['display'];
  $id = empty($settings['id']) ? Blazy::getHtmlId('slick') : $settings['id'];
  $attributes['id'] = $display == 'thumbnail' ? $id . '-thumbnail' : $id;

  // @see SlickManager::buildGrid(), and this should make sense.
  $settings['count'] = isset($settings['count']) ? $settings['count'] : count($element['#items']);
  $settings['unslick'] = $settings['unslick'] || $settings['count'] == 1;
  foreach (SlickDefault::jsSettings() as $key => $default) {
    $settings[$key] = isset($js[$key]) ? strip_tags($js[$key]) : $default;
  }

  // Blazy can still lazyload an unslick.
  // The lazy supercedes JS lazyLoad for background, breakpoints, media, etc.
  $settings['lazy'] = $settings['lazy'] ?: $js['lazyLoad'];
  if ($display != 'thumbnail' && ($settings['lazy'] == 'blazy' || !empty($settings['blazy']))) {
    $js['lazyLoad'] = 'blazy';
  }

  // Make slick language-direction-aware.
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  if ($language
    ->getDirection() == 'rtl') {
    $attributes['dir'] = $language
      ->getDirection();
    $js['rtl'] = $language
      ->getDirection() ? TRUE : FALSE;
  }

  // Remove settings that aren't supported by the active library.
  Slick::removeUnsupportedSettings($js);

  // Prevents broken slick when only one item given, or an enforced unslick.
  if (!$settings['unslick']) {
    $content_attributes
      ->setAttribute('id', $attributes['id'] . '-slider');
    $variables['arrow_attributes'] = new Attribute();
    $variables['arrow_attributes']['role'] = 'navigation';
    if ($display == 'main' && !empty($js['downArrow']) && !empty($js['downArrowTarget'])) {
      $variables['arrow_down_attributes'] = new Attribute();
    }

    // focusOnSelect won't work with empty slide value, so add proper selector.
    // Respects core Grid markups which may wrap .slick__slide within anon DIV.
    // Unfortunately focusOnSelect is removed by Accessible Slick. We kept it
    // here for just in case it will be re-enacted due to being overlooked.
    if (empty($js['slide']) && !empty($js['focusOnSelect'])) {

      // Different versions have different logic causing breaking changes.
      // Basically the fundamental issue: `rows > 0` vs `rows > 1`. This is
      // enough to break slides with duplicate slide markups + anonymous DIV.
      // What we do here is to accommodate both versions.
      $add = $js['rows'] < 2 && $js['slidesPerRow'] < 2;

      // Fix for breaking changes with Slick 1.8.1/1.9.0 and Accessible Slick.
      // This is no use for Accessible Slick due to no-synching w/ thumbnail.
      if (!empty($settings['breaking'])) {
        $add = $js['rows'] < 2;
      }
      $js['slide'] = $add ? '.slick__slide' : $js['slide'];
    }

    // Add the configuration as JSON object into the slick container.
    $js = isset($variables['js']) ? array_merge($js, $variables['js']) : $js;
    if ($json = $optionset
      ->removeDefaultValues($js)) {
      $content_attributes
        ->setAttribute('data-slick', Json::encode($json));
    }
  }

  // Pass settings and attributes to twig.
  $variables['js'] = $js;
  $variables['settings'] = $settings;
  $variables['content_attributes'] = $content_attributes;

  // Process individual item.
  $variables['items'] = [];
  foreach ($element['#items'] as $delta => $item) {
    $item_settings = isset($item['settings']) ? array_merge($settings, $item['settings']) : $settings;
    $item_attributes = isset($item['attributes']) ? $item['attributes'] : [];
    $item_settings['current_item'] = $display;
    unset($item['settings'], $item['attributes'], $item['item']);
    $theme = $settings['vanilla'] ? 'vanilla' : ($display == 'thumbnail' ? 'thumbnail' : 'slide');
    $slide = [
      '#theme' => 'slick_' . $theme,
      '#item' => $item,
      '#delta' => $delta,
      '#settings' => $item_settings,
      '#attributes' => $item_attributes,
    ];
    $variables['items'][$delta] = $slide;
    unset($slide);
  }
}