You are here

views_slideshow_royalslider.module in RoyalSlider Integration 7

Views Slideshow: RoyalSlider module.

File

contrib/views_slideshow_royalslider/views_slideshow_royalslider.module
View source
<?php

/**
 * @file
 * Views Slideshow: RoyalSlider module.
 */

/**
 * Implements hook_theme().
 */
function views_slideshow_royalslider_theme() {
  return array(
    'views_slideshow_royalslider_main_frame' => array(
      'variables' => array(
        'vss_id' => NULL,
        'view' => NULL,
        'settings' => NULL,
        'rows' => NULL,
      ),
      'file' => 'views_slideshow_royalslider.theme.inc',
    ),
  );
}

/**
 * Implements hook_views_prerender().
 */
function views_slideshow_royalslider_views_pre_render(&$view) {
  $display = $view->display[$view->current_display];
  if ($view->style_plugin->plugin_name == 'slideshow' && $view->style_options['slideshow_type'] == 'views_slideshow_royalslider') {

    // Get the caption field.
    if (!empty($view->style_options['views_slideshow_royalslider']['caption'])) {
      $caption_field = $view->style_options['views_slideshow_royalslider']['caption'];

      // Add the rsCaption class.
      $field = $display->handler->handlers['field'][$caption_field];
      $field->options['element_class'] .= strlen($field->options['element_class']) > 0 ? ' rsCaption' : 'rsCaption';
    }
    if (!empty($view->style_options['views_slideshow_royalslider']['main_image'])) {
      $main_image_field = $view->style_options['views_slideshow_royalslider']['main_image'];

      // @TODO: Check if there's a cleaner way to get the field name OR change the render array.
      $view_field_name = 'field_' . $main_image_field;
      foreach ($view->result as $i => $result) {
        $image_render_field = current($result->{$view_field_name});
        if (!is_array($image_render_field)) {

          // The result has no value for the field that is selected as its' image field.
          // @TODO: remove the item from the view result
          // unset($view->result[$i]);
        }
        else {
          $theme = $image_render_field['rendered']['#theme'];

          // @TODO: check if the image render field exists.. otherwise it kills views.
          // RoyalSlider views supports both image fields and file fields.
          if ($theme == 'image_formatter') {
            $render_item = _views_slideshow_royalslider_process_image_item($image_render_field['rendered']['#item'], $image_render_field);
            $image_render_field['rendered']['#item'] = $render_item['item'];
            $new_attributes = $render_item['attributes'];
            $image_style = $image_render_field['rendered']['#image_style'];
            $image_path = image_style_url($image_style, $image_render_field['raw']['uri']);
          }
          elseif ($theme == 'file_entity') {
            if ($image_render_field['rendered']['#bundle'] == 'image') {
              $render_item = _views_slideshow_royalslider_process_image_item($image_render_field['rendered']['file']['#item'], $image_render_field);
              $image_render_field['rendered']['file']['#item'] = $render_item['item'];
              $new_attributes = $render_item['attributes'];

              // The image path should use the image style.
              $image_style = $image_render_field['rendered']['file']['#image_style'];
              $image_path = image_style_url($image_style, $image_render_field['raw']['uri']);
            }
            elseif ($image_render_field['rendered']['#bundle'] == 'video') {
              $render_item = _views_slideshow_royalslider_process_video_item($image_render_field['rendered']['file'], $image_render_field);
              $image_render_field['rendered']['file'] = $render_item['item'];
              $new_attributes = $render_item['#attributes'];
              $image_path = image_style_url($render_item['#style_name'], $render_item['#path']);
            }
          }
          else {

            // @TODO: this throws views errors, use another way to let people know they should choose another formatter for their field.
            // drupal_set_message(t('RoyalSlider: The formatter for the image field you selected is not supported.'), 'warning');
          }

          // @TODO: add support for a custom image field for the video?
          // @TODO: cleanup the variable name(s) as it might not be an image field but a video field.
          // Convert the image to a preloaded one if preloading is enabled.
          $optionset_name = isset($view->style_options['views_slideshow_royalslider']['optionset']) ? $view->style_options['views_slideshow_royalslider']['optionset'] : 'default';
          $optionset = royalslider_optionset_load($optionset_name);
          if ($optionset->options['usePreloader'] == TRUE) {

            // @TODO with preload set, check the number of items that should be preloaded.
            // @TODO Is there a cleaner way to get the image style into the view to use in theme_views_slideshow_royalslider_main_frame() ?
            $original_render_field = $image_render_field;
            $image_render_field['rendered'] = array(
              '#theme' => 'link',
              '#path' => $image_path,
              '#text' => $image_render_field['raw']['alt'],
              '#options' => array(
                'attributes' => $new_attributes,
                'html' => FALSE,
              ),
            );
            if (array_key_exists('#image_style', $original_render_field['rendered'])) {
              $image_render_field['rendered']['#image_style'] = $original_render_field['rendered']['#image_style'];
            }
          }
          $view->result[$i]->{$view_field_name} = array(
            $image_render_field,
          );
        }
      }
    }

    // @TODO: figure out how to wrap the loaded image in a link again (to enable clickable slides linking to other content).
  }
}
function _views_slideshow_royalslider_process_image_item($render_item, $image_render_field) {

  // Add the rsImg class to the image field.
  $new_attributes = _views_slideshow_royalslider_new_attributes($render_item, 'image');
  $render_item['attributes'] = $new_attributes;
  $render_item['item'] = $render_item;
  return $render_item;
}
function _views_slideshow_royalslider_process_video_item($render_item, $image_render_field) {
  $new_attributes = _views_slideshow_royalslider_new_attributes($render_item, 'video');

  // @TODO: switch between youtube or vimeo.
  if ($render_item['#theme'] == 'media_youtube_video') {
    $displays = file_displays_load('video', 'default', TRUE);
    $youtube_image_display = 'media_youtube_image';
    if (array_key_exists($youtube_image_display, $displays)) {
      $display = (array) $displays['media_youtube_image'];
      $render_item = media_youtube_file_formatter_image_view(file_load($image_render_field['raw']['fid']), $display, LANGUAGE_NONE);
    }
  }
  elseif ($render_item['#theme'] == 'media_vimeo_video') {
    $displays = file_displays_load('video', 'default', TRUE);
    $vimeo_image_display = 'media_vimeo_image';
    if (array_key_exists($vimeo_image_display, $displays)) {
      $display = (array) $displays['media_vimeo_image'];
      $render_item = media_vimeo_file_formatter_image_view(file_load($image_render_field['raw']['fid']), $display, LANGUAGE_NONE);
    }
  }
  $render_item['#attributes'] = $new_attributes;
  $render_item['item'] = $render_item;
  return $render_item;
}

/**
 * Helper function to create the new attribues for the slide item.
 */
function _views_slideshow_royalslider_new_attributes($render_item, $media_type = 'image') {

  // Add the rsImg class to the image field.
  $original_attributes = array();
  if (array_key_exists('attributes', $render_item)) {
    $original_attributes = $render_item['#options']['attributes'];
  }
  $slider_class = array(
    'class' => array(
      'rsImg',
    ),
  );
  $new_attributes = array_merge($original_attributes, $slider_class);
  if ($media_type == 'video') {
    $new_attributes['data-rsVideo'] = file_create_url($render_item['#uri']);
  }
  return $new_attributes;
}