View source
<?php
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'galleryformatter') . '/includes/galleryformatter_imagestyles.inc';
function galleryformatter_field_formatter_info() {
return array(
'galleryformatter_default' => array(
'label' => t('jQuery Gallery'),
'field types' => array(
'image',
'media',
),
'settings' => array(
'slide_style' => 'galleryformatter_slide',
'thumb_style' => 'galleryformatter_thumb',
'style' => 'Greenarrows',
'linking_method' => 'show_full_link',
'link_to_full' => 1,
'link_to_full_style' => 0,
'modal' => 'none',
),
'description' => t('Display multi-value fields as an jQuery Image gallery.'),
),
);
}
function galleryformatter_field_formatter_settings_form($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$form = array();
$options = array(
t('None'),
);
foreach (image_styles() as $id => $style) {
$options[$id] = $id;
}
$form['slide_style'] = array(
'#type' => 'select',
'#title' => t('Select the slide style'),
'#options' => $options,
'#default_value' => $settings['slide_style'],
'#description' => t('Select the imagecache style you would like to show when clicked on the thumbnail.'),
);
$form['thumb_style'] = array(
'#type' => 'select',
'#title' => t('Select the thumbnail style'),
'#options' => $options,
'#default_value' => $settings['thumb_style'],
'#description' => t('Select the imagecache style you would like to show for the thumbnails list.'),
);
$style_options = array();
$styles = module_invoke_all('galleryformatter_styles');
foreach ($styles as $style) {
$style_options[$style] = $style;
}
ksort($style_options);
$form['style'] = array(
'#type' => 'select',
'#title' => t('Style'),
'#options' => array(
'nostyle' => t('No style'),
) + $style_options,
'#default_value' => $settings['style'],
'#description' => t('Choose the gallery style.'),
);
$form['link_to_full'] = array(
'#type' => 'checkbox',
'#title' => t('Link slides to the full image'),
'#default_value' => $settings['link_to_full'],
'#description' => t('If you check this on, the slides will be linked to the style you choose in <em>Select the full image style</em> below.'),
);
$options[0] = t('None (original image)');
$form['link_to_full_style'] = array(
'#type' => 'select',
'#title' => t('Select the full image style'),
'#options' => $options,
'#default_value' => $settings['link_to_full_style'],
'#description' => t('Select the imagecache style you would like to show when clicked on the thumbnail.<br />If you select none, the the link will point to the original image.'),
'#states' => array(
'visible' => array(
':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][link_to_full]"]' => array(
'checked' => TRUE,
),
),
),
);
$modal_options = array();
if (module_exists('colorbox')) {
$modal_options['colorbox'] = 'colorbox';
}
if (module_exists('shadowbox')) {
$modal_options['shadowbox'] = 'shadowbox';
}
if (module_exists('lightbox2')) {
$modal_options['lightbox2'] = 'lightbox2';
}
if (module_exists('fancybox')) {
$modal_options['fancybox'] = 'fancybox';
}
$modal_options['none'] = t('Do not use modal');
$form['modal'] = array(
'#type' => 'select',
'#title' => t('Use jQuery modal for full image link'),
'#options' => $modal_options,
'#default_value' => $settings['modal'],
'#description' => t("Select which jQuery modal module you'd like to display the full link image in, if any."),
'#states' => array(
'visible' => array(
':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][link_to_full]"]' => array(
'checked' => TRUE,
),
),
),
);
$form['linking_method'] = array(
'#type' => 'select',
'#title' => t('How to link to the full image'),
'#options' => array(
'show_full_link' => t('Using a link'),
'onclick_full' => t('On slide click'),
),
'#default_value' => $settings['linking_method'],
'#description' => t('On slide click means that when you click on the slide it will open the full image.<br /> Using a link means a magnify glass in default style.'),
'#states' => array(
'visible' => array(
':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][link_to_full]"]' => array(
'checked' => TRUE,
),
),
),
);
return $form;
}
function galleryformatter_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary[] = t('Slides style: @value', array(
'@value' => $settings['slide_style'],
));
$summary[] = t('Thumbnails style: @value', array(
'@value' => $settings['thumb_style'],
));
$summary[] = t('Gallery style: @value', array(
'@value' => $settings['style'],
));
$summary[] = $settings['link_to_full'] ? t('Linking to full image') : t('Not linking to full image');
if ($settings['link_to_full']) {
$full_image_style = $settings['link_to_full_style'] ? $settings['link_to_full_style'] : t('None (original image)');
$summary[] = t('Full image style: @value', array(
'@value' => $full_image_style,
));
$summary[] = t('Modal used for full image: @value', array(
'@value' => $settings['modal'],
));
$linking_method = $settings['linking_method'] == 'onclick_full' ? t('On slide click') : t('Using a link');
$summary[] = t('Linking method: @value', array(
'@value' => $linking_method,
));
}
return implode('<br />', $summary);
}
function galleryformatter_theme() {
return array(
'galleryformatter' => array(
'variables' => array(
'slides' => NULL,
'thumbs' => NULL,
'settings' => NULL,
'dimensions' => NULL,
),
'template' => 'theme/galleryformatter',
),
);
}
function galleryformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if (empty($items)) {
return $element;
}
$modulepath = drupal_get_path('module', 'galleryformatter');
$settings = $display['settings'];
$settings['style'] = strtolower($settings['style']);
if ($field['type'] == 'media') {
foreach ($items as $delta => $item) {
if ($item['file']->type == 'image') {
$items[$delta] = (array) $item;
$items[$delta]['uri'] = $item['file']->uri;
$items[$delta]['filename'] = $item['file']->filename;
}
}
$changed = FALSE;
foreach ($items as $delta => $item) {
if ($item['file']->type !== 'image') {
unset($items[$delta]);
$changed = TRUE;
}
}
if ($changed) {
$items = array_values($items);
}
}
$changed = FALSE;
foreach ($items as $delta => $item) {
$dimensions['slides'] = galleryformatter_getimage_dimensions($settings['slide_style'], $items[$delta]['uri']);
if (!$dimensions['slides']) {
unset($items[$delta]);
$changed = TRUE;
}
}
if ($changed) {
$items = array_values($items);
}
if (empty($items)) {
return $element;
}
$dimensions['slides'] = galleryformatter_getimage_dimensions($settings['slide_style'], $items[0]['uri']);
$dimensions['thumbs'] = galleryformatter_getimage_dimensions($settings['thumb_style'], $items[0]['uri']);
$num_of_images = count($items);
$renderitems = array();
$renderitems['thumbs'] = array();
$ids = entity_extract_ids($entity_type, $entity);
$entity_id = $ids[0];
foreach ($items as $delta => $item) {
if (!empty($item['title'])) {
if (strpos($item['title'], '<') !== FALSE) {
$item['title'] = strip_tags($item['title']);
}
}
else {
$item['title'] = '';
}
$renderitems['slides'][$delta]['title'] = $item['title'];
$item['filename'] = $item['filename'] = '';
if (isset($item['alt']) && !empty($item['alt'])) {
if (strpos($item['alt'], '<') !== FALSE) {
$item['alt'] = strip_tags($item['alt']);
}
}
else {
$item['alt'] = $item['filename'];
}
$renderitems['slides'][$delta]['alt'] = $item['alt'];
if (empty($item['title'])) {
if (!empty($item['alt'])) {
$item['title'] = $item['alt'];
}
else {
if (!empty($entity->title)) {
if (strpos($entity->title, '<') !== FALSE) {
$item['title'] = strip_tags($entity->title);
}
else {
$item['title'] = $entity->title;
}
}
$item['title'] = $num_of_images > 1 ? $item['title'] . t(' image ') . ($delta + 1) : $item['title'];
}
}
$slideset_id = $field['field_name'] . '-' . $entity_id;
$renderitems['slides'][$delta]['hash_id'] = 'slide-' . $delta . '-' . $slideset_id;
$renderitems['slides'][$delta]['image'] = theme('image_formatter', array(
'item' => $item,
'image_style' => $settings['slide_style'],
));
if ($settings['link_to_full'] == TRUE) {
$settings['linking_method'] = isset($settings['linking_method']) ? $settings['linking_method'] : '';
$link_attributes = array(
'title' => $item['title'],
'class' => '',
);
if ($settings['linking_method'] !== 'onclick_full') {
$link_attributes['class'] = 'shownext';
}
switch ($settings['modal']) {
case 'none':
break;
case 'colorbox':
$link_attributes['class'] .= $settings['linking_method'] == 'onclick_full' ? 'colorbox' : ' colorbox';
$link_attributes['rel'] = 'gallery-[' . $slideset_id . ']';
break;
case 'shadowbox':
$link_attributes['rel'] = 'shadowbox[' . $slideset_id . ']';
$element['#attached']['library'][] = array(
'shadowbox',
'shadowbox',
);
break;
case 'lightbox2':
$link_attributes['rel'] = 'lightbox[' . $slideset_id . ']';
break;
case 'fancybox':
$link_attributes['class'] = 'fancybox';
$link_attributes['rel'] = 'fancybox[' . $slideset_id . ']';
break;
}
$link_url = $settings['link_to_full_style'] ? image_style_url($settings['link_to_full_style'], $item['uri']) : file_create_url($item['uri']);
$link_content = $settings['linking_method'] == 'show_full_link' ? '<span class="view-full" title="' . t('View the full image') . '">' . t('View the full image') . '</span>' : '';
$image_rendered = $renderitems['slides'][$delta]['image'];
$link_content .= $settings['linking_method'] == 'onclick_full' ? $image_rendered : '';
$renderitems['slides'][$delta]['image'] = l($link_content, $link_url, array(
'attributes' => $link_attributes,
'html' => TRUE,
));
$renderitems['slides'][$delta]['image'] .= $settings['linking_method'] == 'onclick_full' ? '' : $image_rendered;
$renderitems['slides'][$delta]['full_image_url'] = $link_url;
}
if ($num_of_images > 1) {
$renderitems['thumbs'][$delta]['image'] = theme('image_formatter', array(
'item' => $item,
'image_style' => $settings['thumb_style'],
));
$renderitems['thumbs'][$delta]['hash_id'] = 'slide-' . $delta . '-' . $slideset_id;
}
}
if ($num_of_images > 1) {
drupal_add_js($modulepath . '/theme/infiniteCarousel.js');
drupal_add_js($modulepath . '/theme/galleryformatter.js');
}
galleryformatter_add_css($settings['style']);
drupal_add_css($modulepath . '/theme/galleryformatter.css');
$element['#theme'] = 'galleryformatter';
$element['#slides'] = $renderitems['slides'];
$element['#thumbs'] = $renderitems['thumbs'];
$element['#settings'] = $settings;
$element['#dimensions'] = $dimensions;
return array(
$element,
);
}
function galleryformatter_add_css($style) {
drupal_add_css(drupal_get_path('module', 'galleryformatter') . '/theme/galleryformatter.css');
if ($style != 'nostyle') {
$style_css = _galleryformatter_get_style_css($style);
drupal_add_css($style_css);
}
}
function _galleryformatter_get_style_css($style = 'nostyle') {
static $gallerystyles;
if ($style != 'nostyle') {
if (!isset($gallerystyles)) {
$gallerystyles = module_invoke_all('galleryformatter_styles');
}
foreach ($gallerystyles as $css_file => $gallerystyle) {
if ($style == strtolower($gallerystyle)) {
return $css_file;
}
}
}
return 'nostyle';
}
function galleryformatter_galleryformatter_styles() {
$gallerystyles_directory = drupal_get_path('module', 'galleryformatter') . '/gallerystyles';
$files = file_scan_directory($gallerystyles_directory, '/\\.css$/');
$gallerystyles = array();
foreach ($files as $file) {
if (!strpos($file->name, '-rtl')) {
$gallerystyles[$file->uri] = drupal_ucfirst($file->name);
}
}
return $gallerystyles;
}
function galleryformatter_getimage_dimensions($stylename, $image_path) {
$ret = NULL;
$transformed_path = image_style_path($stylename, $image_path);
$style = image_style_load($stylename);
$default_style = $style['module'] == 'galleryformatter';
if ($style['storage'] == 4 && $default_style) {
$ret['height'] = $style['effects'][0]['data']['height'];
$ret['width'] = $style['effects'][0]['data']['width'];
}
if (file_exists($transformed_path) || image_style_create_derivative($style, $image_path, $transformed_path)) {
$image = image_get_info($transformed_path);
$ret['height'] = $image['height'];
$ret['width'] = $image['width'];
}
return $ret;
}