You are here

shadowbox.module in Shadowbox 8

Shadowbox, a JavaScript media viewer application for displaying content in a modal dialogue.

File

shadowbox.module
View source
<?php

/**
 * @file
 * Shadowbox, a JavaScript media viewer application for displaying content in a
 * modal dialogue.
 */

/**
 * Shadowbox library default path.
 */
define('SHADOWBOX_DEFAULT_PATH', 'libraries/shadowbox');

/**
 * Enable on every page except the listed pages.
 */
define('SHADOWBOX_ACTIVATION_NOTLISTED', 0);

/**
 * Enable on only the listed pages.
 */
define('SHADOWBOX_ACTIVATION_LISTED', 1);

/**
 * Enable if the following PHP code returns TRUE.
 */
define('SHADOWBOX_ACTIVATION_PHP', 2);

/**
 * Implements hook_help().
 */
function shadowbox_help($path, $arg) {
  switch ($path) {
    case 'admin/help#shadowbox':
      return t('
<p>Shadowbox is a modal media viewer application akin to !lightbox and !thickbox. You can use it to display images, movies, and other web content in a window that is overlaid on top of the page.</p>

<p>The module is integrated with filefield\'s image element so you can select from a number of different options for displaying images uploaded to nodes and shown in views.</p>

<p>For instructions on manually crafting your links please see the !usage.</p>

<p>This module has !options that work on a site-wide basis.</p>', array(
        '!lightbox' => l('lightbox2', 'http://drupal.org/project/lightbox2'),
        '!thickbox' => l('thickbox', 'http://drupal.org/project/thickbox'),
        '!options' => l('options', 'admin/settings/shadowbox'),
        '!usage' => l('official usage guide', 'http://www.shadowbox-js.com/usage.html#markup'),
      ));
    case 'admin/settings/shadowbox':
      return t('<p>This page provides access to the Shadowbox settings. The settings here work globally so any changes made here will affect Shadowbox for the entire site.</p>');
  }
}

/**
 * Implements hook_permission().
 */
function shadowbox_permission() {
  return array(
    'administer shadowbox' => array(
      'title' => t('Administer shadowbox'),
      'description' => t('Perform administration tasks for shadowbox.'),
    ),
  );
}

/**
 * Implements hook_page_build().
 */
function shadowbox_page_build(&$page) {
  $shb_enabled = \Drupal::config('shadowbox.settings')
    ->get('shadowbox_enabled');
  $shb_auto = \Drupal::config('shadowbox.automatic')
    ->get('shadowbox_auto_enable_all_images');
  $shb_enable_globally = \Drupal::config('shadowbox.automatic')
    ->get('shadowbox_enable_globally');

  // Add shadowbox library files.
  if ($shb_enabled && ($shb_auto || $shb_enable_globally) && _shadowbox_activation()) {

    // Add the base files.
    $page['#attached']['library'][] = 'shadowbox/shadowbox';
  }
}

/**
 * Implements hook_library_alter().
 */
function shadowbox_library_alter(array &$library, $name, $theme = NULL) {
  if ($name == 'shadowbox/shadowbox') {
    $shadowbox_location = \Drupal::config('shadowbox.settings')
      ->get('shadowbox_location');

    // Alter shadowbox JavaScript library registry.
    $library['js'][] = array(
      'type' => 'setting',
      'data' => _shadowbox_get_settings(),
    );
    $library['js'][] = array(
      'type' => 'inline',
      'scope' => 'header',
      'group' => JS_THEME,
      'data' => 'Shadowbox.path = "' . base_path() . $shadowbox_location . '/";',
    );

    // Alter shadowbox CSS library registry.
    $library['css'][] = array(
      'type' => 'inline',
      'media' => 'print',
      'data' => '
        #sb-container { position: relative; }
        #sb-overlay { display: none; }
        #sb-wrapper { position: relative; top: 0; left: 0; }
        #sb-loading { display: none; }
      ',
    );

    // Shadowbox js and css files location.
    if ($library['js'][0]['data'] != $shadowbox_location . '/shadowbox.js') {
      $library['js'][0]['data'] = $shadowbox_location . '/shadowbox.js';
    }
    if ($library['css'][0]['data'] != $shadowbox_location . '/shadowbox.css') {
      $library['css'][0]['data'] = $shadowbox_location . '/shadowbox.css';
    }
  }
}

/**
 * Construct the JS settings array.
 *
 * @param $override
 *   An array of settings to override global values.
 *
 * @return
 *   An array containing settings.
 */
function _shadowbox_get_settings($override = array()) {
  $config = \Drupal::config('shadowbox.settings');
  $config_automatic = \Drupal::config('shadowbox.automatic');
  $settings = array(
    'animate' => $config
      ->get('shadowbox_animate'),
    'animateFade' => $config
      ->get('shadowbox_animate_fade'),
    'animSequence' => $config
      ->get('shadowbox_animation_sequence'),
    'auto_enable_all_images' => $config_automatic
      ->get('shadowbox_auto_enable_all_images'),
    'auto_gallery' => $config_automatic
      ->get('shadowbox_auto_gallery'),
    'autoplayMovies' => $config
      ->get('shadowbox_autoplay_movies'),
    'continuous' => $config
      ->get('shadowbox_continuous_galleries'),
    'counterLimit' => $config
      ->get('shadowbox_counter_limit'),
    'counterType' => $config
      ->get('shadowbox_counter_type'),
    'displayCounter' => $config
      ->get('shadowbox_display_counter'),
    'displayNav' => $config
      ->get('shadowbox_display_nav'),
    'enableKeys' => $config
      ->get('shadowbox_enable_keys'),
    'fadeDuration' => $config
      ->get('shadowbox_fade_duration'),
    'handleOversize' => $config
      ->get('shadowbox_handle_oversize'),
    'handleUnsupported' => $config
      ->get('shadowbox_handle_unsupported'),
    'initialHeight' => $config
      ->get('shadowbox_initial_height'),
    'initialWidth' => $config
      ->get('shadowbox_initial_width'),
    'modal' => $config
      ->get('shadowbox_overlay_listen'),
    'overlayColor' => '#' . $config
      ->get('shadowbox_overlay_color'),
    'overlayOpacity' => $config
      ->get('shadowbox_overlay_opacity'),
    'resizeDuration' => $config
      ->get('shadowbox_resize_duration'),
    'showMovieControls' => $config
      ->get('shadowbox_show_movie_controls'),
    'slideshowDelay' => $config
      ->get('shadowbox_slideshow_delay'),
    'viewportPadding' => $config
      ->get('shadowbox_viewport_padding'),
    'useSizzle' => $config
      ->get('shadowbox_use_sizzle'),
    'language' => \Drupal::languageManager()
      ->getCurrentLanguage()->id,
  );
  $settings = array_merge($settings, $override);
  $settings = array(
    'shadowbox' => $settings,
  );
  return $settings;
}

/**
 * Verify that Shadowbox should be activation for the current URL.
 *
 * @return
 *   TRUE if Shadowbox should be activation for the current page.
 */
function _shadowbox_activation() {
  $config = \Drupal::config('shadowbox.settings');
  $pages = $config
    ->get('shadowbox_pages');
  $activation = $config
    ->get('shadowbox_activation_type');

  // Match path if necessary.
  if ($pages) {

    // Convert path to lowercase. This allows comparison of the same path with
    // different case. Ex: /Page, /page, /PAGE.
    $lowercase_pages = drupal_strtolower($pages);
    if ($activation < SHADOWBOX_ACTIVATION_PHP) {
      $current_path = current_path();

      // Convert the Drupal path to lowercase.
      $path_alias = \Drupal::service('path.alias_manager.cached')
        ->getPathByAlias($current_path);
      $path = drupal_strtolower($path_alias);

      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = drupal_match_path($path, $lowercase_pages);
      if ($path != $current_path) {
        $page_match = $page_match || drupal_match_path($current_path, $lowercase_pages);
      }

      // When $ACTIVATION has a value of 0 (SHADOWBOX_ACTIVATION_NOTLISTED),
      // shadowbox is added on all pages except those listed in $pages.
      // When set to 1 (SHADOWBOX_ACTIVATION_LISTED), it is added only on those
      // pages listed in $pages.
      $page_match = !($activation xor $page_match);
    }
    elseif (\Drupal::moduleHandler()
      ->moduleExists('php')) {
      $page_match = php_eval($pages);
    }
    else {
      $page_match = FALSE;
    }
  }
  else {
    $page_match = TRUE;
  }
  return $page_match;
}

/**
 * Implements hook_theme().
 */
function shadowbox_theme() {
  return array(
    'shadowbox_formatter' => array(
      'variables' => array(
        'innerHTML' => '',
        'url' => '',
        'rel' => '',
        'title' => '',
        'class' => '',
      ),
    ),
    'shadowbox_thumbnail' => array(
      'variables' => array(
        'path' => '',
        'image_style' => '',
        'attributes' => '',
        'title' => '',
        'alt' => '',
      ),
    ),
  );
}

/**
 * Returns HTML for a field formatted with shadowbox.
 *
 * @param $variables
 *   An associative array containing:
 *   - innerHTML: The html inside the shadowbox link.
 *   - url: The shadowbox link url.
 *   - rel: The rel attribute for shadowbox link.
 *   - title: The title attribute for shadowbox link.
 *   - class: The class attribute for the wrapper.
 *
 * @ingroup themeable
 */
function theme_shadowbox_formatter($variables) {

  // The inner HTML. Can be a thumbnail, ico, ...
  $output = $variables['innerHTML'];

  // The link.
  $link_attributes = array(
    'rel' => $variables['rel'],
    'title' => $variables['title'],
  );
  $options = array(
    'attributes' => $link_attributes,
    'html' => TRUE,
  );
  $output = l($output, $variables['url'], $options);

  // Wrap shadowbox link into a div layer.
  $output = '<div class="' . $variables['class'] . '"' . ($variables['innerHTML'] == '' ? ' style="display:none;"' : '') . '>' . $output . '</div>';
  return $output;
}

/**
 * Returns an image.
 *
 * @param $variables
 *   An associative array containing:
 *   - path: The uri || url of the image.
 *   - image_style: An optional image style.
 *   - attributes: An optional array of attributes.
 *   - title: The title to use on the image.
 *   - alt: An optional alt attribute.
 *
 * @ingroup themeable
 */
function theme_shadowbox_thumbnail($variables) {
  $image_style = $variables['image_style'];
  if ($image_style) {
    $output = array(
      '#theme' => 'image_style',
      '#style_name' => $image_style,
      '#uri' => $variables['path'],
      '#alt' => $variables['alt'] ? $variables['alt'] : $variables['title'],
      '#title' => $variables['title'],
      '#attributes' => array(
        'class' => array(
          'image-' . $image_style,
        ),
      ),
    );
  }
  else {
    $output = array(
      '#theme' => 'image',
      '#uri' => $variables['path'],
      '#alt' => $variables['alt'] ? $variables['alt'] : $variables['title'],
      '#title' => $variables['title'],
      '#attributes' => $variables['attributes'],
    );
  }
  return drupal_render($output);
}

Functions

Namesort descending Description
shadowbox_help Implements hook_help().
shadowbox_library_alter Implements hook_library_alter().
shadowbox_page_build Implements hook_page_build().
shadowbox_permission Implements hook_permission().
shadowbox_theme Implements hook_theme().
theme_shadowbox_formatter Returns HTML for a field formatted with shadowbox.
theme_shadowbox_thumbnail Returns an image.
_shadowbox_activation Verify that Shadowbox should be activation for the current URL.
_shadowbox_get_settings Construct the JS settings array.

Constants

Namesort descending Description
SHADOWBOX_ACTIVATION_LISTED Enable on only the listed pages.
SHADOWBOX_ACTIVATION_NOTLISTED Enable on every page except the listed pages.
SHADOWBOX_ACTIVATION_PHP Enable if the following PHP code returns TRUE.
SHADOWBOX_DEFAULT_PATH Shadowbox library default path.