Skip to content
lightbox2.module 17.1 KiB
Newer Older
 * Enables the use of lightbox V2 which places images above your current page,
 * not within.
 * This frees you from the constraints of the layout, particularly column
Stella Power's avatar
Stella Power committed
 * This module is for Drupal 5.x only.
 *
 * Module by: Mark Ashmead
 * Mailto: bugzie@gmail.com
Stella Power's avatar
Stella Power committed
 * Co-maintainer: Stella Power (http://drupal.org/user/66894)
 * GLOBAL VARIABLES
$_LIGHTBOX2_INCLUDE = false;
 * Implementation of hook_help().
 * 
 * Displays the information for this module.
 * Displays the help text for this module.
function lightbox2_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enables Lightbox V2 for Drupal');
    case 'admin/help#lightbox2':
      return t('<h3>Overview</h3>
Stella Power's avatar
Stella Power committed
      <p align="justify">Lightbox JS V2 is a simple, unobtrusive script used to overlay images on the current page. It\'s a snap to setup and works on all modern browsers. The module comes with a Lightbox2 Lite option which does not use the jQuery libraries; it is therefore less likely to conflict with anything else.</p>
      <p align="justify">Places images above your current page, not within. This frees you from the constraints of the layout, particularly column widths. Keeps users on the same page. Clicking to view an image and then having to click the back button to return to your site is bad for continuity (and no fun!).</p>
Stella Power's avatar
Stella Power committed
<h3>Features</h3>
The version 2 module has several benefits over the plain Lightbox module.  Note, not all of these features are available when the "Lightbox2 Lite" option is enabled.
<ul>
<li>Image Sets: group related images and navigate through them with ease - ideal for your image galleries.</li>
<li>Visual Effects: fancy pre-loader and transition when you click on the image.</li>
<li>Zoom Capability: larger images are reduced in size so they fit snugly inside the browser window.  A zoom button can then be clicked on to see it in its original size.</li>
Stella Power's avatar
Stella Power committed
<li>Automatic Image Node Detection: configurable automatic re-formatting of
image node thumbnails, so there is no need to add \'rel="lightbox"\' to each
image node link on your site.  <a
href="http://drupal.org/project/image">Image</a>, <a
href="http://drupal.org/project/inline">Inline</a>, <a
href="http://drupal.org/project/img_assist">Image Assist</a> and <a
href="http://drupal.org/project/imagefield">CCK Imagefield</a> modules are all
supported (images must have the "inline", "inline-image-link", "thumbnail" or "image-thumbnail" classes).</li>
Stella Power's avatar
Stella Power committed
<li><a href="http://drupal.org/project/imagecache">Imagecache</a> Support: adds a Lightbox2 field formatter for <a href="http://drupal.org/project/imagefield">CCK imagefields</a> for your custom <a href="http://drupal.org/project/views">views</a>.</li>
<li>Image Node Link: a link to the image node can be provided within the lightbox itself.</li>
<li>Page Exclusion Capability: exclude certain pages on your site from having the lightbox2 functionality.</li>
<li>Gallery 2 Support: support for Gallery 2 images via the <a href="http://drupal.org/project/gallery">Gallery</a> module (beta).</li>
</ul>
      <p align="justify"><h3>Usage</h3></p>
      <p>Add rel=&quot;lightbox&quot; attribute to any link tag to activate the lightbox. For example: </p>
      <p>&lt;a href=&quot;image-1.jpg&quot; rel=&quot;lightbox&quot; title=&quot;my caption&quot;&gt;image #1&lt;/a&gt;</p>
      <p>Optional: Use the title attribute if you want to show a caption.</p>
   <p>If you have a set of related images that you would like to group, follow
   step one but additionally include a group name between square brackets in the
   rel attribute. For example:</p><p>&lt;a href="images/image-1.jpg"
   rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br />
&lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]"&gt;image
#2&lt;/a&gt;<br />
&lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]"&gt;image
#3&lt;/a&gt;<br /></p>
Stella Power's avatar
Stella Power committed
<p>No limits to the number of image sets per page or how many images are allowed
in each set. Go nuts!</p>');
Stella Power's avatar
Stella Power committed
/**
 * Implementation of hook_perm()
 * Define the permissions this module uses
 */
Stella Power's avatar
Stella Power committed
function lightbox2_perm() {
  return array('administer lightbox2');
}

/**
 * Implementation of hook_menu()
 */
Stella Power's avatar
Stella Power committed
function lightbox2_menu($may_cache) {
  $items = array();
Stella Power's avatar
Stella Power committed
  if ($may_cache) {
    $items[] = array('path' => 'admin/settings/lightbox2',
      'title' => t('Lightbox2'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lightbox2_settings_form'),
      'access' => user_access('administer lightbox2'),
      'description' => t('Allows the user to configure the lightbox2 settings'),
    );
  }
Stella Power's avatar
Stella Power committed
  return $items;
}

/**
 * Implementation of hook_nodeapi().
 */
function lightbox2_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  global $_LIGHTBOX2_INCLUDE;
  if (lightbox2_exclude_these_paths() != 1) {
    if ($op == 'view' && !$_LIGHTBOX2_INCLUDE) {
      lightbox2_add_files();
      $_LIGHTBOX2_INCLUDE = true;
    }
    elseif ($node->type == 'image' && !$_LIGHTBOX2_INCLUDE) {
      lightbox2_add_files();
      $_LIGHTBOX2_INCLUDE = true;
    }
Stella Power's avatar
Stella Power committed
 * Implementation of hook_settings()
Stella Power's avatar
Stella Power committed
function lightbox2_settings_form() {

  // add the javascript which disables / enables form elements
  drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');

  // Define Lightbox2 Lite fieldset.
  /* ------------------------------- */
  $use_lite = variable_get('lightbox2_lite', false);
  $form['lightbox2_lite_options'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
  // Add Checkbox for Lightbox2 Lite.
  $form['lightbox2_lite_options']['lightbox2_lite'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Lightbox2 Lite'),
    '#description' => t('Checking this box will enable Lightbox2 Lite and will disable all of the automatic image node URL re-formatting features.  It also disables all grouping features.'),
    '#default_value' => $use_lite,

  // Define General settings fieldset
  /* --------------------------------- */
  $form['lightbox2_general_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
 
  // Add Checkbox for Alternative Layout
  $form['lightbox2_general_options']['lightbox2_use_alt_layout'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Alternative Layout'),
    '#description' => t('Enabling this option alters the layout of the lightbox elements.  This doesn\'t apply when using Lightbox Lite.'),
    '#default_value' => variable_get('lightbox2_use_alt_layout', false),
  );

  // Add Checkbox for Force Navigation display
  $form['lightbox2_general_options']['lightbox2_force_show_nav'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force visibility of navigation links'),
    '#description' => t('When viewing grouped images using the default layout, the navigational links to the next and previous images are only displayed when you hover over the image.  Checking this box forces these links to be displayed all the time.'),
    '#default_value' => variable_get('lightbox2_force_show_nav', false),
  );

  // Add text box for image count for grouping
  $form['lightbox2_general_options']['lightbox2_image_count_str'] = array(
    '#type' => 'textfield',
    '#title' => t('Image Count Text'),
    '#description' => t('This text is used to display the image count underneath the image in the lightbox when image grouping is enabled.  Use !current as a placeholder for the number of the current image and !total for the total number of images in the group.  For example, "Image !current of !total".'),
    '#default_value' => variable_get('lightbox2_image_count_str', 'Image !current of !total'),
  );
   
  // Add Checkbox for Zoom image
  $form['lightbox2_general_options']['lightbox2_disable_zoom'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable Zoom Feature'),
    '#description' => t('By default, when the image being displayed in the lightbox is larger than the browser window, it is resized to fit within the window and a zoom button is provided for users who wish to view the image in its original size.  Checking this box will disable this feature and all images will be displayed without any resizing.'),
    '#default_value' => variable_get('lightbox2_disable_zoom', false),
  );
  // Add Checkbox for Gallery2 Image Filter.
  $form['lightbox2_general_options']['lightbox2G2_filter'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Gallery 2 Filter'),
    '#description' => t('Checking this box will enable the Gallery 2 filter.'),
    '#default_value' => variable_get('lightbox2G2_filter', true),
  );
  
  // Add Textarea for urls without lightbox
  $form['lightbox2_general_options']['lightbox2_disable_these_urls'] = array(
    '#type' => 'textarea',
    '#title' => t('Disable LightBox for these urls'),
    '#description' => t('List the urls where the lightbox should be disabled, e.g. "node/add/page".  Use &lt;front&gt; to match the front page.  Put each url on a separate line. You can use the % character as a wildcard.'),
    '#default_value' => variable_get('lightbox2_disable_these_urls', ''),
  );


  // Define Image Node Options fieldset.
  /* ----------------------------------- */
  $form['image_node_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Automatic image node options'),
Stella Power's avatar
Stella Power committed
    '#description' => t('These options allow automatic URL re-formatting of Image Nodes.  This removes the need for you to add \'rel="lightbox"\' to each image node link throughout your site.  Only image nodes which have the CSS classes "inline", "thumbnail" or "image-thumbnail" will have their URLs automatically re-formatted.  This feature is not available when using Lightbox2 Lite.'),
    '#collapsible' => TRUE,
  // Add Checkbox for Image Node.
  $form['image_node_options']['lightbox2_image_node'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable for Image Nodes'),
    '#description' => t('Checking this box will enable automatic URL formatting for Image Nodes.'),
    '#default_value' => variable_get('lightbox2_image_node', true),
  // Add text box for link text to node
  $form['image_node_options']['lightbox2_node_link_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Text for Node Link'),
    '#description' => t('This is the text that will appear as the link to the node underneath the image in the lightbox'),
    '#default_value' => variable_get('lightbox2_node_link_text', 'View Image Details'),
  );
  
  // Add Checkbox for Image Node Grouping.
  $form['image_node_options']['lightbox2_image_group'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Grouping'),
    '#description' => t('Checking this box will enable automatic grouping of Image Nodes on a page. Useful for image galleries.'),
    '#default_value' => variable_get('lightbox2_image_group', true),
  // Add Checkbox for disabling lightbox for gallery lists
  $form['image_node_options']['lightbox2_disable_nested_galleries'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable Lightbox for Gallery Lists'),
    '#description' => t('Checking this box will disable the lightbox for images in gallery lists.  This means it is possible to open a gallery by clicking on the teaser image, but the lightbox will still appear when viewing images within the gallery.'),
    '#default_value' => variable_get('lightbox2_disable_nested_galleries', true),
  return system_settings_form($form);
Stella Power's avatar
Stella Power committed

 * Implementation of hook_filter().
 */
function lightbox2_filter_tips($delta, $format, $long = false) {
Stella Power's avatar
Stella Power committed
  return t('Image links from G2 are formatted for use with Lightbox.V2');
Stella Power's avatar
Stella Power committed

// Check to see if the G2 Filter is Enabled in Settings
if (variable_get('lightbox2G2_filter', true)) {
Stella Power's avatar
Stella Power committed
  function lightbox2_filter($op, $delta = 0, $format = -1, $text = '') {
    switch ($op) {
      case 'list':
        return array(0 => t('Lightbox filter'));
Stella Power's avatar
Stella Power committed
      case 'description':
        return t('Turns g2_filter links into Lightbox.V2 appropriate links');
Stella Power's avatar
Stella Power committed
      case 'process':
        $text = ' '. $text .' ';
        $text = preg_replace('/ShowItem/', 'DownloadItem', $text);
        $text = preg_replace('/target=""/', 'rel="lightbox"', $text);
Stella Power's avatar
Stella Power committed
        return $text;
Stella Power's avatar
Stella Power committed
      default:
        return $text;
    }
/**
 * Provides a link to the CSS stylesheet associated with this module.
 * Provides a link to the JS file associated with this module.
 */
function lightbox2_add_files() {
  // Load required js and css files.
  $path = drupal_get_path('module', 'lightbox2');
  // load the javascript settings
  $js_settings = array(
    'use_alt_layout' => variable_get('lightbox2_use_alt_layout', false),
    'disable_zoom' => variable_get('lightbox2_disable_zoom', false),
    'force_show_nav' => variable_get('lightbox2_force_show_nav', false),
    'group_images' => variable_get('lightbox2_image_group', true),
    'disable_for_gallery_lists' => variable_get('lightbox2_disable_nested_galleries', true),
    'node_link_text' => check_plain(t(variable_get('lightbox2_node_link_text', 'View Image Details'))),
    'image_count' => check_plain(t(variable_get('lightbox2_image_count_str', 'Image !current of !total'))),
Stella Power's avatar
Stella Power committed
    'lite_press_x_close' => t('press !x to close', array('!x' => '<a href="#" onclick="hideLightbox(); return false;"><kbd>x</kbd></a>')),
  );
  drupal_add_js(array('lightbox2' => $js_settings), 'setting');

  // Lightbox2 Plus
  if (!variable_get('lightbox2_lite', false)) {
Stella Power's avatar
Stella Power committed
    $css = $path .'/css/lightbox.css';
    if (variable_get('lightbox2_use_alt_layout', false)) {
      $css = $path .'/css/lightbox_alt.css';
    }
    if (function_exists('drupal_add_css')) {
Stella Power's avatar
Stella Power committed
      drupal_add_css($css);
Stella Power's avatar
Stella Power committed
    }
    else {
Stella Power's avatar
Stella Power committed
      theme('add_style', $css);
Stella Power's avatar
Stella Power committed
    }
    // Check to see if the Image Node Option is enabled in settings.
    if (variable_get('lightbox2_image_node', true)) {
      drupal_add_js($path .'/js/image_nodes.js');
Stella Power's avatar
Stella Power committed
  }
Stella Power's avatar
Stella Power committed
    $css = $path .'/css/lightbox_lite.css';
    if (function_exists('drupal_add_css')) {
Stella Power's avatar
Stella Power committed
      drupal_add_css($css);
Stella Power's avatar
Stella Power committed
      theme('add_style', $css);
    drupal_add_js($path .'/js/lightbox_lite.js');
  drupal_add_js($path .'/js/lightningload.js');
 * Implementation of hook_field_formatter_info().
 * Adds certain lightbox+imagecache formatters to CCK image fields if the
 * imagefield.module and the imagecache.module exist.
 */
function lightbox2_field_formatter_info() {
  $formatter = array();
  if (module_exists('imagefield') && module_exists('imagecache')) {
    $rules = _imagecache_get_presets();
    foreach ($rules as $ruleid => $rulename) {
      $formatters['lightbox2]['. $rulename] = array(
        'label' => 'Lightbox2: '. $rulename,
        'field types' => array('image'),
      );
  return $formatters;
 * Implementation of hook_field_formatter().
 */
function lightbox2_field_formatter($field, $item, $formatter) {
  if (module_exists('imagefield') && module_exists('imagecache')) {
    if (!isset($item['fid'])) {
      return '';
    }
    if (!$_LIGHTBOX2_INCLUDE && lightbox2_exclude_these_paths() != 1) {
      lightbox2_add_files();
      $_LIGHTBOX2_INCLUDE = true;
    }
    $file = _imagefield_file_load($item['fid']);
    $item = array_merge($item, $file);
    if (strpos($formatter, 'lightbox2][') !== false) {
      list($null, $namespace) = explode('][', $formatter, 2);
      $rules = _imagecache_get_presets();
      if (in_array($namespace, (array) $rules)) {
        return theme('imagefield_image_imagecache_lightbox2', $namespace, $field, $item);
      }
    }
  }
 * Implementation of theme_imagefield_image_imagecache_lightbox2().
 */
function theme_imagefield_image_imagecache_lightbox2($namespace, $field, $item, $attributes = NULL) {
  $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $item['filepath']);
  
  $rel = 'lightbox';
  if (variable_get('lightbox2_image_group', TRUE)) {
    $rel = 'lightbox[cck_image_nodes]';
  
  $node_link = '';
  if (!empty($item['nid'])) {
    $node_link = '<br /><br />'. l(t('View Image Information'), 'node/'. $item['nid']);
  $link_attributes = array(
    'rel' => $rel,
    'title' => $item['alt'] . $node_link);
  
  $image = theme('imagecache', $namespace, $item['filepath'], $item['alt'], $item['title'], $attributes);
  $output = l($image, $item['filepath'], $link_attributes, NULL, NULL, FALSE, TRUE);
  return $output;

function lightbox2_exclude_these_paths() {
  // Use lightbox if this url is not excluded
  $disabled_urls = variable_get('lightbox2_disable_these_urls', '');
  
Stella Power's avatar
Stella Power committed
    $urls = explode("\n", $disabled_urls);
      $url = rtrim($url);
      if ($url == "<front>") {
        $url = "";
      }
      $url = preg_replace('/%/', '.*?', preg_quote($url, '/'));
      $pattern = '/^'. $url .'$/';
      if (preg_match($pattern, $_REQUEST['q'])) {