You are here

views_nivo_slider.module in Views Nivo Slider 7.3

The implementation of Views Nivo Slider module.

File

views_nivo_slider.module
View source
<?php

/**
 * @file
 * The implementation of Views Nivo Slider module.
 */

/**
 * Implementation of hook_views_api().
 */
function views_nivo_slider_views_api() {
  return array(
    'api' => '3',
  );
}

/**
 * Implementation of template preprocess for the view
 */
function template_preprocess_views_nivo_slider_view(&$vars) {
  $view = $vars['view'];
  $options = $vars['options'];
  $vars['views_nivo_slider_id'] = drupal_html_id('views-nivo-slider-' . $view->name . '-' . $view->current_display);
  $vars['classes_array'][] = 'slider-wrapper';
  $vars['classes_array'][] = 'theme-' . $options['style'];

  // Prevent problems on update versions of this module
  if (is_string($options['effect'])) {
    $options['effect'] = array(
      $options['effect'] => $options['effect'],
    );
  }
  $cfg = array();
  $params = array(
    'startSlide' => 0,
    'effect' => join(',', array_keys($options['effect'])),
    'slices' => (int) $options['slices'],
    'animSpeed' => (int) $options['animSpeed'],
    'pauseTime' => (int) $options['pauseTime'],
    'directionNav' => $options['directionNav'] == 'true' ? TRUE : FALSE,
    'directionNavHide' => $options['directionNavHide'] == 'true' ? TRUE : FALSE,
    'controlNav' => $options['controlNav'] != 'disabled' ? TRUE : FALSE,
    'controlNavThumbs' => FALSE,
    'pauseOnHover' => $options['pauseOnHover'] == 'true' ? TRUE : FALSE,
    'manualAdvance' => $options['manualAdvance'] == 'true' ? TRUE : FALSE,
    'captionOpacity' => (double) $options['captionOpacity'],
    'beforeChange' => $options['advanced_settings']['beforeChange'] == '' ? 'function(){}' : $options['advanced_settings']['beforeChange'],
    'afterChange' => $options['advanced_settings']['afterChange'] == '' ? 'function(){}' : $options['advanced_settings']['afterChange'],
    'slideshowEnd' => $options['advanced_settings']['slideshowEnd'] == '' ? 'function(){}' : $options['advanced_settings']['slideshowEnd'],
    'lastSlide' => $options['advanced_settings']['lastSlide'] == '' ? 'function(){}' : $options['advanced_settings']['lastSlide'],
    'afterLoad' => $options['advanced_settings']['afterLoad'] == '' ? 'function(){}' : $options['advanced_settings']['afterLoad'],
  );

  // 2.7.x version options
  if ($options['nivo_version'] == '2.7.x') {
    $params['version'] = '2.7.x';
    $params['controlNavThumbsFromRel'] = FALSE;

    //$params['controlNavThumbsSearch'] = '';

    //$params['controlNavThumbsReplace'] = '';
    $params['keyboardNav'] = $options['keyboardNav'] == 'true' ? TRUE : FALSE;
  }
  if ($options['controlNav'] == 'thumbsIC') {
    $params['controlNavThumbs'] = TRUE;
    if ($options['nivo_version'] == '2.7.x') {
      $params['controlNavThumbsFromRel'] = TRUE;

      //$params['controlNavThumbsSearch'] = "/{$options['imagefield_preset']}/";

      //$params['controlNavThumbsReplace'] = "/{$options['controlNavThumbsIC']}/";
    }
    $vars['classes_array'][] = 'nivo-control-thumbs';
  }
  elseif ($options['controlNav'] == 'thumbsField') {
    $params['controlNavThumbs'] = TRUE;
    $params['controlNavThumbsFromRel'] = TRUE;
    $vars['classes_array'][] = 'nivo-control-thumbs';
  }

  // hide when only single slide
  if (count($view->result) <= 1) {
    $params['directionNav'] = FALSE;
    $params['controlNav'] = FALSE;
  }
  $cfg[$vars['views_nivo_slider_id']] = $params;
  drupal_add_js(array(
    'views_nivo_slider' => $cfg,
  ), 'setting');
  views_nivo_slider_add_libraries($options['style']);
}

/**
 * Implementation of template preprocess for the view fields
 */
function template_preprocess_views_nivo_slider_view_nivo_sliderfields(&$vars) {
  $view =& $vars['view'];
  $options = $vars['options'];
  $style = $view->style_options;
  $row = $vars['row'];
  $presetname = NULL;
  if (is_object($view)) {
    if (isset($view->field[$options['image_field']])) {
      if (is_object($view->field[$options['image_field']])) {
        if (isset($view->field[$options['image_field']]->options['settings']['image_style'])) {
          $presetname = $view->field[$options['image_field']]->options['settings']['image_style'];
        }
      }
    }
  }
  $thumbnail_url = $title = $link = NULL;

  // Force URL format for image_field
  if (isset($view->field[$options['image_field']])) {

    //$view->field[$options['image_field']]->options['type'] = 'path_plain';
    $view->field[$options['image_field']]->options['alter']['make_link'] = 0;
  }
  if (isset($style['controlNav'])) {
    if ($style['controlNav'] == 'thumbsField') {
      $view->field[$style['controlNavThumbsField']]->options['settings']['image_style'] = $style['controlNavThumbsICField'];

      //$view->field[$style['controlNavThumbsField']]->options['format'] = 'path_plain';
      $view->field[$style['controlNavThumbsField']]->options['alter']['make_link'] = 0;
    }
  }

  // link
  if (isset($view->field[$options['link_field']])) {

    //Node: Link
    if ($view->field[$options['link_field']]->definition['handler'] == 'views_handler_field_node_link') {
      $link = url('node/' . $view->result[$view->row_index]->nid);
    }
    else {
      $view->field[$options['link_field']]->options['format'] = 'plain';
    }
  }
  $attributes = NULL;
  $title_id = '';
  foreach ($view->field as $id => $field) {
    $field_output = $view->field[$id]
      ->theme($vars['row']);
    switch ($id) {
      case $options['image_field']:

        //$image_url = $field_output;
        $field_image_name = "field_{$options['image_field']}";

        // No image - no content.
        if (!isset($row->{$field_image_name}[0]['raw']['uri'])) {
          $vars['content'] = NULL;
          return;
        }
        $image_url = $field_output = $row->{$field_image_name}[0]['raw']['uri'];
        break;
      case $options['title_field']:
        $title = $field_output;
        $title_id = $title ? drupal_clean_css_identifier($id . "-" . $field->view->row_index) : '';
        if ($title) {
          $element_id = drupal_html_id($view->name . '-' . $view->current_display . '-' . $title_id);
          $view->_vns_captions[$element_id] = $title;
          $title_id = '#' . $element_id;
        }
        break;
      case $options['link_field']:
        if ($field->definition['handler'] != 'views_handler_field_node_link') {
          $link = check_url($field_output);
        }
        break;
      case $style['controlNavThumbsField']:
        if ($style['controlNav'] == 'thumbsField') {
          $field_image_name = "field_data_{$style['controlNavThumbsField']}_node_values";
          $field_output = $row->{$field_image_name}[0]['uri'];
          if (!empty($style['controlNavThumbsICField'])) {
            $turl = image_style_url($style['controlNavThumbsICField'], $field_output);
          }
          else {
            $turl = $field_output;
          }
          if ($style['nivo_version'] == '2.7.x') {
            $attributes = array(
              'rel' => $turl,
            );
          }
          else {
            $attributes = array(
              'data-thumb' => $turl,
            );
          }
        }
        break;
    }
  }
  if (isset($style['controlNav'])) {
    if ($style['controlNav'] == 'thumbsIC') {
      $turl = image_style_url($style['controlNavThumbsIC'], $image_url);
      if ($style['nivo_version'] == '2.7.x') {
        $attributes = array(
          'rel' => $turl,
        );
      }
      else {
        $attributes = array(
          'data-thumb' => $turl,
        );
      }
    }
  }
  $params = array(
    'style_name' => isset($presetname) ? $presetname : NULL,
    'path' => isset($image_url) ? $image_url : NULL,
    'title' => isset($title_id) ? $title_id : NULL,
    'attributes' => isset($attributes) ? $attributes : NULL,
  );
  $img = !$presetname ? theme('image', $params) : theme('image_style', $params);
  if ($link) {
    $img = l($img, $link, array(
      'html' => TRUE,
      'attributes' => array(
        'class' => array(
          'views-processed',
        ),
      ),
    ));
  }
  $vars['content'] = $img;
}

/**
 * Add the needed libraries and css.
 */
function views_nivo_slider_add_libraries($style = 'default') {

  // Don't load javascript unless libraries module is present.
  if (module_exists('libraries')) {
    $lib_path = libraries_get_path('nivo-slider');

    // Load jQuery plugin
    if (!empty($lib_path)) {

      // Attempt to use minified version of jQuery nivo slider plugin.
      if (file_exists($lib_path . '/jquery.nivo.slider.pack.js')) {
        drupal_add_js($lib_path . '/jquery.nivo.slider.pack.js');
      }
      elseif (file_exists($lib_path . '/jquery.nivo.slider.js')) {
        drupal_add_js($lib_path . '/jquery.nivo.slider.js');
      }

      // Load css
      drupal_add_css($lib_path . '/nivo-slider.css');
      if ($style != 'none') {
        drupal_add_css($lib_path . '/themes/' . $style . '/' . $style . '.css');
      }
    }
  }

  // Load js
  $module_path = drupal_get_path('module', 'views_nivo_slider');
  drupal_add_js($module_path . '/views_nivo_slider.js');
  drupal_add_css($module_path . '/views_nivo_slider.css');
}

/**
 * List the available themes.
 */
function views_nivo_slider_list_themes() {
  static $themes;
  if (isset($themes)) {
    return $themes;
  }
  $themes = array();
  if (module_exists('libraries')) {
    $dir = libraries_get_path('nivo-slider') . '/themes';
    if (!empty($dir) && $dir != NULL) {
      if (is_dir($dir)) {
        if ($handle = opendir($dir)) {
          while (FALSE !== ($filename = readdir($handle))) {
            if ($filename[0] != '.') {
              $uri = "{$dir}/{$filename}";
              $uri = file_stream_wrapper_uri_normalize($uri);
              if (is_dir($uri)) {
                $themes[drupal_strtolower($filename)] = t(drupal_ucfirst($filename));
              }
            }
          }
        }
      }
      natcasesort($themes);
    }
  }

  // Insert None into the head
  array_splice($themes, 0, 0, array(
    'none' => t('None'),
  ));
  return $themes;
}

Functions

Namesort descending Description
template_preprocess_views_nivo_slider_view Implementation of template preprocess for the view
template_preprocess_views_nivo_slider_view_nivo_sliderfields Implementation of template preprocess for the view fields
views_nivo_slider_add_libraries Add the needed libraries and css.
views_nivo_slider_list_themes List the available themes.
views_nivo_slider_views_api Implementation of hook_views_api().