You are here

photoswipe.module in PhotoSwipe 7.2

Photswipe integration with Drupal module.

File

photoswipe.module
View source
<?php

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

/**
 * The minimum PhotoSwipe version we support.
 */
define('PHOTOSWIPE_MIN_PLUGIN_VERSION', '4.0.0');

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

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

/**
 * 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_libraries_info().
 */
function photoswipe_libraries_info() {
  $libraries['photoswipe'] = array(
    'name' => 'Photoswipe plugin',
    'vendor url' => 'http://photoswipe.com',
    'download url' => 'https://github.com/dimsemenov/PhotoSwipe/archive/v4.0.5.zip',
    'path' => 'dist',
    'version arguments' => array(
      'file' => 'dist/photoswipe.min.js',
      'pattern' => '@(?i:PhotoSwipe)\\s-\\sv?([0-9\\.a-z]+)@',
      'lines' => 5,
    ),
    'files' => array(
      'js' => array(
        'photoswipe.min.js',
        'photoswipe-ui-default.min.js',
      ),
      'css' => array(
        'photoswipe.css',
        'default-skin/default-skin.css',
      ),
    ),
    'variants' => array(
      'minified' => array(
        'files' => array(
          'js' => array(
            'photoswipe.min.js',
            'photoswipe-ui-default.min.js',
          ),
          'css' => array(
            'photoswipe.css',
          ),
        ),
      ),
      'source' => array(
        'files' => array(
          'js' => array(
            'photoswipe.js',
            'photoswipe-ui-default.js',
          ),
          'css' => array(
            'photoswipe.css',
            'default-skin/default-skin.css',
          ),
        ),
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_page_build().
 */
function photoswipe_page_build(&$page) {
  if (variable_get('photoswipe_always_load_non_admin', FALSE) && !path_is_admin(current_path())) {
    photoswipe_load_assets();
  }
  if (photoswipe_assets_loaded()) {
    $page['page_bottom']['photoswipe'] = array(
      '#theme' => 'photoswipe_container',
    );
  }
}

/**
 * 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_first' => '',
        'photoswipe_node_style' => '',
        'photoswipe_image_style' => '',
        'photoswipe_caption' => '',
        'photoswipe_view_mode' => '',
      ),
    ),
  );
}

/**
 * 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_first'] = array(
    '#title' => t('Node image style for first image'),
    '#type' => 'select',
    '#default_value' => $settings['photoswipe_node_style_first'],
    '#empty_option' => t('No special style.'),
    '#options' => $image_styles_hide,
    '#description' => t('Image style to use in the content for the first 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.'),
  );

  // Set our caption options
  $caption_options = array(
    'alt' => t('Image Alt Tag'),
    'title' => t('Image Title Tag'),
    'node_title' => t('Node Title'),
  );

  // Add the other node fields as options
  foreach ($form['#fields'] as $node_field) {
    if ($node_field != $instance['field_name']) {
      $caption_options[$node_field] = $node_field;
    }
  }
  $element['photoswipe_caption'] = array(
    '#title' => t('Photoswipe image caption'),
    '#type' => 'select',
    '#default_value' => $settings['photoswipe_caption'] ? $settings['photoswipe_caption'] : 'alt',
    '#options' => $caption_options,
    '#description' => t('Field that should be used for the caption.'),
  );

  // Add the current view mode so we can control the view mode for node fields.
  $element['photoswipe_view_mode'] = array(
    '#type' => 'hidden',
    '#value' => $view_mode,
  );
  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_node_style_first']])) {
    $summary[] = t('Node image style of first image: @style', array(
      '@style' => $image_styles[$settings['photoswipe_node_style_first']],
    ));
  }
  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');
  }
  if (isset($settings['photoswipe_caption'])) {
    $caption_options = array(
      'alt' => t('Image Alt Tag'),
      'title' => t('Image Title Tag'),
      'node_title' => t('Node Title'),
    );
    if (array_key_exists($settings['photoswipe_caption'], $caption_options)) {
      $caption_setting = $caption_options[$settings['photoswipe_caption']];
    }
    else {
      $caption_setting = $settings['photoswipe_caption'];
    }
    $summary[] = t('Photoswipe Caption: @field', array(
      '@field' => $caption_setting,
    ));
  }
  else {
    $summary[] = t('Photoswipe Caption: Image Alt Tag');
  }
  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)) {
    photoswipe_load_assets();
  }
  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'photoswipe_image_formatter',
      '#item' => $item,
      '#node' => $entity,
      '#field' => $field,
      '#display_settings' => $display['settings'],
      '#delta' => $delta,
    );
  }
  return $element;
}

/**
 * Loads photoswipe assets.
 */
function photoswipe_load_assets() {
  if (!photoswipe_assets_loaded()) {
    libraries_load('photoswipe');
    drupal_add_js(drupal_get_path('module', 'photoswipe') . '/js/photoswipe.jquery.js', array(
      'scope' => 'footer',
    ));
    $settings = variable_get('photoswipe_settings', photoswipe_get_default_settings());
    drupal_add_js(array(
      'photoswipe' => array(
        'options' => $settings,
      ),
    ), array(
      'type' => 'setting',
      'scope' => JS_DEFAULT,
    ));
    photoswipe_assets_loaded(TRUE);
  }
}

/**
 * Stores whether photoswipe assets has been loaded in the same request.
 *
 * @param bool $loaded
 *   (Optional) Set whether assets are loaded or not.
 *
 * @return bool
 */
function photoswipe_assets_loaded($loaded = NULL) {
  $status =& drupal_static(__FUNCTION__);
  if ($loaded) {
    $status = $loaded;
  }
  return $status;
}

/**
 * Get photoswipe default settings.
 *
 * @see http://photoswipe.com/documentation/options.html
 */
function photoswipe_get_default_settings() {
  $settings = array(
    //'index' => 0,

    //'getThumbBoundsFn' => NULL,
    'showAnimationDuration' => 333,
    'hideAnimationDuration' => 333,
    'showHideOpacity' => FALSE,
    'bgOpacity' => 1,
    'spacing' => 0.12,
    'allowPanToNext' => TRUE,
    'maxSpreadZoom' => 2,
    //'getDoubleTapZoom' => NULL,
    'loop' => TRUE,
    'pinchToClose' => TRUE,
    'closeOnScroll' => TRUE,
    'closeOnVerticalDrag' => TRUE,
    'mouseUsed' => FALSE,
    'escKey' => TRUE,
    'arrowKeys' => TRUE,
    'history' => TRUE,
    //'galleryUID' => 1,
    'errorMsg' => '<div class="pswp__error-msg">' . t('<a href="%url%" target="_blank">The image</a> could not be loaded.') . '</div>',
    'preload' => array(
      1,
      1,
    ),
    'mainClass' => NULL,
    //'getNumItemsFn' => NULL,
    'focus' => TRUE,
  );

  // Add ability to override default settings using hook_photoswipe_default_settings_alter().
  drupal_alter('photoswipe_default_settings', $settings);
  return $settings;
}

Functions

Constants

Namesort descending Description
PHOTOSWIPE_MIN_PLUGIN_VERSION The minimum PhotoSwipe version we support.