You are here

function template_preprocess_field_slideshow in Field Slideshow 7

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

Implements template_preprocess().

File

./field_slideshow.module, line 888
Implement a slideshow formatter for fields.

Code

function template_preprocess_field_slideshow(&$variables) {

  // Add js variables
  $js_variables = $variables["js_variables"];
  drupal_add_js(array(
    'field_slideshow' => array(
      'field-slideshow-' . $variables['slideshow_id'] => $js_variables,
    ),
  ), 'setting');

  // Add the JQuery plugins and the JS code
  if (module_exists('libraries')) {
    $path = libraries_get_path('jquery.cycle');
    if (file_exists($path . '/jquery.cycle.all.min.js')) {
      drupal_add_js($path . '/jquery.cycle.all.min.js');
    }
    elseif (file_exists($path . '/jquery.cycle.all.js')) {
      drupal_add_js($path . '/jquery.cycle.all.js');
    }
    if (isset($js_variables['pager']) && $js_variables['pager'] == 'carousel') {
      $path = libraries_get_path('jquery.jcarousel');
      if (file_exists($path . '/lib/jquery.jcarousel.min.js')) {
        drupal_add_js($path . '/lib/jquery.jcarousel.min.js');
      }
      elseif (file_exists($path . '/lib/jquery.jcarousel.js')) {
        drupal_add_js($path . '/lib/jquery.jcarousel.js');
      }
    }
    $path = libraries_get_path('jquery.imagesloaded');
    if (file_exists($path . '/jquery.imagesloaded.min.js')) {
      drupal_add_js($path . '/jquery.imagesloaded.min.js');
    }
    elseif (file_exists($path . '/jquery.imagesloaded.js')) {
      drupal_add_js($path . '/jquery.imagesloaded.js');
    }
    drupal_add_js(drupal_get_path('module', 'field_slideshow') . '/field_slideshow.js');
  }

  // Add css
  drupal_add_css(drupal_get_path('module', 'field_slideshow') . '/field_slideshow.css');

  // Generate classes
  $variables['classes_array'][] = 'field-slideshow-' . $variables['slideshow_id'];
  $variables['classes_array'][] = 'effect-' . $variables['js_variables']['fx'];
  $variables['classes_array'][] = 'timeout-' . $variables['js_variables']['timeout'];
  if (isset($variables['pager']) && $variables['pager'] != '') {
    $variables['classes_array'][] = 'with-pager';
  }
  if (isset($variables['controls'])) {
    $variables['classes_array'][] = 'with-controls';
  }

  // Change order if needed
  if (isset($variables['order'])) {
    if ($variables['order'] == 'reverse') {
      $variables['items'] = array_reverse($variables['items']);
    }
    elseif ($variables['order'] == 'random') {
      shuffle($variables['items']);
    }
  }

  // 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';
  foreach ($variables['items'] as $num => $item) {

    // Generate classes
    $classes = array(
      'field-slideshow-slide',
      'field-slideshow-slide-' . (1 + $num),
    );
    $field_slideshow_zebra = $field_slideshow_zebra == 'odd' ? 'even' : 'odd';
    $classes[] = $field_slideshow_zebra;
    if ($num == 0) {
      $classes[] = 'first';
    }
    elseif ($num == count($variables['items']) - 1) {
      $classes[] = 'last';
    }
    $variables['items'][$num]['classes'] = implode(' ', $classes);
    if (isset($variables['items'][$num]['view_mode'])) {
      $entity = field_collection_item_revision_load($variables['items'][$num]['revision_id']);
      $variables['items'][$num]['rendered_entity'] = entity_view('field_collection_item', array(
        $entity,
      ), $variables['items'][$num]['view_mode']);
    }
    else {

      // Generate the image html
      $image = array();
      $image['path'] = $item['uri'];
      $image['attributes']['class'] = array(
        'field-slideshow-image',
        'field-slideshow-image-' . (1 + $num),
      );
      $image['alt'] = isset($item['alt']) ? $item['alt'] : '';
      if (isset($item['width']) && isset($item['height'])) {
        $image['width'] = $item['width'];
        $image['height'] = $item['height'];
      }
      else {
        $image_dims = getimagesize($image['path']);
        $image['width'] = $image_dims[0];
        $image['height'] = $image_dims[1];
      }
      if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
        $image['title'] = $item['title'];
      }
      if (isset($variables['image_style']) && $variables['image_style'] != '') {
        $image['style_name'] = $variables['image_style'];
        $image['breakpoints'] = $variables['breakpoints'];
        $variables['items'][$num]['image'] = theme($slide_theme, $image);
      }
      else {
        $variables['items'][$num]['image'] = theme('image', $image);
      }

      // Get image sizes and keeps the bigger ones, so height is correctly calculated by Cycle
      $dimensions = array(
        'width' => $image['width'],
        'height' => $image['height'],
      );
      if (isset($variables['image_style']) && $variables['image_style'] != '') {
        if (function_exists('image_style_transform_dimensions')) {
          image_style_transform_dimensions($image['style_name'], $dimensions);
        }

        // manual calculation if Drupal < 7.9 or image_style_transform_dimensions doesn't work
        if (!function_exists('image_style_transform_dimensions') || !is_numeric($dimensions['width'])) {
          $thumbnail_path = image_style_path($variables['image_style'], $image['path']);

          // if thumbnail is not generated, do it, so we can get the dimensions
          if (!file_exists($thumbnail_path)) {
            image_style_create_derivative(image_style_load($variables['image_style']), $image['path'], $thumbnail_path);
          }
          $thumbnail_dims = getimagesize($thumbnail_path);
          $dimensions = array(
            'width' => $thumbnail_dims[0],
            'height' => $thumbnail_dims[1],
          );
        }
      }

      // If the theme function hasn't added width and height attributes to the image, add them
      if (strpos($variables['items'][$num]['image'], 'width=') === FALSE) {
        $variables['items'][$num]['image'] = drupal_substr($variables['items'][$num]['image'], 0, -2) . "width=\"{$dimensions['width']}\" height=\"{$dimensions['height']}\" />";
      }

      // Keeps biggest dimensions
      if ($dimensions['width'] > $variables['slides_max_width']) {
        $variables['slides_max_width'] = $dimensions['width'];
      }
      if ($dimensions['height'] > $variables['slides_max_height']) {
        $variables['slides_max_height'] = $dimensions['height'];
      }
    }

    // Add links if needed
    $links = array(
      'path' => 'image',
    );
    if (isset($item['caption']) && $item['caption'] != '') {
      $links['caption_path'] = 'caption';
    }

    // Loop thru required links (because image and caption can have different links)
    foreach ($links as $link => $out) {
      if (!empty($item[$link])) {
        $path = $item[$link]['path'];
        $options = $item[$link]['options'];

        // When displaying an image inside a link, the html option must be TRUE.
        $options['html'] = TRUE;

        // Generate differnet rel attribute for image and caption, so colorbox doesn't double the image list
        if (isset($options['attributes']['rel'])) {
          $options['attributes']['rel'] .= $out;
        }
        $options = array_merge($options, drupal_parse_url($path));
        $variables['items'][$num][$out] = l($variables['items'][$num][$out], $options['path'], $options);
      }
    }
  }

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