You are here

slick.module in Slick Carousel 7

Same filename and directory in other branches
  1. 8.2 slick.module
  2. 8 slick.module
  3. 7.3 slick.module
  4. 7.2 slick.module

Slick carousel for Drupal.

File

slick.module
View source
<?php

/**
 * @file
 * Slick carousel for Drupal.
 */

/**
 * Implements hook_views_api().
 */
function slick_views_api() {
  return array(
    'api' => '3',
  );
}

/**
 * Implements hook_theme().
 */
function slick_theme() {
  return array(
    'slick_carousel' => array(
      'variables' => array(
        'items' => NULL,
        'options' => NULL,
      ),
      'template' => 'theme/slick-carousel',
      'file' => 'theme/slick.theme.inc',
    ),
  );
}

/**
 * Implements hook_field_formatter_info().
 */
function slick_field_formatter_info() {
  $info = array(
    'slick' => array(
      'label' => t('Slick carousel'),
      'field types' => array(
        'image',
        'field_collection',
      ),
      'settings' => array(
        'slick_image_style' => 'thumbnail',
      ),
    ),
  );

  // Add slick options as defaults.
  $options = slick_get_options();
  foreach ($options as $name => $values) {
    $info['slick']['settings'][$name] = $values['default'];
  }
  return $info;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function slick_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();
  switch ($field->type) {
    case 'image':
      $image_style_options = image_style_options(FALSE);
      $element['slick_image_style'] = array(
        '#title' => t('Image style'),
        '#type' => 'select',
        '#options' => $image_style_options,
        '#default_value' => $settings['slick_image_style'],
      );
      break;
    case 'field_collection':
      break;
  }

  // Build slick options as form elements.
  $options = slick_get_options();
  foreach ($options as $name => $values) {
    $element[$name] = array(
      '#title' => $values['title'],
      '#description' => $values['description'],
      '#type' => $values['type'] == 'boolean' ? 'checkbox' : 'textfield',
      '#default_value' => $settings[$name],
    );
  }
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function slick_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('Slick carousel');
  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
function slick_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $slick = array(
    '#theme' => 'slick_carousel',
    '#options' => $settings,
  );
  switch ($field['type']) {
    case 'image':
      foreach ($items as $i => $item) {
        $slick['#items'][] = array(
          '#theme' => 'image_style',
          '#path' => $item['uri'],
          '#style_name' => $settings['slick_image_style'],
          '#attributes' => array(
            'alt' => $item['alt'],
            'title' => $item['title'],
          ),
        );
      }
      break;
    case 'field_collection':
      foreach ($items as $i => $item) {
        $field_collection_item = field_collection_item_load($item['value']);
        $slick['#items'][$i] = entity_view('field_collection_item', array(
          $field_collection_item,
        ));
      }
      break;
  }
  return $slick;
}

/**
 * Defines a list of options available for slick.
 */
function slick_get_options() {
  $options = array();
  $options['accessibility'] = array(
    'name' => 'accessibility',
    'title' => t('Accessibility'),
    'description' => t('Enables tabbing and arrow key navigation'),
    'type' => 'boolean',
    'default' => TRUE,
  );
  $options['vertical'] = array(
    'name' => 'vertical',
    'title' => t('Vertical'),
    'description' => t('Vertical slide direction'),
    'type' => 'boolean',
    'default' => FALSE,
  );
  $options['autoplay'] = array(
    'name' => 'autoplay',
    'title' => t('Autoplay'),
    'description' => t('Enables Autoplay'),
    'type' => 'boolean',
    'default' => FALSE,
  );
  $options['autoplayspeed'] = array(
    'name' => 'autoplaySpeed',
    'title' => t('Autoplay speed'),
    'description' => t('Autoplay Speed in milliseconds'),
    'type' => 'integer',
    'default' => 3000,
  );
  $options['arrows'] = array(
    'name' => 'arrows',
    'title' => t('Arrows'),
    'description' => t('Show prev/next arrows'),
    'type' => 'boolean',
    'default' => TRUE,
  );
  $options['dots'] = array(
    'name' => 'dots',
    'title' => t('Dots'),
    'description' => t('Current slide indicator dots'),
    'type' => 'boolean',
    'default' => FALSE,
  );
  $options['draggable'] = array(
    'name' => 'draggable',
    'title' => t('Draggable'),
    'description' => t('Enable mouse dragging'),
    'type' => 'boolean',
    'default' => TRUE,
  );
  $options['swipe'] = array(
    'name' => 'swipe',
    'title' => t('Swipe'),
    'description' => t('Enable swiping'),
    'type' => 'boolean',
    'default' => TRUE,
  );
  $options['touchmove'] = array(
    'name' => 'touchMove',
    'title' => t('TouchMove'),
    'description' => t('Enable slide motion with touch'),
    'type' => 'boolean',
    'default' => TRUE,
  );
  $options['touchthreshold'] = array(
    'name' => 'touchThreshold',
    'title' => t('touchThreshold'),
    'description' => t('Swipe distance threshold'),
    'type' => 'integer',
    'default' => 5,
  );
  $options['fade'] = array(
    'name' => 'fade',
    'title' => t('Fade'),
    'description' => t('Enable fade'),
    'type' => 'boolean',
    'default' => FALSE,
  );
  $options['infinite'] = array(
    'name' => 'infinite',
    'title' => t('Infinite'),
    'description' => t('Infinite loop sliding'),
    'type' => 'boolean',
    'default' => TRUE,
  );
  $options['cssease'] = array(
    'name' => 'cssEase',
    'title' => t('cssEase'),
    'description' => t('CSS3 Animation Easing'),
    'type' => 'varchar',
    'default' => 'ease',
  );
  $options['slidestoshow'] = array(
    'name' => 'slidesToShow',
    'title' => t('Slides to show'),
    'description' => t('Number of slides to show'),
    'type' => 'integer',
    'default' => 1,
  );
  $options['slidestoscroll'] = array(
    'name' => 'slidesToScroll',
    'title' => t('Slides to scroll'),
    'description' => t('Number of slides to scroll'),
    'type' => 'integer',
    'default' => 1,
  );
  $options['speed'] = array(
    'name' => 'speed',
    'title' => t('Speed'),
    'description' => t('Slide speed in milliseconds'),
    'type' => 'integer',
    'default' => 300,
  );
  return $options;
}

/**
 * Implements hook_library().
 */
function slick_library() {
  $slick_library_path = slick_get_slick_path();
  $slick_module_path = drupal_get_path('module', 'slick');
  $libraries = array();

  // Slick plugin.
  $libraries['slick'] = array(
    'title' => 'Slick',
    'website' => 'http://kenwheeler.github.io/slick/',
    'version' => '1.3.2',
    'js' => array(
      $slick_library_path . '/slick/slick.min.js' => array(
        'group' => JS_DEFAULT,
      ),
    ),
    'css' => array(
      $slick_library_path . '/slick/slick.css' => array(
        'group' => CSS_DEFAULT,
      ),
    ),
  );

  // Slick module.
  $libraries['drupal.slick'] = array(
    'title' => 'Slick',
    'version' => '1.0',
    'js' => array(
      $slick_module_path . '/js/slick.js' => array(),
    ),
    'css' => array(
      $slick_module_path . '/css/slick.css' => array(
        'type' => 'file',
        'media' => 'screen',
      ),
    ),
    'dependencies' => array(
      array(
        'system',
        'jquery.once',
      ),
      array(
        'slick',
        'slick',
      ),
    ),
  );
  return $libraries;
}

/**
 * Get the location of the slick library.
 *
 * @return string.
 *   The location of the library, or FALSE if the library isn't installed.
 */
function slick_get_slick_path() {
  if (function_exists('libraries_get_path')) {
    return libraries_get_path('slick');
  }

  // The following logic is taken from libraries_get_libraries()
  $searchdir = array();

  // Similar to 'modules' and 'themes' directories inside an installation
  // profile, installation profiles may want to place libraries into a
  // 'libraries' directory.
  $searchdir[] = 'profiles/' . drupal_get_profile() . '/libraries';

  // Always search sites/all/libraries.
  $searchdir[] = 'sites/all/libraries';

  // Also search sites/<domain>/*.
  $searchdir[] = conf_path() . '/libraries';
  foreach ($searchdir as $dir) {
    if (file_exists($dir . '/slick/slick.min.js')) {
      return $dir . '/slick';
    }
  }
  return FALSE;
}