You are here

lightgallery.module in Lightgallery 7

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

File

lightgallery.module
View source
<?php

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

// Move the field-based logic (field hooks and related) into another file
// purely for DX. This file still has to be globally included as field
// hooks don't work as expected if formally defined in hook_hook_info().
require_once dirname(__FILE__) . '/includes/lightgallery.field.inc';

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

/**
 * Implements hook_libraries_info().
 */
function lightgallery_libraries_info() {
  $libraries['lightgallery'] = array(
    'name' => 'Lightgallery',
    'vendor url' => 'http://sachinchoolur.github.io/lightGallery/',
    'download url' => 'https://github.com/sachinchoolur/lightGallery',
    'path' => 'dist',
    'version arguments' => array(
      'file' => 'dist/js/lightgallery.min.js',
      // Version 1.2.4
      'pattern' => '/- v(1.2.*)/',
      'lines' => 5,
    ),
    'files' => array(
      'js' => array(
        'js/lightgallery.min.js',
        'js/lg-autoplay.min.js',
        'js/lg-fullscreen.min.js',
        'js/lg-thumbnail.min.js',
        'js/lg-video.min.js',
        'js/lg-zoom.min.js',
        'js/lg-hash.min.js',
      ),
      'css' => array(
        'css/lightgallery.min.css',
        'css/lg-transitions.min.css',
      ),
    ),
  );
  return $libraries;
}

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

/**
 * Implements hook_theme().
 */
function lightgallery_theme() {
  return array(
    'lightgallery_image_formatter' => array(
      'variables' => array(
        'items' => NULL,
        'entity_type' => NULL,
        'entity' => NULL,
        'node' => NULL,
        // Left for legacy support.
        'field' => array(),
        'display_settings' => array(),
      ),
      'file' => 'includes/lightgallery.theme.inc',
    ),
    'lightgallery_item_list' => array(
      'variables' => array(
        'items' => array(),
        'unque_id' => NULL,
      ),
      'path' => drupal_get_path('module', 'lightgallery') . '/templates',
      'template' => 'lightgallery-item-list',
    ),
  );
}

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

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

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

  // Render fields with view style plugin.
  $handler = $view->style_plugin;
  $rendered_rows = $handler
    ->render_fields($result);
  $thumb_field = $options['thumb_field'];
  $image_field = $options['image_field'];
  $title_field = $options['title'];

  // Overwrite $vars['rows'] with rendered fields and thumbnails.
  foreach ($rendered_rows as $num => $row) {
    $rendered_slide = FALSE;
    $rendered_thumb = FALSE;
    $rendered_title = FALSE;
    foreach ($row as $field_name => $field) {

      // Check if field is thumb field.
      if ($field_name == $thumb_field) {
        $rendered_thumb = $field;
      }

      // Check if field is image field.
      if ($field_name == $image_field) {
        $rendered_slide = $field;
      }

      // Check if field is title field.
      if ($field_name == $title_field) {
        $rendered_title = $field;
      }

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

    // Set row style to display: hidden or block.
    $vars['row_styles'][$num] = 'display: block;';
  }

  // 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 = _lightgallery_build_option_set($options);
  lightgallery_add($unique_id, $option_set);
}

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

  // Add library.
  if (!($library = libraries_load('lightgallery')) || empty($library['loaded'])) {
    drupal_set_message(t('Lightgallery 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(
      'lightgallery' => $js_settings,
    ), 'setting');

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

/**
 * Builds compatible JS options set from views options form.
 * @param type $options
 */
function _lightgallery_build_option_set($options) {
  $option_set = array(
    'mode' => !empty($options['mode']) ? $options['mode'] : 'lg-slide',
    'preload' => !empty($options['preload']) ? $options['preload'] : 1,
    'loop' => !empty($options['loop']) ? TRUE : FALSE,
    'closable' => !empty($options['closable']) ? TRUE : FALSE,
    'escKey' => !empty($options['esc_key']) ? TRUE : FALSE,
    'keyPress' => !empty($options['key_press']) ? TRUE : FALSE,
    'contols' => !empty($options['contols']) ? TRUE : FALSE,
    'mousewheel' => !empty($options['mouse_wheel']) ? TRUE : FALSE,
    'download' => !empty($options['download']) ? TRUE : FALSE,
    'counter' => !empty($options['counter']) ? TRUE : FALSE,
    'enableDrag' => !empty($options['drag']) ? TRUE : FALSE,
    'enableTouch' => !empty($options['touch']) ? TRUE : FALSE,
    'thumbnail' => !empty($options['thumbnails']) ? TRUE : FALSE,
    'autoplay' => !empty($options['autoplay']) ? TRUE : FALSE,
    'fullScreen' => !empty($options['full_screen']) ? TRUE : FALSE,
    'pager' => !empty($options['pager']) ? TRUE : FALSE,
    'zoom' => !empty($options['zoom']) ? TRUE : FALSE,
    'hash' => !empty($options['hash']) ? TRUE : FALSE,
    'selector' => !empty($options['selector']) ? $options['selector'] : FALSE,
  );
  if ($option_set['thumbnail']) {

    // Add extra thumb options.
    $option_set['animateThumb'] = !empty($options['animate_thumb']) ? TRUE : FALSE;
    $option_set['currentPagerPosition'] = !empty($options['current_pager_position']) ? $options['current_pager_position'] : 'middle';
    $option_set['thumbWidth'] = !empty($options['thumb_width']) ? $options['thumb_width'] : 100;
    $option_set['thumbContHeight'] = !empty($options['thumb_cont_height']) ? $options['thumb_cont_height'] : 100;
  }
  if ($option_set['autoplay']) {

    // Add extra autoplay options.
    $option_set['pause'] = !empty($options['pause']) ? $options['pause'] : 5000;
    $option_set['progressBar'] = !empty($options['progress_bar']) ? TRUE : FALSE;
    $option_set['autoplayControls'] = !empty($options['autoplay_controls']) ? TRUE : FALSE;
  }
  if ($option_set['zoom']) {

    // Add extra autoplay options.
    $option_set['scale'] = !empty($options['scale']) ? $options['scale'] : 1;
  }
  if ($option_set['hash']) {

    // Add extra autoplay options.
    $option_set['galleryId'] = !empty($options['gallery_id']) ? $options['gallery_id'] : 1;
  }
  if ($option_set['selector']) {
    $option_set['selector'] = !empty($options['selector']) ? $options['selector'] : FALSE;
  }
  return $option_set;
}

/**
 * Fetches available lightgallery modes.
 */
function _lightgallery_get_modes() {
  return drupal_map_assoc(array(
    'lg-slide',
    'lg-fade',
    'lg-zoom-in',
    'lg-zoom-in-big',
    'lg-zoom-out',
    'lg-zoom-out-big',
    'lg-zoom-out-in',
    'lg-zoom-in-out',
    'lg-soft-zoom',
    'lg-scale-up',
    'lg-slide-circular',
    'lg-slide-circular-vertical',
    'lg-slide-vertical',
    'lg-slide-vertical-growth',
    'lg-slide-skew-only',
    'lg-slide-skew-only-rev',
    'lg-slide-skew-only-y',
    'lg-slide-skew-only-y-rev',
    'lg-slide-skew',
    'lg-slide-skew-rev',
    'lg-slide-skew-cross',
    'lg-slide-skew-cross-rev',
    'lg-slide-skew-ver',
    'lg-slide-skew-ver-rev',
    'lg-slide-skew-ver-cross',
    'lg-slide-skew-ver-cross-rev',
    'lg-lollipop',
    'lg-lollipop-rev',
    'lg-rotate',
    'lg-rotate-rev',
    'lg-tube',
  ));
}

Functions

Namesort descending Description
lightgallery_add This function loads the required JavaScripts and css for a lightgallery instance.
lightgallery_help Implements hook_help().
lightgallery_libraries_info Implements hook_libraries_info().
lightgallery_theme Implements hook_theme().
lightgallery_views_api Implements hook_views_api().
template_preprocess_lightgallery_view Display a view as a Lightgallery style.
_lightgallery_build_option_set Builds compatible JS options set from views options form.
_lightgallery_get_modes Fetches available lightgallery modes.