You are here

function royalslider_preprocess_royalslider_item in RoyalSlider Integration 7

Implements hook_preprocess_HOOK().

Preprocess variables for the RoyalSlider Item template.

File

theme/royalslider.theme.inc, line 68
Theme functions for RoyalSlider.

Code

function royalslider_preprocess_royalslider_item(&$variables) {

  // Only set slider dimensions once.
  static $slider_size_added = FALSE;
  $optionset = $variables['optionset'];
  $item = $variables['item'];

  // Tag is defined by optionset, not individual item.
  $variables['tag'] = $optionset->options['usePreloader'] ? 'a' : 'img';
  $add_classes = array(
    'rsImg',
  );
  $variables['classes_array'] = array_unique(array_merge($variables['classes_array'], $add_classes));
  $attributes = array(
    'class' => $variables['classes_array'],
  );
  if (!empty($item['alt'])) {
    $attributes['alt'] = $item['alt'];
  }
  if (!empty($item['title'])) {
    $attributes['title'] = $item['title'];
    $variables['caption'] = $item['title'];
  }
  if (array_key_exists('row', $item)) {
    $variables['row'] = $item['row'];
  }

  // Handle image and slider dimensions.
  $manual_width = !empty($optionset->options['imgWidth']);
  $manual_height = !empty($optionset->options['imgHeight']);
  if ($optionset->options['drupalAutoSetImageDimensions'] || $manual_width || $manual_height) {

    // Grab image dimensions.
    if (empty($item['width']) || empty($item['height'])) {
      $image_info = image_get_info($item['uri']);
      $item['width'] = $image_info['width'];
      $item['height'] = $image_info['height'];
    }
    $dimensions = array(
      'width' => $item['width'],
      'height' => $item['height'],
    );

    // Resize if we're using an image preset.
    // @TODO: This should be skipped when item is part of a views_slideshow.
    if (!empty($optionset->imagestyle_normal)) {
      image_style_transform_dimensions($optionset->imagestyle_normal, $dimensions);
    }

    // Automatic image dimensions.
    if ($optionset->options['drupalAutoSetImageDimensions']) {
      $attributes['data-rsw'] = $dimensions['width'];
      $attributes['data-rsh'] = $dimensions['height'];
    }
    else {
      if ($manual_width || $manual_height) {
        if ($manual_width) {
          $attributes['data-rsw'] = $optionset->options['imgWidth'];
        }
        if ($manual_height) {
          $attributes['data-rsh'] = $optionset->options['imgHeight'];
        }
      }
    }
  }

  // Handle full-screen images.
  if ($optionset->options['fullscreen']['enabled']) {
    $image_style = $optionset->imagestyle_fullscreen;
    $attributes['data-rsBigImg'] = !empty($image_style) ? _royalslider_image_style($image_style, $item['uri']) : file_create_url($item['uri']);
  }

  // Handle thumbnail images.
  if ($optionset->options['controlNavigation'] === 'thumbnails') {
    $image_style = $optionset->imagestyle_thumbnail;
    $attributes['data-rsTmb'] = !empty($image_style) ? _royalslider_image_style($image_style, $item['uri']) : file_create_url($item['uri']);
  }

  // Set main image url.
  $image_style = $optionset->imagestyle_normal;
  $variables['url'] = !empty($image_style) ? _royalslider_image_style($image_style, $item['uri']) : file_create_url($item['uri']);
  if (isset($item['path'])) {
    $variables['path'] = $item['path'];
  }

  // Set slider dimensions.
  if ($variables['optionset']->options['autoScaleSlider'] && !empty($dimensions)) {

    // Only add them once.
    if (!$slider_size_added) {
      drupal_add_js(array(
        'royalslider' => array(
          'instances' => array(
            $variables['royalslider_id'] => array(
              'slider_height' => $dimensions['height'],
              'slider_width' => $dimensions['width'],
            ),
          ),
        ),
      ), 'setting');
    }
  }

  // Finally, set attributes.
  $variables['attributes_array'] = $attributes;
}