You are here

function template_preprocess_field_orbit in ZURB Orbit 7

Same name and namespace in other branches
  1. 8 field_orbit.module \template_preprocess_field_orbit()
  2. 7.2 field_orbit.module \template_preprocess_field_orbit()

Implements template_preprocess().

File

./field_orbit.module, line 438
Implement a orbit formatter for fields.

Code

function template_preprocess_field_orbit(&$variables) {
  drupal_add_css(drupal_get_path('module', 'field_orbit') . '/vendor/orbit.css');
  drupal_add_js(array(
    'field_orbit' => array(
      'field-orbit-' . $variables['id'] => $variables['options'],
    ),
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'field_orbit') . '/vendor/jquery.foundation.orbit.min.js', array(
    'scope' => 'footer',
  ));
  drupal_add_js(drupal_get_path('module', 'field_orbit') . '/field_orbit.js', array(
    'scope' => 'footer',
  ));

  // Generate classes
  $variables['classes_array'][] = 'field-orbit-' . $variables['id'];

  // Generate slides
  $field_orbit_zebra = 'odd';
  $variables['slides_max_width'] = 0;
  $variables['slides_max_height'] = 0;
  foreach ($variables['items'] as $num => $item) {

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

    // Generate the image html
    $image = array();
    $image['path'] = $item['uri'];
    $image['attributes']['class'] = array(
      'field-orbit-image',
      'field-orbit-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 (!empty($item['caption'])) {
      $image['attributes']['data-caption'] = '#field-orbit-image-' . ($num + 1) . '-caption';
      $variables['items'][$num]['caption_id'] = 'field-orbit-image-' . ($num + 1) . '-caption';
    }
    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'];
      $variables['items'][$num]['image'] = theme('image_style', $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']}\" />";
    }

    // 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);
      }
    }
  }
}