You are here

function template_preprocess_slick in Slick Carousel 8

Same name and namespace in other branches
  1. 8.2 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 15
Hooks and preprocess functions for the Slick module.

Code

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

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

  // Sniffs for Views to allow block__no_wrapper, views__no_wrapper, etc.
  // @todo: Use $attributes if TWIG class ordering puts $attributes to the last.
  if ($settings['view_name'] && $settings['current_view_mode']) {
    $settings['attributes']['class'][] = str_replace('_', '-', 'slick--view--' . $settings['view_name']);
    $settings['attributes']['class'][] = str_replace('_', '-', 'slick--view--' . $settings['view_name'] . '--' . $settings['current_view_mode']);
  }

  // @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 (Slick::jsSettings() as $key => $default) {
    $settings[$key] = isset($js[$key]) ? strip_tags($js[$key]) : $default;
  }

  // Blazy can still lazyload an unslick.
  $settings['lazy'] = empty($settings['lazy']) ? $js['lazyLoad'] : $settings['lazy'];
  if ($display != 'thumbnail') {
    if ($settings['lazy'] == 'blazy' || !empty($settings['blazy'])) {
      if ($settings['lazy'] == 'blazy') {
        $js['lazyLoad'] = 'blazy';
      }
      $attributes['data-blazy'] = empty($settings['blazy_data']) ? '' : Json::encode($settings['blazy_data']);
    }
    if (!empty($settings['media_switch']) && empty($settings['grid'])) {
      $switch = str_replace('_', '-', $settings['media_switch']);
      $attributes['data-' . $switch . '-gallery'] = TRUE;
    }
  }

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

  // 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();
    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.
    if (empty($js['slide']) && !empty($js['focusOnSelect'])) {
      $js['slide'] = $js['rows'] == 1 && $js['slidesPerRow'] == 1 ? '.slick__slide' : $js['slide'];
    }

    // Add the configuration as JSON object into the slick container.
    $variables['js'] = isset($variables['js']) ? array_merge($js, $variables['js']) : $js;
    if ($json = $optionset
      ->removeDefaultValues($variables['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) {
    $settings['current_item'] = $display;
    $settings = isset($item['settings']) ? array_merge($settings, $item['settings']) : $settings;
    $item_attributes = isset($item['attributes']) ? $item['attributes'] : [];
    unset($item['settings'], $item['attributes']);
    $theme = $settings['vanilla'] ? 'vanilla' : ($display == 'thumbnail' ? 'thumbnail' : 'slide');
    $slide = [
      '#theme' => 'slick_' . $theme,
      '#item' => $item,
      '#delta' => $delta,
      '#settings' => $settings,
      '#attributes' => $item_attributes,
    ];
    $variables['items'][$delta] = $slide;
    unset($slide);
  }
}