View source
<?php
define('PHOTOSWIPE_MIN_PLUGIN_VERSION', '4.0.0');
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;
}
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',
),
);
}
function photoswipe_help($path, $arg) {
switch ($path) {
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>';
}
}
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;
}
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',
);
}
}
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' => '',
),
),
);
}
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.'),
);
$caption_options = array(
'alt' => t('Image Alt Tag'),
'title' => t('Image Title Tag'),
'node_title' => t('Node Title'),
);
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.'),
);
$element['photoswipe_view_mode'] = array(
'#type' => 'hidden',
'#value' => $view_mode,
);
return $element;
}
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($image_styles['']);
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);
}
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;
}
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);
}
}
function photoswipe_assets_loaded($loaded = NULL) {
$status =& drupal_static(__FUNCTION__);
if ($loaded) {
$status = $loaded;
}
return $status;
}
function photoswipe_get_default_settings() {
$settings = array(
'showAnimationDuration' => 333,
'hideAnimationDuration' => 333,
'showHideOpacity' => FALSE,
'bgOpacity' => 1,
'spacing' => 0.12,
'allowPanToNext' => TRUE,
'maxSpreadZoom' => 2,
'loop' => TRUE,
'pinchToClose' => TRUE,
'closeOnScroll' => TRUE,
'closeOnVerticalDrag' => TRUE,
'mouseUsed' => FALSE,
'escKey' => TRUE,
'arrowKeys' => TRUE,
'history' => TRUE,
'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,
'focus' => TRUE,
);
drupal_alter('photoswipe_default_settings', $settings);
return $settings;
}