You are here

function template_preprocess_slick in Slick Carousel 7.2

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.3 templates/slick.theme.inc \template_preprocess_slick()
  4. 7 theme/slick.theme.inc \template_preprocess_slick()

Prepares variables for slick templates.

Default template: slick.tpl.php.

@variables array: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #items, #settings, #options, #optionset, #attached.

    • #settings is set via sub-modules and serves various purposes, and not related to JS settings, mostly slide layouts or attaching assets.
    • #options is set programmatically, or hand-crafted, and only accepts direct key|value pairs related to JS settings, or an optionset name.
    • #optionset, if supplied, ensures the optionset loaded once, and cached.

File

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

Code

function template_preprocess_slick(&$variables) {
  $defaults = array(
    'current_display' => 'main',
    'optionset' => 'default',
    'skin_arrows' => '',
  );
  $element = $variables['element'];
  $settings = isset($element['#settings']) ? array_merge($defaults, $element['#settings']) : $defaults;
  $customs = isset($element['#options']) ? $element['#options'] : array();
  $name = isset($customs['optionset']) ? strip_tags($customs['optionset']) : $settings['optionset'];
  $optionset = isset($element['#optionset']) && is_object($element['#optionset']) ? $element['#optionset'] : slick_optionset_load($name);
  $general = $optionset->options['general'];
  $goodies = $general['goodies'];
  $js = $customs ? array_merge($optionset->options['settings'], $customs) : $optionset->options['settings'];
  $skin = empty($settings['skin']) ? $optionset->skin : $settings['skin'];
  $display = $settings['current_display'];
  $id = empty($settings['id']) ? slick_html_id('slick') : $settings['id'];

  // Allows manipulating markups with an enforced unslick.
  $settings['count'] = empty($settings['count']) ? count($element['#items']) : $settings['count'];
  $settings['skin_arrows'] = empty($settings['skin_arrows']) ? '' : ' slick__arrow--' . str_replace('_', '-', $settings['skin_arrows']);
  $settings['unslick'] = !empty($settings['unslick']) || $settings['count'] == 1;
  $settings['slidesToShow'] = $js['slidesToShow'];

  // Prepare attributes, supports Omega 4 alike, or regular.
  $attributes =& $variables['attributes_array'];
  $attributes['class'] = isset($attributes['class']) ? $attributes['class'] : array(
    'slick',
  );
  $attributes['id'] = $id;
  $content_attributes =& $variables['content_attributes_array'];
  if ($display == 'thumbnail') {
    $attributes['id'] = $id . '-thumbnail';
    $skin = empty($settings['skin_thumbnail']) ? $optionset->skin : $settings['skin_thumbnail'];
  }
  elseif ($display == 'main') {

    // Some settings are only reasonable for the main display, not thumbnail.
    $settings['has_pattern'] = !empty($settings['has_pattern']) || !empty($goodies['pattern']);
    if (!empty($settings['media_switch']) && strpos($settings['media_switch'], 'box') !== FALSE) {
      $swicther = str_replace('-switch', '', $settings['media_switch']);
      $attributes['class'][] = 'slick--' . $swicther;
    }
  }

  // Sniffs for Views to allow block__no_wrapper, views__no_wrapper, etc.
  if (!empty($settings['view_name']) && !empty($settings['current_view_mode'])) {
    $attributes['class'][] = str_replace('_', '-', 'slick--view--' . $settings['view_name']);
    $attributes['class'][] = str_replace('_', '-', 'slick--view--' . $settings['view_name'] . '--' . $settings['current_view_mode']);
  }

  // @todo: Remove temp fix for when total <= slidesToShow.
  // @see https://github.com/kenwheeler/slick/issues/262
  if ($settings['count'] <= $settings['slidesToShow']) {
    $attributes['class'][] = 'slick--less';
  }

  // Consistent styling is always needed even for an unslick.
  if ($skin) {
    foreach (array(
      'boxed',
      'split',
    ) as $key) {
      if ($skin !== $key && strpos($skin, $key) !== FALSE) {
        $attributes['class'][] = 'slick--skin--' . $key;
      }
    }
    $attributes['class'][] = str_replace('_', '-', 'slick--skin--' . $skin);
    $settings['skin'] = $skin;
  }
  $attributes['class'][] = str_replace('_', '-', 'slick--optionset--' . $name);
  if (!empty($general['template_class'])) {
    $attributes['class'][] = $general['template_class'];
  }

  // Prevents broken slick when only one item given, or an enforced unslick.
  if (!empty($settings['unslick'])) {
    $attributes['class'][] = 'unslick';
  }
  else {
    $js['randomize'] = !empty($goodies['random']);
    $content_attributes['class'][] = 'slick__slider';
    $content_attributes['id'] = $attributes['id'] . '-slider';

    // The slider must have the attribute "dir" set to "rtl", if so configured.
    global $language;
    $attributes['dir'] = $language->direction ? 'rtl' : 'ltr';
    $js['rtl'] = $language->direction ? TRUE : FALSE;

    // Arrows are enforced to allow responsive options hide/show them.
    $settings['prev_arrow'] = strip_tags($js['prevArrow']);
    $settings['next_arrow'] = strip_tags($js['nextArrow']);

    // Adds helper class if thumbnail on dots hover provided.
    $js['dotsClass'] = empty($js['dotsClass']) ? 'slick-dots' : $js['dotsClass'];

    // @todo drop backward compatibility.
    if (!empty($settings['thumbnail_hover'])) {
      $settings['thumbnail_effect'] = 'hover';
    }
    if (!empty($settings['thumbnail_style']) && !empty($settings['thumbnail_effect'])) {
      $js['dotsClass'] .= ' slick-dots--thumbnail slick-dots--thumbnail-' . $settings['thumbnail_effect'];
    }

    // Adds dots skin modifier class if provided.
    if (!empty($settings['skin_dots'])) {
      $js['dotsClass'] .= ' ' . str_replace('_', '-', 'slick-dots--' . $settings['skin_dots']);
    }
    $js['asNavFor'] = empty($settings['asnavfor_target']) ? $js['asNavFor'] : $settings['asnavfor_target'];
    if (!empty($js['asNavFor'])) {

      // Only if asNavFor, enforce clickable thumbnail, otherwise no joy.
      if ($display == 'thumbnail') {
        $js['focusOnSelect'] = TRUE;
      }
      $attributes['class'][] = 'slick--display--' . $display;
    }

    // focusOnSelect won't work with empty slide value, so add proper selector.
    if (empty($js['slide']) && $js['focusOnSelect']) {
      $js['slide'] = $js['rows'] == 1 && $js['slidesPerRow'] == 1 ? '.slick__slide' : $js['slide'];
    }
    $has_arrow_down = !empty($goodies['arrow-down']) && !empty($general['arrow_down_target']);
    if ($display == 'main' && $has_arrow_down) {
      $attributes['class'][] = 'slick--has-arrow-down';
      $arrow_down['class'] = array(
        'slick-down',
        'jump-scroll',
      );
      $arrow_down['data-target'] = $general['arrow_down_target'];
      $arrow_down['data-offset'] = $general['arrow_down_offset'];
      $variables['arrow_down'] = '<button' . drupal_attributes($arrow_down) . '></button>';
    }

    // Add the configuration as JSON object into the slick container.
    $js_data = _slick_remove_default_optionset_options($optionset, $js, $settings);
    if (!isset($content_attributes['data-slick']) && $js_data) {
      $content_attributes['data-slick'] = drupal_json_encode($js_data);
    }
  }
  $variables['settings'] = $settings;

  // Process individual item.
  $variables['items'] = array();
  foreach ($element['#items'] as $delta => $item) {
    $settings['current_item'] = $display;
    $settings = isset($item['settings']) ? array_merge($settings, $item['settings']) : $settings;
    $slide = array(
      '#theme' => 'slick_item',
      '#item' => isset($item['slide']) ? $item['slide'] : $item,
      '#caption' => empty($item['caption']) ? array() : array_filter($item['caption']),
      '#delta' => $delta,
      '#settings' => $settings,
    );
    $variables['items'][$delta] = $slide;
    unset($slide);
  }
  $variables['classes_array'] = $attributes['class'];
}