You are here

photoswipe.module in PhotoSwipe 7

Photswipe integration with Drupal module.

File

photoswipe.module
View source
<?php

/**
 * @file
 * Photswipe integration with Drupal module.
 */

/**
 * Implements hook_theme().
 */
function photoswipe_theme() {
  return array(
    'photoswipe_imagefield' => array(
      'variables' => array(
        'image' => array(),
        'path' => NULL,
        'title' => NULL,
        'gid' => NULL,
      ),
      'file' => 'photoswipe.theme.inc',
    ),
    'photoswipe_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'node' => NULL,
        'field' => array(),
        'display_settings' => array(),
      ),
      'file' => 'photoswipe.theme.inc',
    ),
  );
}

/**
 * Implements hook_help().
 */
function photoswipe_help($path, $arg) {
  switch ($path) {

    // Main module help for the photoswipe module
    case 'admin/help#photoswipe':
      return '<p>' . t('PhotoSwipe provides a nice javascript-based display for photo galleries, very sleek on mobile browsers.', array(
        '!website' => l(t('PhotoSwipe website'), 'http://www.photoswipe.com/'),
      )) . '</p>';
  }
}

/**
 * Implements hook_init().
 */
function photoswipe_init() {
  $path = libraries_get_path('photoswipe');
  $version = photoswipe_get_version($path);
  drupal_add_js($path . '/lib/klass.min.js');
  drupal_add_js($path . '/code.photoswipe.jquery-' . $version . '.js');
  drupal_add_js(drupal_get_path('module', 'photoswipe') . '/js/photoswipe.jquery.js');
  drupal_add_css($path . '/photoswipe.css');
  $settings = variable_get('photoswipe_settings', photoswipe_get_default_settings());
  drupal_add_js(array(
    'photoswipe' => array(
      'options' => $settings,
    ),
  ), array(
    'type' => 'setting',
    'scope' => JS_DEFAULT,
  ));
}

/**
 * Implements hook_menu().
 */
function photoswipe_menu() {
  $items = array();
  $items['admin/config/media/photoswipe'] = array(
    'title' => 'PhotoSwipe',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'photoswipe_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'description' => 'Configure settings for PhotoSwipe javascript library.',
  );
  return $items;
}

/**
 * Configure global settings for PhotoSwipe.
 */
function photoswipe_admin_settings($form, &$form_state) {
  $form['photoswipe']['photoswipe_jquery_mobile'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force jQuery Mobile integration'),
    '#default_value' => variable_get('photoswipe_jquery_mobile', FALSE),
    '#description' => t('By default, PhotoSwipe will try and work this out for you. Enable this if PhotoSwipe fails to do so.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_field_formatter_info().
 */
function photoswipe_field_formatter_info() {
  return array(
    'photoswipe' => array(
      'label' => t('Photoswipe'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'photoswipe_node_style' => '',
        'photoswipe_image_style' => '',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function photoswipe_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $image_styles = image_style_options(FALSE);
  $image_styles_hide = $image_styles;
  $image_styles_hide['hide'] = t('Hide (do not display image)');
  $element['photoswipe_node_style'] = array(
    '#title' => t('Node image style'),
    '#type' => 'select',
    '#default_value' => $settings['photoswipe_node_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles_hide,
    '#description' => t('Image style to use in the node.'),
  );
  $element['photoswipe_image_style'] = array(
    '#title' => t('Photoswipe image style'),
    '#type' => 'select',
    '#default_value' => $settings['photoswipe_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
    '#description' => t('Image style to use in the Photoswipe.'),
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function photoswipe_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = array();
  $image_styles = image_style_options(FALSE);

  // Unset possible 'No defined styles' option.
  unset($image_styles['']);

  // Styles could be lost because of enabled/disabled modules that defines
  // their styles in code.
  if (isset($image_styles[$settings['photoswipe_node_style']])) {
    $summary[] = t('Node image style: @style', array(
      '@style' => $image_styles[$settings['photoswipe_node_style']],
    ));
  }
  else {
    if ($settings['photoswipe_node_style'] == 'hide') {
      $summary[] = t('Node image style: Hide');
    }
    else {
      $summary[] = t('Node image style: Original image');
    }
  }
  if (isset($image_styles[$settings['photoswipe_image_style']])) {
    $summary[] = t('Photoswipe image style: @style', array(
      '@style' => $image_styles[$settings['photoswipe_image_style']],
    ));
  }
  else {
    $summary[] = t('photoswipe image style: Original image');
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function photoswipe_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if (!empty($items)) {
    $element = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'photoswipe-gallery',
        ),
      ),
    );
  }
  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'photoswipe_image_formatter',
      '#item' => $item,
      '#node' => $entity,
      '#field' => $field,
      '#display_settings' => $display['settings'],
    );
  }
  return $element;
}

/**
 * Get photoswipe version.
 */
function photoswipe_get_version($path = NULL) {
  $version = drupal_static(__FUNCTION__);
  if (!empty($version)) {
    return $version;
  }
  if (!$path) {
    $path = libraries_get_path('photoswipe');
  }
  foreach (glob($path . "/code.photoswipe.jquery-*.min.js") as $filename) {
    $version = str_replace(array(
      $path,
      '/code.photoswipe.jquery-',
      '.min.js',
    ), '', $filename);
  }
  return $version;
}

/**
 * Get photoswipe default settings.
 */
function photoswipe_get_default_settings() {
  return array(
    'allowUserZoom' => FALSE,
    'autoStartSlideshow' => FALSE,
    'allowRotationOnUserZoom' => FALSE,
    'backButtonHideEnabled' => TRUE,
    'captionAndToolbarAutoHideDelay' => 5000,
    'captionAndToolbarFlipPosition' => FALSE,
    'captionAndToolbarHide' => FALSE,
    'captionAndToolbarOpacity' => 0.8,
    'captionAndToolbarShowEmptyCaptions' => TRUE,
    'cacheMode' => 'normal',
    'doubleTapSpeed' => 300,
    'doubleTapZoomLevel' => '2.5',
    'enableDrag' => TRUE,
    'enableKeyboard' => TRUE,
    'enableMouseWheel' => TRUE,
    'fadeInSpeed' => 250,
    'fadeOutSpeed' => 250,
    'imageScaleMethod' => 'fit',
    'invertMouseWheel' => FALSE,
    'jQueryMobile' => variable_get('photoswipe_jquery_mobile'),
    'jQueryMobileDialogHash' => '&ui-state=dialog',
    'loop' => TRUE,
    'margin' => 20,
    'maxUserZoom' => '5.0',
    'minUserZoom' => '0.5',
    'mouseWheelSpeed' => 500,
    'nextPreviousSlideSpeed' => 0,
    'preventHide' => FALSE,
    'preventSlideshow' => FALSE,
    'slideshowDelay' => 3000,
    'slideSpeed' => 250,
    'swipeThreshold' => 50,
    'swipeTimeThreshold' => 250,
    'slideTimingFunction' => 'ease-out',
    'zIndex' => 1000,
  );
}