You are here

function template_preprocess_slick in Slick Carousel 7.3

Same name and namespace in other branches
  1. 8.2 templates/slick.theme.inc \template_preprocess_slick()
  2. 8 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()

Preprocess variables for theme_slick().

File

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

Code

function template_preprocess_slick(&$variables) {
  _slick_preprocess_container($variables);
  foreach ([
    'options',
    'optionset',
  ] as $key) {
    $variables[$key] = isset($variables['element']["#{$key}"]) ? $variables['element']["#{$key}"] : [];
  }

  // Prepare attributes.
  $settings =& $variables['settings'];
  $settings += SlickDefault::htmlSettings();
  $optionset = $variables['optionset'] ?: Slick::loadWithFallback($settings['optionset']);
  $js = array_merge($optionset
    ->getSettings(), $variables['options']) + SlickDefault::jsSettings();
  $display = $settings['display'];
  $id = $settings['id'] ?: Blazy::getHtmlId('slick');

  // @see SlickManager::buildGrid(), and this should make sense.
  $settings['count'] = isset($settings['count']) ? $settings['count'] : count($variables['items']);
  $settings['unslick'] = $settings['unslick'] || $settings['count'] == 1;
  $attributes =& $variables['attributes_array'];
  $custom_classes = empty($attributes['class']) ? [] : $attributes['class'];
  $attributes['class'] = array_merge([
    'slick',
  ], $custom_classes);
  $attributes['id'] = $display == 'thumbnail' ? $id . '-thumbnail' : $id;
  $content_attributes =& $variables['content_attributes_array'];

  // 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.
  global $language;
  if ($language->direction == 'rtl') {
    $attributes['dir'] = isset($language->dir) ? $language->dir : '';
    $js['rtl'] = $language->direction ? TRUE : FALSE;
  }

  // Prevents broken slick when only one item given, or an enforced unslick.
  if ($settings['unslick']) {
    $attributes['class'][] = 'unslick';
  }
  else {
    if ($settings['count'] <= $js['slidesToShow']) {
      $attributes['class'][] = 'slick--less';
    }

    // Arrows.
    $arrows = $downs = [];
    if ($js['vertical']) {
      $arrows[] = 'slick__arrow--v';
    }

    // Only makes sense for the main display, not thumbnail, overlay.
    if ($display == 'main') {
      if ($settings['skin_arrows']) {
        $arrows[] = 'slick__arrow--' . str_replace('_', '-', $settings['skin_arrows']);
      }

      // Down arrows.
      if (!empty($js['downArrow']) && !empty($js['downArrowTarget'])) {
        $downs['class'] = [
          'slick-down',
        ];
        $downs['data-offset'] = $js['downArrowOffset'];
        $downs['data-target'] = $js['downArrowTarget'];
        $downs['data-role'] = 'none';
        $downs['type'] = 'button';
        $attributes['class'][] = 'slick--has-arrow-down';
      }
    }
    $variables['arrow_down_attributes_array'] = $downs;
    $variables['arrow_attributes_array']['class'] = array_merge([
      'slick__arrow',
    ], $arrows);
    $variables['arrow_attributes_array']['role'] = 'navigation';

    // Main arrows.
    // @todo remove the $settings parts (BC) for $js to reduce dups.
    $js['prevArrow'] = $settings['prev_arrow'] = strip_tags($js['prevArrow']);
    $js['nextArrow'] = $settings['next_arrow'] = strip_tags($js['nextArrow']);

    // 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.
    $js = isset($variables['js']) ? array_merge($js, $variables['js']) : $js;
    $content_attributes['id'] = $attributes['id'] . '-slider';
    $content_attributes['class'][] = 'slick__slider';
    if ($json = $optionset
      ->removeDefaultValues($js)) {
      $content_attributes['data-slick'] = drupal_json_encode($json);
    }
  }

  // Pass js, settings and attributes to theme_slick().
  $variables['js'] = $js;
  $variables['settings'] = $settings;

  // Process individual item, basically converting array to render array.
  foreach ($variables['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;

    // Remove extracted item elements to prevent them from being rendered.
    unset($item['settings'], $item['attributes'], $item['item']);

    // Using non-hard-coded theme_slick_BLAH() is simply for flexibility
    // to extend functionality -- lightbox, grid, main media or thumbnail, etc.
    // before arriving here. This is kind of tiny control room to decide what
    // to do with each item. Note the new vanilla and thumbnail themes.
    $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);
  }

  // Had weird issues with some theme expecting classes array at 2014, satisfy.
  $variables['classes_array'] = $attributes['class'];
}