You are here

function field_slideshow_field_formatter_view in Field Slideshow 7.2

Same name and namespace in other branches
  1. 7 field_slideshow.module \field_slideshow_field_formatter_view()

Implements hook_field_formatter_view().

File

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

Code

function field_slideshow_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  static $slideshow_count;
  $slideshow_count = is_int($slideshow_count) ? $slideshow_count + 1 : 1;

  // Needed when upgrading to 1.1
  if (!isset($settings['slideshow_order'])) {
    $settings['slideshow_order'] = '';
  }
  if (!isset($settings['slideshow_carousel_image_style'])) {
    $settings['slideshow_carousel_image_style'] = '';
  }
  if (!isset($settings['slideshow_carousel_visible'])) {
    $settings['slideshow_carousel_visible'] = '3';
  }
  if (!isset($settings['slideshow_carousel_scroll'])) {
    $settings['slideshow_carousel_scroll'] = '1';
  }
  if (!isset($settings['slideshow_carousel_speed'])) {
    $settings['slideshow_carousel_speed'] = '500';
  }
  if (!isset($settings['slideshow_carousel_vertical'])) {
    $settings['slideshow_carousel_vertical'] = 0;
  }

  // Needed when upgrading to 1.2
  if (!isset($settings['slideshow_colorbox_transition'])) {
    $settings['slideshow_colorbox_transition'] = 'elastic';
  }
  if (!isset($settings['slideshow_colorbox_speed'])) {
    $settings['slideshow_colorbox_speed'] = '350';
  }

  // Needed when upgrading to 1.3
  if (!isset($settings['slideshow_field_collection_image'])) {
    $settings['slideshow_field_collection_image'] = '';
  }
  if (!isset($settings['slideshow_carousel_circular'])) {
    $settings['slideshow_carousel_circular'] = 0;
  }
  if (!isset($settings['slideshow_carousel_follow'])) {
    $settings['slideshow_carousel_follow'] = 0;
  }
  if (!isset($settings['slideshow_pager_position'])) {
    $settings['slideshow_pager_position'] = 'after';
  }
  if (!isset($settings['slideshow_controls_position'])) {
    $settings['slideshow_controls_position'] = 'after';
  }
  if (!isset($settings['slideshow_start_on_hover'])) {
    $settings['slideshow_start_on_hover'] = 0;
  }
  if (!isset($settings['slideshow_carousel_skin'])) {
    $settings['slideshow_carousel_skin'] = '';
  }

  // Check plugins
  if (module_exists('libraries')) {
    $path = libraries_get_path('jquery.cycle');
    if (!file_exists($path . '/jquery.cycle.all.min.js') && !file_exists($path . '/jquery.cycle.all.js')) {
      drupal_set_message(t('JQuery Cycle must be installed in order to run the slideshow. Please go to !page for instructions.', array(
        '!page' => l(t('Status Report'), 'admin/reports/status'),
      )), 'warning', FALSE);
    }
    if ($settings['slideshow_pager'] == 'carousel') {
      $path = libraries_get_path('jquery.jcarousel');
      if (!file_exists($path . '/lib/jquery.jcarousel.min.js') && !file_exists($path . '/lib/jquery.jcarousel.js')) {
        drupal_set_message(t('JCarousel must be installed in order to run the slideshow. Please go to !page for instructions.', array(
          '!page' => l(t('Status Report'), 'admin/reports/status'),
        )), 'warning', FALSE);
      }
    }
  }
  else {
    drupal_set_message(t('Please install the !module module in order to use Field Slideshow.', array(
      '!module' => l('Libraries API', 'http://drupal.org/project/libraries'),
    )), 'warning', FALSE);
  }
  $element = array();
  if ($field['type'] == 'image') {
    foreach ($items as $delta => $item) {
      $items[$delta]['type'] = 'image';
    }
  }

  // Media field support
  if ($field['type'] == 'media') {
    foreach ($items as $delta => $item) {
      $items[$delta] = (array) $item;
      $items[$delta]['uri'] = $item['file']->uri;
      $items[$delta]['type'] = 'media';
    }
  }
  elseif ($field['type'] == 'field_collection') {
    foreach ($items as $delta => $item) {
      $items[$delta] = (array) field_collection_field_get_entity($item);
      $items[$delta]['type'] = 'field_collection_item';
      if ($settings['slideshow_content_type'] == 'image') {
        if (!empty($items[$delta][$settings['slideshow_field_collection_image']][$langcode][0]['uri'])) {
          $items[$delta]['uri'] = $items[$delta][$settings['slideshow_field_collection_image']][$langcode][0]['uri'];
          $items[$delta]['title'] = $items[$delta][$settings['slideshow_field_collection_image']][$langcode][0]['title'];
          $items[$delta]['alt'] = $items[$delta][$settings['slideshow_field_collection_image']][$langcode][0]['alt'];
          $items[$delta]['type'] = 'image';
        }
      }
      else {
        $items[$delta]['view_mode'] = empty($settings['slideshow_view_mode']) ? 'full' : $settings['slideshow_view_mode'];
      }
    }
  }

  // Get correct caption
  if ($settings['slideshow_caption'] != '') {
    foreach ($items as $delta => $item) {
      if ($field['type'] == 'media' || $field['type'] == 'field_collection') {
        if ($field['type'] == 'media') {
          $items[$delta]['caption'] = $items[$delta]['file']->{$settings}['slideshow_caption'];
        }
        elseif ($field['type'] == 'field_collection') {
          $items[$delta]['caption'] = $items[$delta][$settings['slideshow_caption']];
        }
        if (!empty($items[$delta]['caption']) && isset($items[$delta]['caption'][$langcode])) {
          $items[$delta]['caption'] = filter_xss($items[$delta]['caption'][$langcode][0]['value']);
        }
        else {
          $items[$delta]['caption'] = '';
        }
      }
      else {
        $items[$delta]['caption'] = filter_xss($item[$settings['slideshow_caption']]);
      }
    }
  }
  $links = array(
    'slideshow_link' => 'path',
    'slideshow_caption_link' => 'caption_path',
  );
  $content_uri = entity_uri($entity_type, $entity);

  // Get the correct field for identifying entity (used to get correct links)
  $entity_info = entity_get_info($entity_type);
  $entity_id_field = $entity_info['entity keys']['id'];

  // Loop through required links (because image and caption can have different links).
  foreach ($links as $setting => $path) {

    // Check if the formatter involves a link.
    $link_type = '';
    switch ($settings[$setting]) {
      case 'content':
        $link_type = 'content';
        break;
      case 'file':
      case 'colorbox':
        $link_type = 'file';
        break;
      default:
        if (($field['type'] == 'media' || $field['type'] == 'field_collection') && drupal_substr($settings[$setting], 0, 6) == 'field_') {
          $infos = field_info_field($settings[$setting]);
          $link_type = $infos['type'];
        }
        break;
    }

    // Generate special links (other than node)
    foreach ($items as $delta => $item) {
      $uri = array();
      switch ($link_type) {
        case 'content':
          $uri = $content_uri;
          break;
        case 'file':
          $uri = array(
            'path' => file_create_url($item['uri']),
            'options' => array(),
          );
          if ($settings[$setting] == 'colorbox') {

            // check if we need a thumbnaill and change the link
            if ($settings['slideshow_colorbox_image_style'] != '') {
              $uri['path'] = image_style_url($settings['slideshow_colorbox_image_style'], $item['uri']);
            }

            // add correct attributes
            $uri['options']['attributes'] = array(
              'class' => array(
                'colorbox',
              ),
              'rel' => 'field-slideshow[' . $entity_id_field . '-' . $entity->{$entity_id_field} . $field["id"] . ']',
            );
            if ($settings['slideshow_caption'] != '') {
              $uri['options']['attributes']['title'] = $item['caption'];
            }
            if (isset($settings['slideshow_colorbox_slideshow']) && $settings['slideshow_colorbox_slideshow'] != '') {
              $uri['options']['attributes']['class'] = array(
                'colorbox-load',
              );
              $uri['path'] .= strpos($uri['path'], '?') === FALSE ? '?' : '&';
              $uri['path'] .= 'slideshow=true&slideshowAuto=' . ($settings['slideshow_colorbox_slideshow'] == 'automatic' ? 'true' : 'false') . '&slideshowSpeed=' . $settings['slideshow_colorbox_slideshow_speed'] . '&speed=' . $settings['slideshow_colorbox_speed'] . '&transition=' . $settings['slideshow_colorbox_transition'];
            }
          }
          break;
        case 'link_field':
          if ($field['type'] == 'media') {
            $pathfield = $item['file']->{$settings}[$setting];
          }
          elseif ($field['type'] == 'field_collection') {
            $pathfield = $item[$settings[$setting]];
          }
          if (isset($pathfield[$langcode])) {
            $uri = array(
              'path' => $pathfield[$langcode][0]['url'],
              'options' => array(),
            );
          }
          break;
        case 'node_reference':
          if ($field['type'] == 'media') {
            $pathfield = $item['file']->{$settings}[$setting];
          }
          elseif ($field['type'] == 'field_collection') {
            $pathfield = $item[$settings[$setting]];
          }
          if (isset($pathfield[$langcode])) {
            $uri = array(
              'path' => drupal_get_path_alias('node/' . $pathfield[$langcode][0]['nid'], $langcode),
              'options' => array(),
            );
          }
          break;
        case 'entityreference':
          if ($field['type'] == 'media') {
            $pathfield = $item['file']->{$settings}[$setting];
          }
          elseif ($field['type'] == 'field_collection') {
            $pathfield = $item[$settings[$setting]];
          }
          if (isset($pathfield[$langcode])) {
            $uri = array(
              'path' => drupal_get_path_alias('node/' . $pathfield[$langcode][0]['target_id'], $langcode),
              'options' => array(),
            );
          }
          break;
      }
      $items[$delta][$path] = !empty($uri) ? $uri : '';
    }
  }
  if (count($items)) {
    $pager = array(
      '#theme' => 'field_slideshow_pager',
      '#items' => $items,
      '#pager' => $settings['slideshow_pager'],
      '#pager_image_style' => $settings['slideshow_pager_image_style'],
      '#carousel_image_style' => $settings['slideshow_carousel_image_style'],
      '#slideshow_id' => $slideshow_count,
      '#carousel_skin' => $settings['slideshow_carousel_skin'],
    );
    $controls = array(
      '#theme' => 'field_slideshow_controls',
      '#slideshow_id' => $slideshow_count,
      '#controls_pause' => $settings['slideshow_controls_pause'],
    );
    $element[] = array(
      '#theme' => 'field_slideshow',
      '#items' => $items,
      '#image_style' => $settings['slideshow_image_style'],
      '#file_style' => $settings['slideshow_file_style'],
      '#order' => $settings['slideshow_order'],
      '#controls' => $settings['slideshow_controls'] === 1 ? $controls : array(),
      '#controls_position' => $settings['slideshow_controls_position'],
      '#pager' => $settings['slideshow_pager'] !== "" ? $pager : array(),
      '#pager_position' => $settings['slideshow_pager_position'],
      '#entity' => $entity,
      '#slideshow_id' => $slideshow_count,
      '#js_variables' => array(
        'fx' => $settings['slideshow_fx'],
        'speed' => $settings['slideshow_speed'],
        'timeout' => $settings['slideshow_timeout'],
        'pause' => $settings['slideshow_pause'],
        'start_on_hover' => $settings['slideshow_start_on_hover'],
        'carousel_visible' => $settings['slideshow_carousel_visible'],
        'carousel_scroll' => $settings['slideshow_carousel_scroll'],
        'carousel_speed' => $settings['slideshow_carousel_speed'],
        'carousel_vertical' => $settings['slideshow_carousel_vertical'],
        'carousel_circular' => $settings['slideshow_carousel_circular'],
        'carousel_follow' => $settings['slideshow_carousel_follow'],
        'carousel_skin' => $settings['slideshow_carousel_skin'],
        // Need to access the following variables in js too
        'pager' => $settings['slideshow_pager'],
        'controls' => $settings['slideshow_controls'] === 1 ? $controls : array(),
      ),
    );
  }
  return $element;
}