You are here

slider_pro.module in Slider Pro 7

Same filename and directory in other branches
  1. 8 slider_pro.module

File

slider_pro.module
View source
<?php

/**
 * Holds hooks and general functionality.
 */

/**
 * Implements hook_help().
 */
function slider_pro_help($path, $arg) {
  switch ($path) {
    case 'admin/help#slider_pro':
      $output = file_get_contents(drupal_get_path('module', 'slider_pro') . '/README.txt');
      return '<pre>' . check_plain($output) . '</pre>';
  }
}

/**
 * Implements hook_libraries_info().
 */
function slider_pro_libraries_info() {
  $libraries['slider-pro'] = array(
    'name' => 'Slider Pro',
    'vendor url' => 'http://bqworks.com/slider-pro/',
    'download url' => 'https://github.com/bqworks/slider-pro',
    'path' => 'dist',
    'version arguments' => array(
      'file' => 'dist/js/jquery.sliderPro.min.js',
      // Version 1.2.0
      'pattern' => '/- v(1.2\\.+\\d+)/',
      'lines' => 5,
    ),
    'files' => array(
      'js' => array(
        'js/jquery.sliderPro.min.js',
      ),
      'css' => array(
        'css/slider-pro.min.css',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_theme().
 */
function slider_pro_theme() {
  return array(
    'slider_pro_layers_table' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Implements hook_views_api().
 */
function slider_pro_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'slider_pro') . '/views',
  );
}

/**
 * Custom theme function to render layers input table.
 * @param type $vars
 * @return type
 */
function theme_slider_pro_layers_table($vars) {
  $element = $vars['element'];
  $rows = array();
  foreach (element_children($element) as $key) {
    $rows[] = array(
      'data' => array(
        drupal_render($element[$key]['fields']),
        drupal_render($element[$key]['background']),
        drupal_render($element[$key]['data_position']),
        drupal_render($element[$key]['data_show_transition']),
        drupal_render($element[$key]['data_hide_transition']),
        drupal_render($element[$key]['data_show_delay']),
        drupal_render($element[$key]['data_stay_duration']),
        drupal_render($element[$key]['remove']),
        drupal_render($element[$key]['weight']),
      ),
      // To support the tabledrag behaviour, we need to assign each row of the
      // table a class attribute of 'draggable'. This will add the 'draggable'
      // class to the <tr> element for that row when the final table is
      // rendered.
      'class' => array(
        'draggable',
      ),
    );
  }
  $header = array(
    t('Fields'),
    t('Background'),
    t('Position'),
    t('Show transition'),
    t('Hide transition'),
    t('Delay'),
    t('Stay duration'),
    '',
    t('Weight'),
  );

  // Add table dragging.
  drupal_add_tabledrag('slider-pro-layers-table', 'order', 'sibling', 'order-weight');
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => 'slider-pro-layers-table',
    ),
  ));
}

/**
 * Display a view as a Slider Pro style.
 */
function template_preprocess_slider_pro_view(&$vars) {
  $view = $vars['view'];
  $result = $vars['rows'];
  $vars['rows'] = array();

  // Get values from options form.
  $options = $view->style_plugin->options['slider_pro'];

  // Get fields.
  $fields =& $view->field;

  // Render fields with view style plugin.
  $handler = $view->style_plugin;
  $rendered_rows = $handler
    ->render_fields($result);

  // Overwrite $vars['rows'] with rendered fields and thumbnails.
  $fields_on_slide = array_filter($options['fields_on_slide']);
  $fields_on_thumbnail = $vars['use_thumbnails'] ? array_filter($options['thumbnails']['fields_on_thumbs']) : array();

  // Store use_thumbnails boolean.
  $vars['use_thumbnails'] = $options['thumbnails']['thumbnails_position'];
  foreach ($rendered_rows as $num => $row) {
    $rendered_slide = array();
    $rendered_thumb = array();
    $rendered_layers = array();
    foreach ($row as $field_name => $field) {

      // Check if fields has to be displayed on slide.
      if (in_array($field_name, $fields_on_slide)) {
        $rendered_slide[$field_name] = $field;
      }

      // Check if fields has to be displayed on thumb.
      if (in_array($field_name, $fields_on_thumbnail) && $vars['use_thumbnails']) {
        $rendered_thumb[$field_name] = $field;
      }

      // Check if any layers have to be rendered.
      $layers = !empty($options['layers']['table']) ? array_values($options['layers']['table']) : array();
      foreach ($layers as $key => $layer) {
        $render_layer = FALSE;
        $layer_fields = array_filter($layer['fields']);
        if (!empty($layer_fields)) {

          // A layer is configured.
          $render_layer = TRUE;
          if (in_array($field_name, $layer_fields)) {

            // Field has to be on layer.
            $rendered_layers[$key]['fields'][$field_name] = $field;
          }
        }
        if ($render_layer) {
          $rendered_layers[$key]['background'] = $layer['background'];
          $rendered_layers[$key]['data_position'] = $layer['data_position'];
          $rendered_layers[$key]['data_show_transition'] = $layer['data_show_transition'];
          $rendered_layers[$key]['data_hide_transition'] = $layer['data_hide_transition'];
          $rendered_layers[$key]['data_show_delay'] = !empty($layer['data_show_delay']) ? $layer['data_show_delay'] : 500;
          $rendered_layers[$key]['data_stay_duration'] = !empty($layer['data_stay_duration']) ? $layer['data_stay_duration'] : 500;
        }
      }

      // Add classes to fields.
      $vars['field_classes'][$field_name][$num] = array(
        drupal_clean_css_identifier($field_name),
      );
      $classes = $fields[$field_name]
        ->element_classes($num);
      if (!empty($classes)) {
        $vars['field_classes'][$field_name][$num][] = $classes;
      }

      // Store field labels.
      $vars['field_labels'][$field_name][$num] = $fields[$field_name]
        ->label();
    }
    $vars['rows'][$num]['slide'] = $rendered_slide;
    $vars['rows'][$num]['thumb'] = $rendered_thumb;
    $vars['rows'][$num]['layers'] = $rendered_layers;
  }

  // Set unique id, so that multiple instances on one page can be created.
  $unique_id = uniqid();
  $vars['id'] = $unique_id;

  // Add libraries and settings.
  $option_set = _slider_pro_build_option_set($options);
  slider_pro_add($unique_id, $option_set);
}

/**
 * This function loads the required JavaScripts and css for a slider pro instance.
 * @param type $id
 * @param type $option_set
 */
function slider_pro_add($id = NULL, $option_set = NULL) {

  // Add library.
  if (!($library = libraries_load('slider-pro')) || empty($library['loaded'])) {
    drupal_set_message(t('Slider Pro library was not found.'), 'error');
    return FALSE;
  }

  // If the ID or optionset aren't set, it is assumed the settings will be set
  // manually via the calling module/theme
  if (!empty($id) && !empty($option_set)) {

    // JavaScript settings
    $js_settings = array(
      'instances' => array(
        $id => $option_set,
      ),
    );

    // Add settings.
    drupal_add_js(array(
      'sliderPro' => $js_settings,
    ), 'setting');

    // Add loader file.
    drupal_add_js(drupal_get_path('module', 'slider_pro') . '/js/slider_pro_load.js', array(
      'type' => 'file',
      'scope' => 'footer',
    ));
  }
}

/**
 * Builds compatible JS options set from views options form.
 * @param type $options
 */
function _slider_pro_build_option_set($options) {
  return array(
    'width' => $options['width'],
    'height' => $options['height'],
    'orientation' => $options['orientation'],
    'buttons' => $options['buttons'] ? TRUE : FALSE,
    'fade' => $options['transition'] ? TRUE : FALSE,
    'arrows' => $options['arrows'] ? TRUE : FALSE,
    'thumbnailsPosition' => isset($options['thumbnails']['thumbnails_position']) ? $options['thumbnails']['thumbnails_position'] : '',
    'thumbnailWidth' => isset($options['thumbnails']['width']) ? $options['thumbnails']['width'] : '',
    'thumbnailHeight' => isset($options['thumbnails']['height']) ? $options['thumbnails']['height'] : '',
    'waitForLayers' => TRUE,
    'visibleSize' => $options['visible_size'],
    'forceSize' => $options['force_size'],
  );
}

Functions

Namesort descending Description
slider_pro_add This function loads the required JavaScripts and css for a slider pro instance.
slider_pro_help Implements hook_help().
slider_pro_libraries_info Implements hook_libraries_info().
slider_pro_theme Implements hook_theme().
slider_pro_views_api Implements hook_views_api().
template_preprocess_slider_pro_view Display a view as a Slider Pro style.
theme_slider_pro_layers_table Custom theme function to render layers input table.
_slider_pro_build_option_set Builds compatible JS options set from views options form.