You are here

function template_preprocess_field_slideshow in Field Slideshow 8

Same name and namespace in other branches
  1. 8.2 field_slideshow.module \template_preprocess_field_slideshow()
  2. 7.2 field_slideshow.module \template_preprocess_field_slideshow()
  3. 7 field_slideshow.module \template_preprocess_field_slideshow()

Implements template_preprocess().

File

./field_slideshow.module, line 66
Preprocess and theme hook functions.

Code

function template_preprocess_field_slideshow(&$variables) {
  $items = $variables['items'];

  // Add the JQuery plugins and the JS code
  $libraries_module_exists = \Drupal::moduleHandler()
    ->moduleExists('libraries');
  $colorbox_module_exists = \Drupal::moduleHandler()
    ->moduleExists('colorbox');
  if ($libraries_module_exists) {
    $libraries_path = DRUPAL_ROOT . '/libraries';

    // Add js variables
    $js_variables = $variables["js_variables"];
    $variables['#attached']['library'][] = 'field_slideshow/field_slideshow.view';
    $variables['#attached']['drupalSettings']['field_slideshow'] = array(
      'field-slideshow-' . $variables['slideshow_id'] => $js_variables,
    );
    if (file_exists($libraries_path . '/jquery.cycle/jquery.cycle.all.min.js')) {
      $variables['#attached']['library'][] = 'field_slideshow/field_slideshow.jquery.cycle.min';
    }
    elseif (file_exists($libraries_path . '/jquery.cycle/jquery.cycle.all.js')) {
      $variables['#attached']['library'][] = 'field_slideshow/field_slideshow.jquery.cycle.all';
    }
    if ($colorbox_module_exists) {
      $colorbox_attachment = \Drupal::service('colorbox.attachment');
      $colorbox_applicable = $colorbox_attachment
        ->isApplicable();
      if ($colorbox_applicable) {
        $colorbox_attachment
          ->attach($variables);
      }
      $variables['#attached']['library'][] = 'colorbox/colorbox-dev';
      $variables['#attached']['library'][] = 'colorbox/default';
      $variables['#attached']['library'][] = 'colorbox/init';
    }
  }

  // Change order if needed
  if (isset($variables['order'])) {
    $getSlide = $items
      ->getValue();
    if ($variables['order'] == 'reverse') {
      $slide_order = array_reverse($getSlide);
      $items
        ->setValue($slide_order);
    }
    elseif ($variables['order'] == 'random') {
      shuffle($getSlide);
      $items
        ->setValue($getSlide);
    }
  }

  // Generate slides
  $field_slideshow_zebra = 'odd';
  $variables['slides_max_width'] = 0;
  $variables['slides_max_height'] = 0;
  $slide_theme = isset($variables['breakpoints']) && isset($variables['breakpoints']['mapping']) && !empty($variables['breakpoints']['mapping']) ? 'picture' : 'image_style';
  $variables['slide'] = array();
  $variables['slides_max_width'] = 0;
  $variables['slides_max_height'] = 0;
  $variables['colorbox_img_path'] = '';
  foreach ($items as $num => $item) {
    if (!empty($item
      ->getValue()['fc_path'])) {
      if ($item
        ->getValue()['fc_path'] != '') {
        $variables['fc_url'] = 1;
        $node_path = \Drupal::url($item
          ->getValue()['fc_path']
          ->getRouteName(), $item
          ->getValue()['fc_path']
          ->getRouteParameters());
        $variables['fc'][$num]['path'] = $node_path;
      }
      else {
        $variables['fc_url'] = '';
      }
      $variables['image'][$num]['#item_attributes'] = array_merge($variables['image'][$num]['#item_attributes'], array(
        'class' => 'field-slideshow-image field-slideshow-image-' . (1 + $num),
      ));
    }
    elseif (!empty($item
      ->getValue()['fc_file_path'])) {
      if ($item
        ->getValue()['fc_file_path'] != '') {
        $variables['fc_url'] = 1;
        $file_path = $item
          ->getValue()['fc_file_path']
          ->getUri();
        $variables['fc'][$num]['path'] = $file_path;
      }
      else {
        $variables['fc_url'] = '';
      }
    }

    // Generate classes
    $field_slideshow_zebra = $field_slideshow_zebra == 'odd' ? 'even' : 'odd';
    $classes[] = $field_slideshow_zebra;
    if ($num == 0) {
      $classes[] = 'first';
    }
    elseif ($num == count($items) - 1) {
      $classes[] = 'last';
    }
    $variables['slide'][$num]['classes'] = implode(' ', $classes);

    //Colorbox
    if (!is_object($item
      ->getValue()['path'])) {
      $variables['colorbox_img_path'][$num]['path'] = $item
        ->getValue()['path']['uri'];
    }
    $variables['image_url'] = $variables['image'][$num]['#url'];
    if ($variables['image_url'] == '' && $variables['colorbox_img_path'][$num]['path'] != '') {
      $variables['colorbox_attributes'][$num] = new Attribute();
      $variables['colorbox_attributes'][$num]['title'] = $items[$num]
        ->getValue()['path']['attributes']['title'];
      $variables['colorbox_attributes'][$num]['rel'] = $items[$num]
        ->getValue()['path']['attributes']['rel'];
      $variables['colorbox_attributes'][$num]['class'] = $items[$num]
        ->getValue()['path']['attributes']['class'];
    }
  }

  // Don't add controls if there's only one image
  if (count($items) == 1) {
    $variables['controls'] = '';
    $variables['pager'] = '';
  }
}