You are here

fancybox.module in fancyBox 7

Same filename and directory in other branches
  1. 5 fancybox.module
  2. 6 fancybox.module
  3. 7.2 fancybox.module

Provides the Fancybox jQuery plugin for displaying images, HTML content and multimedia in an elegant box.

File

fancybox.module
View source
<?php

/**
 * @file
 * Provides the Fancybox jQuery plugin for displaying images, HTML content and
 * multimedia in an elegant box.
 */

/**
 * Implementation of hook_init().
 */
function fancybox_init() {
  $settings = variable_get('fancybox_settings', array());
  if (!empty($settings) && fancybox_active($settings)) {
    fancybox_activate($settings);
  }
}

/**
 * Implementation of hook_permission().
 */
function fancybox_permission() {
  return array(
    'administer fancybox' => array(
      'title' => t('Administer Fancybox'),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function fancybox_menu() {
  $items = array();
  $base = array(
    'access arguments' => array(
      'administer fancybox',
    ),
    'file' => 'fancybox.admin.inc',
  );
  $items['admin/config/user-interface/fancybox'] = $base + array(
    'title' => 'Fancybox',
    'description' => 'Configure the settings for Fancybox.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fancybox_admin_settings_form',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Check whether Fancybox should be initialized for the current URL.
 */
function fancybox_active($settings) {

  // If PHP is selected, evaluate it.
  if ($settings['activation']['activation_type'] == 'php') {
    if (module_exists('php')) {
      return php_eval($settings['activation']['activation_pages']);
    }
    else {
      return FALSE;
    }
  }

  // Compare defined paths with the current path.
  $path = drupal_get_path_alias($_GET['q']);
  $page_match = drupal_match_path($path, $settings['activation']['activation_pages']);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $settings['activation']['activation_pages']);
  }

  // Decide whether to include or exclude pages.
  if ($settings['activation']['activation_type'] == 'exclude') {
    return !$page_match;
  }
  else {
    return $page_match;
  }
}

/**
 * Include the Fancybox JS.
 */
function fancybox_activate($settings = NULL) {
  static $active = FALSE;
  if (!$active) {
    if ($settings == NULL) {
      $settings = variable_get('fancybox_settings', array());
    }

    // Add Fancybox files.
    $path = libraries_get_path('fancybox') . '/' . 'fancybox' . '/';
    $files = variable_get('fancybox_files', array());

    // TODO: add easing

    //drupal_add_js($path .'jquery.easing-1.3.pack.js');
    drupal_add_css($path . $files['css']);
    drupal_add_js($path . $files['js']);

    // Check for empty settings
    if (empty($settings['options'])) {
      $settings['options'] = array();
    }
    if (empty($settings['activation']['selector'])) {
      $settings['activation'] = array();
      $settings['activation']['selector'] = '';
    }

    // prep settings for exposure (by removing empty options)
    $settings['options'] = array_filter($settings['options'], 'strlen');

    // Expose settings to the DOM.
    $exposed_settings['options'] = $settings['options'];
    $exposed_settings['selector'] = $settings['activation']['selector'];
    drupal_add_js(array(
      'fancybox' => $exposed_settings,
    ), 'setting');

    // Initialize Fancybox.
    drupal_add_js(drupal_get_path('module', 'fancybox') . '/js/fancybox.js');
    $active = TRUE;
  }
}

/**
 * Define form elements for the Fancybox plugin's options.
 */
function fancybox_options($values = array()) {

  /*
    Fancybox 1.3.1 options:

    TODO: autoDimensions 	true 	For inline and ajax views, resizes the view to the element recieves. Make sure it has dimensions otherwise this will give unexpected results
    TODO: modal 	false 	When true, 'overlayShow' is set to 'true' and 'hideOnOverlayClick', 'hideOnContentClick', 'enableEscapeButton', 'showCloseButton' are set to 'false'
    TODO: ajax 	{ } 	Ajax options (error, success will be overwritten)
    TODO: swf 	{wmode: 'transparent'} 	Flashvars to put on the swf object
    TODO: titleFormat 	null 	Callback to customize title area. You can set any html - custom image counter or even custom navigation
  */
  $easing_options = array(
    'none' => t('None'),
    'swing' => t('Swing'),
    'easeInQuad' => t('Quad in'),
    'easeOutQuad' => t('Quad out'),
    'easeInOutQuad' => t('Quad in/out'),
    'easeInCubic' => t('Cubic in'),
    'easeOutCubic' => t('Cubic out'),
    'easeInOutCubic' => t('Cubic in/out'),
    'easeInQuart' => t('Quart in'),
    'easeOutQuart' => t('Quart out'),
    'easeInOutQuart' => t('Quart in/out'),
    'easeInQuint' => t('Quint in'),
    'easeOutQuint' => t('Quint out'),
    'easeInOutQuint' => t('Quint in/out'),
    'easeInSine' => t('Sine in'),
    'easeOutSine' => t('Sine out'),
    'easeInOutSine' => t('Sine in/out'),
    'easeInExpo' => t('Expo in'),
    'easeOutExpo' => t('Expo out'),
    'easeInOutExpo' => t('Expo in/out'),
    'easeInCirc' => t('Circular in'),
    'easeOutCirc' => t('Circular out'),
    'easeInOutCirc' => t('Circular in/out'),
    'easeInElastic' => t('Elastic in'),
    'easeOutElastic' => t('Elastic out'),
    'easeInOutElastic' => t('Elastic in/out'),
    'easeInBack' => t('Back in'),
    'easeOutBack' => t('Back out'),
    'easeInOutBack' => t('Back in/out'),
    'easeInBounce' => t('Bounce in'),
    'easeOutBounce' => t('Bounce out'),
    'easeInOutBounce' => t('Bounce in/out'),
  );
  for ($i = 0; $i <= 10; $i++) {
    $float = (double) $i / 10;
    $opacity_options[(string) $float] = $i * 10 . '%';
  }
  $options = array();
  $options['appearance'] = array(
    '#type' => 'fieldset',
    '#title' => t('Appearance'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['appearance']['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#description' => t("Width for content types 'iframe' and 'swf'. Also set for inline content if 'autoDimensions' is set to 'false'"),
    '#default_value' => isset($values['width']) ? $values['width'] : 425,
    '#size' => 5,
  );
  $options['appearance']['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#description' => t("Height for content types 'iframe' and 'swf'. Also set for inline content if 'autoDimensions' is set to 'false'"),
    '#default_value' => isset($values['height']) ? $values['height'] : 355,
    '#size' => 5,
  );
  $options['appearance']['padding'] = array(
    '#type' => 'textfield',
    '#title' => t('Padding'),
    '#description' => t('Space between FancyBox wrapper and content'),
    '#default_value' => isset($values['padding']) ? $values['padding'] : 10,
    '#size' => 5,
  );
  $options['appearance']['margin'] = array(
    '#type' => 'textfield',
    '#title' => t('Margin'),
    '#description' => t('Space between viewport and FancyBox wrapper'),
    '#default_value' => isset($values['margin']) ? $values['margin'] : 20,
    '#size' => 5,
  );
  $options['appearance']['titleShow'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show title'),
    '#description' => t('If checked, shows the title.'),
    '#default_value' => isset($values['titleShow']) ? $values['titleShow'] : TRUE,
  );
  $options['appearance']['titlePosition'] = array(
    '#type' => 'select',
    '#title' => t('Title position'),
    '#description' => t('The position of title.'),
    '#options' => array(
      'outside' => t('Outside'),
      'inside' => t('Inside'),
      'over' => t('Over'),
    ),
    '#default_value' => isset($values['titlePosition']) ? $values['titlePosition'] : 'outside',
  );
  $options['appearance']['scrolling'] = array(
    '#type' => 'select',
    '#title' => t('Content scrolling'),
    '#description' => t('Set the overflow CSS property to create or hide scrollbars'),
    '#options' => array(
      'auto' => t('Auto'),
      'yes' => t('Yes'),
      'no' => t('No'),
    ),
    '#default_value' => isset($values['scrolling']) ? $values['scrolling'] : 'auto',
  );
  $options['appearance']['showCloseButton'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show close button'),
    '#description' => t('Toggle close button'),
    '#default_value' => isset($values['showCloseButton']) ? $values['showCloseButton'] : TRUE,
  );
  $options['appearance']['showNavArrows'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show navigation arrows'),
    '#description' => t('Toggle navigation arrows'),
    '#default_value' => isset($values['showNavArrows']) ? $values['showNavArrows'] : TRUE,
  );
  $options['appearance']['cyclic'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cyclic'),
    '#description' => t('When checked, galleries will be cyclic, allowing you to keep pressing next/back'),
    '#default_value' => isset($values['cyclic']) ? $values['cyclic'] : FALSE,
  );
  $options['appearance']['opacity'] = array(
    '#type' => 'checkbox',
    '#title' => t('Opacity'),
    '#description' => t('If checked,  transparency of content is changed for elastic transitions'),
    '#default_value' => isset($values['opacity']) ? $values['opacity'] : FALSE,
  );
  $options['appearance']['overlayShow'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show overlay'),
    '#description' => t('If checked, shows the overlay behind the item'),
    '#default_value' => isset($values['overlayShow']) ? $values['overlayShow'] : TRUE,
  );
  $options['appearance']['overlayColor'] = array(
    '#type' => 'textfield',
    '#title' => t('Overlay color'),
    '#description' => t('Color of the overlay (i.e. #666666)'),
    '#default_value' => isset($values['overlayColor']) ? $values['overlayColor'] : '#666',
    '#size' => 8,
  );
  $options['appearance']['overlayOpacity'] = array(
    '#type' => 'select',
    '#title' => t('Overlay opacity'),
    '#description' => t('Opacity of overlay (from 0% to 100%)'),
    '#options' => $opacity_options,
    '#default_value' => isset($values['overlayOpacity']) ? $values['overlayOpacity'] : 0.3,
  );
  $options['appearance']['hideOnOverlayClick'] = array(
    '#type' => 'checkbox',
    '#title' => t('Close on overlay click'),
    '#description' => t('If checked, clicking the overlay closes FancyBox'),
    '#default_value' => isset($values['hideOnOverlayClick']) ? $values['hideOnOverlayClick'] : TRUE,
  );
  $options['appearance']['hideOnContentClick'] = array(
    '#type' => 'checkbox',
    '#title' => t('Close on content click'),
    '#description' => t('If checked, clicking the content closes FancyBox'),
    '#default_value' => isset($values['hideOnContentClick']) ? $values['hideOnContentClick'] : FALSE,
  );
  $options['appearance']['enableEscapeButton'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Escape button'),
    '#description' => t('If checked, pressing Esc button closes FancyBox'),
    '#default_value' => isset($values['enableEscapeButton']) ? $values['enableEscapeButton'] : TRUE,
  );
  $options['appearance']['centerOnScroll'] = array(
    '#type' => 'checkbox',
    '#title' => t('Keep centered when scrolling'),
    '#description' => t('If checked, item stays centered when user scrolls the page'),
    '#default_value' => isset($values['centerOnScroll']) ? $values['centerOnScroll'] : TRUE,
  );
  $options['appearance']['autoScale'] = array(
    '#type' => 'checkbox',
    '#title' => t('Scale images'),
    '#description' => t('If checked, FancyBox is scaled to fit in viewport'),
    '#default_value' => isset($values['autoScale']) ? $values['autoScale'] : TRUE,
  );
  $options['effects'] = array(
    '#type' => 'fieldset',
    '#title' => t('Effects'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['effects']['easingIn'] = array(
    '#type' => 'select',
    '#title' => t('Easing effect on open'),
    '#description' => t('Easing effect used when opening an item.'),
    '#options' => $easing_options,
    '#default_value' => isset($values['easingIn']) ? $values['easingIn'] : 'swing',
  );
  $options['effects']['easingOut'] = array(
    '#type' => 'select',
    '#title' => t('Easing effect on close'),
    '#description' => t('Easing effect used when closing an item.'),
    '#options' => $easing_options,
    '#default_value' => isset($values['easingOut']) ? $values['easingOut'] : 'swing',
  );
  $options['effects']['changeFade'] = array(
    '#type' => 'select',
    '#title' => t('Change fade'),
    '#description' => t('Speed of the content fading while changing gallery items'),
    '#options' => array(
      'fast' => t('Fast'),
    ),
    '#default_value' => isset($values['changeFade']) ? $values['changeFade'] : 'fast',
  );
  $options['effects']['changeSpeed'] = array(
    '#type' => 'textfield',
    '#title' => t('Change speed'),
    '#description' => t('Speed of resizing when changing gallery items, in milliseconds'),
    '#default_value' => isset($values['changeSpeed']) ? $values['changeSpeed'] : 300,
    '#size' => 8,
  );
  $options['effects']['transitionIn'] = array(
    '#type' => 'select',
    '#title' => t('Transition in'),
    '#description' => t('The transition type'),
    '#options' => array(
      'elastic' => t('Elastic'),
      'fade' => t('Fade'),
      'none' => t('None'),
    ),
    '#default_value' => isset($values['transitionIn']) ? $values['transitionIn'] : 'fade',
  );
  $options['effects']['speedIn'] = array(
    '#type' => 'textfield',
    '#title' => t('Transition in speed'),
    '#description' => t('Speed of the fade and elastic transitions, in milliseconds'),
    '#default_value' => isset($values['speedIn']) ? $values['speedIn'] : 300,
    '#size' => 8,
  );
  $options['effects']['transitionOut'] = array(
    '#type' => 'select',
    '#title' => t('Transition out'),
    '#description' => t('The transition type'),
    '#options' => array(
      'elastic' => t('Elastic'),
      'fade' => t('Fade'),
      'none' => t('None'),
    ),
    '#default_value' => isset($values['transitionOut']) ? $values['transitionOut'] : 'fade',
  );
  $options['effects']['speedOut'] = array(
    '#type' => 'textfield',
    '#title' => t('Transition out speed'),
    '#description' => t('Speed of the fade and elastic transitions, in milliseconds'),
    '#default_value' => isset($values['speedOut']) ? $values['speedOut'] : 300,
    '#size' => 8,
  );
  $options['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['advanced']['onStart'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on start'),
    '#description' => t('Enter the name of a JavaScript function to be called right before attempting to load the content.'),
    '#default_value' => isset($values['onStart']) ? $values['onStart'] : '',
  );
  $options['advanced']['onComplete'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on open'),
    '#description' => t('Enter the name of a JavaScript function to be called once the content is displayed.'),
    '#default_value' => isset($values['onComplete']) ? $values['onComplete'] : '',
  );
  $options['advanced']['onClosed'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on close'),
    '#description' => t('Enter the name of a JavaScript function to be called once FancyBox is closed.'),
    '#default_value' => isset($values['onClosed']) ? $values['onClosed'] : '',
  );
  $options['advanced']['onCleanup'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on cleanup'),
    '#description' => t('Enter the name of a JavaScript function to be called just before closing.'),
    '#default_value' => isset($values['onCleanup']) ? $values['onCleanup'] : '',
  );
  $options['advanced']['onCancel'] = array(
    '#type' => 'textfield',
    '#title' => t('Callback on cancel'),
    '#description' => t('Enter the name of a JavaScript function to be called after loading is canceled.'),
    '#default_value' => isset($values['onCancel']) ? $values['onCancel'] : '',
  );
  return $options;
}

/**
 * Implements hook_field_formatter_info().
 */
function fancybox_field_formatter_info() {
  $formatters = array(
    'fancybox_image' => array(
      'label' => t('Fancybox image'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'image_style' => '',
        'image_link_style' => '',
      ),
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function fancybox_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);
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['image_link_style'] = array(
    '#title' => t('Linked image style'),
    '#type' => 'select',
    '#default_value' => $settings['image_link_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function fancybox_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['image_style']])) {
    $summary[] = t('Image style: @style', array(
      '@style' => $image_styles[$settings['image_style']],
    ));
  }
  else {
    $summary[] = t('Original image');
  }
  if (isset($image_styles[$settings['image_link_style']])) {
    $summary[] = t('Linked image style: @style', array(
      '@style' => $image_styles[$settings['image_link_style']],
    ));
  }
  else {
    $summary[] = t('Linked image style: Original image');
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function fancybox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = variable_get('fancybox_settings', array());
  fancybox_activate($settings);

  // Get grouping settings
  $rel = '';
  if (!empty($settings['imagefield']['grouping'])) {
    switch ($settings['imagefield']['grouping']) {
      case 1:
        $rel = $field['field_name'];
        break;
      case 2:
        $rel = 'all-images';
        break;
      default:
        $rel = '';
    }
  }
  foreach ($items as $delta => $item) {
    $style_name = isset($display['settings']['image_link_style']) ? $display['settings']['image_link_style'] : 0;
    if ($style_name) {
      $style_path = image_style_path($style_name, $item['uri']);
      if (!file_exists($style_path)) {
        $style_path = image_style_url($style_name, $item['uri']);
      }
    }
    else {
      $style_path = file_create_url($item['uri']);
    }
    $href = file_create_url($style_path);

    // Image caption.
    if (!empty($settings['imagefield']['use_node_title'])) {
      $image_title = $entity->title;
    }
    else {
      $image_title = !empty($item['title']) ? $item['title'] : '';
      $image_title = !empty($image_title) ? $image_title : $item['alt'];
    }
    $element[$delta] = array(
      '#theme' => 'image_formatter',
      '#item' => $item,
      '#image_style' => $display['settings']['image_style'],
      '#path' => array(
        'path' => $href,
        'options' => array(
          'attributes' => array(
            'class' => array(
              'imagefield-fancybox',
            ),
            'rel' => $rel,
            'title' => $image_title,
          ),
        ),
      ),
    );
  }
  return $element;
}

/** 
 * Detect needed script and stylesheet in specified path
 */
function _detect_fancybox_files() {
  $path = libraries_get_path('fancybox');
  $js_files = glob(getcwd() . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'jquery.fancybox-*.js');
  $css_files = glob(getcwd() . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'jquery.*.css');
  $files = array(
    'js' => basename(array_shift($js_files)),
    'css' => basename(array_shift($css_files)),
  );
  return $files;
}
function _set_fancybox_files() {
  $files = _detect_fancybox_files();
  $path = libraries_get_path('fancybox');
  if (empty($files['js']) || empty($files['css'])) {
    drupal_set_message(t('No Fancybox plugin found in @path, install the Fancybox plugin and go to the Fancybox settings page for the files to be found', array(
      '@path' => $path,
    )), 'warning');
  }
  else {
    variable_set('fancybox_files', $files);
  }
  return $files;
}

Functions

Namesort descending Description
fancybox_activate Include the Fancybox JS.
fancybox_active Check whether Fancybox should be initialized for the current URL.
fancybox_field_formatter_info Implements hook_field_formatter_info().
fancybox_field_formatter_settings_form Implements hook_field_formatter_settings_form().
fancybox_field_formatter_settings_summary Implements hook_field_formatter_settings_summary().
fancybox_field_formatter_view Implements hook_field_formatter_view().
fancybox_init Implementation of hook_init().
fancybox_menu Implementation of hook_menu().
fancybox_options Define form elements for the Fancybox plugin's options.
fancybox_permission Implementation of hook_permission().
_detect_fancybox_files Detect needed script and stylesheet in specified path
_set_fancybox_files