Skip to content
colorbox.module 13.3 KiB
Newer Older
<?php
// $Id$
/**
 * @file
 * A light-weight, customizable lightbox plugin for jQuery 1.3
 */

/**
 * The default path to the Colorbox directory.
 */
define('COLORBOX_PATH', 'sites/all/libraries/colorbox');
define('COLORBOX_MIN_PLUGIN_VERSION', '1.3.6');


/**
 * Implmentation of hook_theme().
 */
function colorbox_theme() {
      'variables' => array(
        'path' => NULL,
        'title' => NULL,
        'gid' => NULL,
      ),
    'colorbox_insert_image' => array(
      'variables' => array(
        'item' => NULL,
        'widget' => NULL,
      ),
      'template' => 'colorbox-insert-image',
      'file' => 'colorbox.theme.inc',
    ),

    'colorbox_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'node' => NULL,
        'field' => array(),
        'colorbox_node_style' => NULL,
        'colorbox_image_style' => NULL,
      ),
      'file' => 'colorbox.theme.inc',
    ),
  );
}

/**
 * Implementation of hook_init().
 */
function colorbox_init() {
  _colorbox_doheader();
}

/**
 * Implementation of hook_views_api().
 */
function colorbox_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'colorbox') . '/views',
  $items['admin/config/media/colorbox'] = array(
    'description' => 'Adjust Colorbox settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('colorbox_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );
  $items['user/login/colorbox'] = array(
    'title' => 'Login',
    'page callback' => 'colorbox_login',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_CALLBACK,
  );
  $items['colorbox/form'] = array(
    'title' => 'Form',
    'access callback' => '_colorbox_form_page_access',
    'access arguments' => array(2),
function _colorbox_form_page_access($form_id) {
  $access = FALSE;

  // First check if this is one of the forms Colorbox support out of the box.
    case 'contact_site_form':
      $access = user_access('access site-wide contact form');
      break;
    case 'user_register_form':
      $access = user_register_access();
      break;
    case 'user_pass':
    case 'user_login':
    case 'user_login_block':
      $access = user_is_anonymous();
      break;
  }

  // Invoke hook_colorbox_form_access for all modules.
  if (!$access) {
    foreach (module_implements('colorbox_form_access') as $module) {
      if ($access = module_invoke($module, 'colorbox_form_access', $form_id)) {
        return $access;
      }
    }
/**
 * Check Colorbox dependencies, Colorbox plugin.
 *
 * @return
 *   array with TRUE/FALSE for each dependency.
 *
 * @see colorbox_requirements()
 */
function _colorbox_status() {
  $status = array();
  $status['colorbox_plugin'] = FALSE;

  if (version_compare(colorbox_get_version(), COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
    $status['colorbox_plugin'] = TRUE;
  }

  return $status;
}

/**
 * Check if Colorbox should be active for the current URL.
 *
 * @return
 *   TRUE if Colorbox should be active for the current page.
 */
function _colorbox_active() {
  // Code from the block_list funtion in block.module.
  $path = drupal_get_path_alias($_GET['q']);
  $colorbox_pages = variable_get('colorbox_pages', "admin*\nimg_assist*\nimce*\nnode/add/*\nnode/*/edit");
  // Compare with the internal and path alias (if any).
  $page_match = drupal_match_path($path, $colorbox_pages);
    $page_match = $page_match || drupal_match_path($_GET['q'], $colorbox_pages);
  }

  return !$page_match;
}

/**
 * Loads the various js and css files.
 */
function _colorbox_doheader() {
  static $already_added = FALSE;
  if ($already_added) {
    return; // Don't add the JavaScript and CSS multiple times.
  }
  if (!_colorbox_active()) {
    return; // Don't add the JavaScript and CSS on specified paths.
  }

  // Insert options and translated strings as javascript settings.
  if (variable_get('colorbox_custom_settings_activate', 0)) {
    $js_settings = array(
      'transition' => variable_get('colorbox_transition_type', 'elastic'),
      'speed' => variable_get('colorbox_transition_speed', 350),
      'opacity' => variable_get('colorbox_opacity', '0.9'),
      'slideshow' => variable_get('colorbox_slideshow', 0) ? TRUE : FALSE,
      'slideshowAuto' => variable_get('colorbox_slideshowauto', 1) ? TRUE : FALSE,
      'slideshowSpeed' => variable_get('colorbox_slideshowspeed', 2500),
      'slideshowStart' => variable_get('colorbox_text_start', 'start slideshow'),
      'slideshowStop' => variable_get('colorbox_text_stop', 'stop slideshow'),
      'current' => variable_get('colorbox_text_current', '{current} of {total}'),
      'previous' => variable_get('colorbox_text_previous', '« Prev'),
      'next' => variable_get('colorbox_text_next', 'Next »'),
      'close' => variable_get('colorbox_text_close', 'Close'),
      'overlayClose' => variable_get('colorbox_overlayclose', 1) ? TRUE : FALSE,
      'maxWidth' => variable_get('colorbox_maxwidth', '100%'),
      'maxHeight' => variable_get('colorbox_maxheight', '100%'),
    );
  }
  else {
    $js_settings = array(
      'transition' => 'elastic',
      'speed' => 350,
      'slideshowAuto' => FALSE,
      'slideshowSpeed' => 2500,
      'slideshowStart' => '',
      'slideshowStop' => '',
      'current' => t('{current} of {total}'),
      'previous' => t('« Prev'),
      'next' => t('Next »'),
      'close' => t('Close'),
      'overlayClose' => TRUE,
      'maxWidth' => '100%',
      'maxHeight' => '100%',

  $path = drupal_get_path('module', 'colorbox');
  $style = variable_get('colorbox_style', 'default');

  // Give other modules the possibility to override Colorbox settings and style.
  $data = &$js_settings;
  $data['__drupal_alter_by_ref'] = array(&$style);
  drupal_alter('colorbox_settings', $data);

  drupal_add_js(array('colorbox' => $js_settings), array('type' => 'setting', 'scope' => JS_DEFAULT));
  if (module_exists('image') && variable_get('colorbox_auto_image_nodes', 0)) {
    $image_derivative = variable_get('colorbox_image_derivative', 'preview');
    // If the image derivative is set to IMAGE_ORIGINAL check if the
    // user has access before activating Colorbox.
    if (!($image_derivative == IMAGE_ORIGINAL && !user_access('view original images'))) {
      drupal_add_js(array('colorbox' => array('image_derivative' => $image_derivative)), array('type' => 'setting', 'scope' => JS_DEFAULT));
      drupal_add_js($path . '/js/colorbox_image_module.js');
  // Add and initialise the Colorbox plugin.
  drupal_add_js($path . '/js/colorbox.js');
  // Add JS and CSS based on selected style.
  switch ($style) {
    case 'none':
      break;
    case 'default':
      drupal_add_css($path . '/styles/default/colorbox_default_style.css');
      drupal_add_js($path . '/styles/default/colorbox_default_style.js');
      break;
    case 'stockholmsyndrome':
      drupal_add_css($path . '/styles/stockholmsyndrome/colorbox_stockholmsyndrome.css');
      drupal_add_js($path . '/styles/stockholmsyndrome/colorbox_stockholmsyndrome.js');
      break;
    default:
      drupal_add_css($style . '/colorbox.css');
    drupal_add_js($path . '/js/colorbox_load.js');
  if (variable_get('colorbox_inline', 0)) {
    drupal_add_js($path . '/js/colorbox_inline.js');
  }

  if ($GLOBALS['user']->uid == 0 && variable_get('colorbox_login', 0)) {
    drupal_add_js($path . '/js/colorbox_login.js');
/**
 * Return the version of Colorbox plugin that is installed.
 *
 * This can be used by other modules' hook_requirements() to ensure that the
 * proper version of Colorbox plugin is installed.
 *
 * @see version_compare
 */
function colorbox_get_version($colorbox_path = NULL) {
  $version = 0;
  $pattern = '#ColorBox v([0-9\.a-z]+)#';

  // No file is passed in so use the default location.
  if (is_null($colorbox_path)) {
    $colorbox_path = colorbox_get_path();
  }

  // Return the version of Colorbox plugin.
  $colorbox_plugin = file_get_contents($colorbox_path, NULL, NULL, 0, 32);
  if (preg_match($pattern, $colorbox_plugin, $matches)) {
    $version = $matches[1];
  }

  return $version;
}

/**
 * Return the path to the Colorbox plugin.
 */
function colorbox_get_path() {
  $colorbox_file = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
  return variable_get('colorbox_path', COLORBOX_PATH) . '/colorbox/' . $colorbox_file[variable_get('colorbox_compression_type', 'min')];
/**
 * Implementation of hook_form_alter().
 * Reformat the login form.
 */
function colorbox_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == variable_get('colorbox_login_form', '') && arg(0) == 'user' && arg(1) == 'login' && arg(2) == 'colorbox') {
    $destination = isset($_GET['destination']) ? urlencode($_GET['destination']) : 'user';
    $form['#action'] = url('user/login', array('query' => array('destination' => $destination)));
    $form['name']['#size'] = 25;
    $form['pass']['#size'] = 25;
  }
/**
 * Implementation of hook_link_alter().
 */
function colorbox_link_alter(&$links, $node) {
  if ($node->type == 'image' && variable_get('colorbox_auto_image_nodes', 0)) {
    // Add a colorbox class to the image link sizes on the image node.
    foreach ($node->images as $size => $path) {
      $links['image_size_' . $size]['attributes']['class'] = "image image-{$size} colorbox";
      $links['image_size_' . $size]['href'] = $path;
      unset($links['image_size_' . $size]['query']);
 * Implementation of hook_field_formatter_info().
 */
function colorbox_field_formatter_info() {
  return array(
    'colorbox' => array(
      'label' => t('Colorbox'),
      'field types' => array('image'),
      'settings' => array('colorbox_node_style' => '', 'colorbox_image_style' => ''),
    ),
  );
/**
 * Implementation of hook_field_formatter_settings_form().
 */
function colorbox_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['colorbox_node_style'] = array(
    '#title' => t('Node image style'),
    '#type' => 'select',
    '#default_value' => $settings['colorbox_node_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['colorbox_image_style'] = array(
    '#title' => t('Colorbox image style'),
    '#type' => 'select',
    '#default_value' => $settings['colorbox_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );

  return $element;
 * Implementation of hook_field_formatter_settings_summary().
function colorbox_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['colorbox_node_style']])) {
    $summary[] = t('Node image style: @style', array('@style' => $image_styles[$settings['colorbox_node_style']]));
  }
  else {
    $summary[] = t('Node image style: Original image');
  }
  if (isset($image_styles[$settings['colorbox_image_style']])) {
    $summary[] = t('Colorbox image style: @style', array('@style' => $image_styles[$settings['colorbox_image_style']]));
  else {
    $summary[] = t('Colorbox image style: Original image');
  }

  return implode('<br />', $summary);
}

/**
 * Implementation of hook_field_formatter_view().
 */
function colorbox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'colorbox_image_formatter',
      '#item' => $item,
      '#node' => $entity,
      '#field' => $field,
      '#colorbox_node_style' => $display['settings']['colorbox_node_style'],
      '#colorbox_image_style' => $display['settings']['colorbox_image_style'],
/**
 * Implementation of hook_insert_styles().
 */
function colorbox_insert_styles() {
  $insert_styles = array();
  foreach (image_styles() as $key => $style) {
    $insert_styles['colorbox__' . $key] = array('label' => 'Colorbox ' . $style['name']);
  }

  return $insert_styles;
}

/**
 * Implementation of hook_insert_content().
 */
function colorbox_insert_content($item, $style, $widget) {
  list($item['module_name'], $item['style_name']) = explode('__', $style['name'], 2);
  return theme('colorbox_insert_image', $item, $widget);