You are here

photobox.module in PhotoboxPhotobox 7

Main file for the Photobox module.

File

photobox.module
View source
<?php

/**
 * @file
 * Main file for the Photobox module.
 */

/**
 * Implements hook_menu().
 */
function photobox_menu() {
  $items = array(
    'admin/config/media/photobox' => array(
      'title' => 'Photobox',
      'description' => 'Configure Photobox settings.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'photobox_settings_form',
      ),
      'access arguments' => array(
        'administer site configuration',
      ),
      'file' => 'photobox.admin.inc',
    ),
  );
  return $items;
}

/**
 * Implements hook_field_formatter_info().
 */
function photobox_field_formatter_info() {
  $formatters = array(
    'photobox' => array(
      'label' => t('Photobox'),
      // 'field types' => array('image', 'imagefield_crop', 'media', 'field_collection'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'photobox_content_image_style' => '',
        'photobox_content_image_style_first' => '',
        'photobox_image_style' => '',
        'photobox_gallery' => 'post',
        'photobox_gallery_custom' => '',
        'photobox_caption' => 'auto',
        'photobox_caption_custom' => '',
      ),
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function photobox_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $options = image_style_options(FALSE);
  $element['photobox_content_image_style'] = array(
    '#title' => t('Content image style'),
    '#type' => 'select',
    '#default_value' => $settings['photobox_content_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $options,
  );
  $element['photobox_content_image_style_first'] = array(
    '#title' => t('Content image style for first image'),
    '#type' => 'select',
    '#default_value' => $settings['photobox_content_image_style_first'],
    '#empty_option' => t('None (original image)'),
    '#options' => $options,
  );
  $element['photobox_image_style'] = array(
    '#title' => t('Photobox image style'),
    '#type' => 'select',
    '#default_value' => $settings['photobox_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $options,
  );
  $gallery = array(
    'post' => t('Per post gallery'),
    'page' => t('Per page gallery'),
    'field_post' => t('Per field in post gallery'),
    'field_page' => t('Per field in page gallery'),
    'custom' => t('Custom'),
    'none' => t('No gallery'),
  );
  $element['photobox_gallery'] = array(
    '#title' => t('Gallery (image grouping)'),
    '#type' => 'select',
    '#default_value' => $settings['photobox_gallery'],
    '#options' => $gallery,
    '#description' => t('How Photobox should group the image galleries.'),
  );
  $element['photobox_gallery_custom'] = array(
    '#title' => t('Custom gallery'),
    '#type' => 'textfield',
    '#maxlength' => 32,
    '#default_value' => $settings['photobox_gallery_custom'],
    '#description' => t('All images on a page with the same gallery value (rel attribute) will be grouped together. It must only contain lowercase letters, numbers, hyphen and underscores.'),
    '#element_validate' => array(
      'photobox_gallery_custom_validate',
    ),
    '#required' => FALSE,
    '#states' => array(
      'visible' => array(
        ':input[name$="[settings_edit_form][settings][photobox_gallery]"]' => array(
          'value' => 'custom',
        ),
      ),
    ),
  );
  $caption = array(
    'auto' => t('Automatic'),
    'title' => t('Title text'),
    'alt' => t('Alt text'),
    'content_title' => t('Content title'),
    'custom' => t('Custom (with tokens)'),
    'none' => t('None'),
  );
  $element['photobox_caption'] = array(
    '#title' => t('Caption'),
    '#type' => 'select',
    '#default_value' => $settings['photobox_caption'],
    '#options' => $caption,
    '#description' => t('Automatic will use the first non-empty value of the title, the alt text and the content title.'),
  );
  $element['photobox_caption_custom'] = array(
    '#title' => t('Custom caption'),
    '#type' => 'textfield',
    '#default_value' => $settings['photobox_caption_custom'],
    '#states' => array(
      'visible' => array(
        ':input[name$="[settings_edit_form][settings][photobox_caption]"]' => array(
          'value' => 'custom',
        ),
      ),
    ),
  );

  // Allow users to hide or set a custom recursion limit.
  // The module token_tweaks sets a global recursion limit that can not be bypassed.
  if (module_exists('token')) {
    $recursion_limit = min(variable_get('token_tree_recursion_limit', 3), variable_get('colorbox_token_recursion_limit', 3));
    $element['photobox_token'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      '#theme' => 'token_tree',
      '#token_types' => array(
        $instance['entity_type'],
        'file',
      ),
      '#recursion_limit' => $recursion_limit,
      '#dialog' => TRUE,
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][photobox_caption]"]' => array(
            'value' => 'custom',
          ),
        ),
      ),
    );
  }
  else {
    $element['photobox_token'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement patterns'),
      '#description' => '<strong class="error">' . t('For token support the <a href="@token_url">token module</a> must be installed.', array(
        '@token_url' => 'http://drupal.org/project/token',
      )) . '</strong>',
      '#states' => array(
        'visible' => array(
          ':input[name$="[settings_edit_form][settings][photobox_caption]"]' => array(
            'value' => 'custom',
          ),
        ),
      ),
    );
  }
  return $element;
}

/**
 * Validate function for photobox_gallery_custom.
 */
function photobox_gallery_custom_validate($element, &$form_state) {
  if (!empty($element['#value']) && !preg_match('!^[a-z0-9_-]+$!', $element['#value'])) {
    form_error($element, t('%name must only contain lowercase letters, numbers, hyphen and underscores.', array(
      '%name' => $element['#title'],
    )));
  }
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function photobox_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['photobox_content_image_style']])) {
    $summary[] = t('Content image style: @style', array(
      '@style' => $image_styles[$settings['photobox_content_image_style']],
    ));
  }
  else {
    $summary[] = t('Content image style: @style', array(
      '@style' => t('Original image'),
    ));
  }
  if (isset($image_styles[$settings['photobox_content_image_style_first']])) {
    $summary[] = t('Content image style for first image: @style', array(
      '@style' => $image_styles[$settings['photobox_content_image_style_first']],
    ));
  }
  else {
    $summary[] = t('Content image style: @style', array(
      '@style' => t('Original image'),
    ));
  }
  if (isset($image_styles[$settings['photobox_image_style']])) {
    $summary[] = t('Photobox image style: @style', array(
      '@style' => $image_styles[$settings['photobox_image_style']],
    ));
  }
  else {
    $summary[] = t('Photobox image style: @style', array(
      '@style' => t('Original image'),
    ));
  }
  $gallery = array(
    'post' => t('Per post gallery'),
    'page' => t('Per page gallery'),
    'field_post' => t('Per field in post gallery'),
    'field_page' => t('Per field in page gallery'),
    'custom' => t('Custom'),
    'none' => t('No gallery'),
  );
  if (isset($settings['photobox_gallery'])) {
    $summary[] = t('Photobox gallery type: @type', array(
      '@type' => $gallery[$settings['photobox_gallery']],
    )) . ($settings['photobox_gallery'] == 'custom' ? ' (' . $settings['photobox_gallery_custom'] . ')' : '');
  }
  $caption = array(
    'auto' => t('Automatic'),
    'title' => t('Title text'),
    'alt' => t('Alt text'),
    'content_title' => t('Content title'),
    'custom' => t('Custom (with tokens)'),
    'none' => t('None'),
  );
  if (isset($settings['photobox_caption'])) {
    $summary[] = t('Photobox caption: @type', array(
      '@type' => $caption[$settings['photobox_caption']],
    ));
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function photobox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    $element[$delta] = array(
      '#theme' => 'photobox_image_formatter',
      '#item' => $item,
      '#entity_type' => $entity_type,
      '#entity' => $entity,
      '#field' => $field,
      '#display_settings' => $display['settings'],
      '#delta' => $delta,
    );

    // We need to add it to every delta for the File entity display to work correctly.
    $element[$delta]['#attached'] = photobox_attached_resources();
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function photobox_theme($existing, $type, $theme, $path) {
  return array(
    'photobox_image_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'entity_type' => NULL,
        'entity' => NULL,
        'field' => array(),
        'display_settings' => array(),
        'delta' => NULL,
      ),
      'file' => 'photobox.theme.inc',
    ),
    'photobox_imagefield' => array(
      'variables' => array(
        'image' => array(),
        'path' => NULL,
        'title' => NULL,
        'gid' => NULL,
      ),
      'file' => 'photobox.theme.inc',
    ),
  );
}

/**
 * Utility function to get #attached resources.
 */
function photobox_attached_resources() {
  $settings = array();
  foreach (_photobox_settings() as $key => $setting) {
    $settings[$key] = (bool) $setting;
  }
  return array(
    'library' => array(
      array(
        'photobox',
        'photobox',
      ),
    ),
    'js' => array(
      drupal_get_path('module', 'photobox') . '/photobox.js',
      array(
        'data' => array(
          'photobox' => $settings,
        ),
        'type' => 'setting',
      ),
    ),
  );
}

/**
 * Implements hook_library().
 */
function photobox_library() {
  $libraries['photobox'] = array(
    'title' => 'Photobox',
    'website' => 'http://dropthebit.com/500/photobox-css3-image-gallery-jquery-plugin/',
    'version' => _photobox_library_version(),
    'js' => array(
      _photobox_library_path_js() => array(),
    ),
    'css' => array(
      _photobox_library_path() . '/photobox.css' => array(
        'type' => 'file',
      ),
    ),
  );
  return $libraries;
}

/**
 * Returns the path to the Photobox library.
 */
function _photobox_library_path() {
  $library_path =& drupal_static(__FUNCTION__, NULL);
  if (is_null($library_path)) {
    $library_path = variable_get('photobox_library_path', module_exists('libraries') ? libraries_get_path('photobox') : 'sites/all/libraries/photobox');
    if (!photobox_library_path_check($library_path)) {
      $library_path .= '/photobox';
      if (!photobox_library_path_check($library_path)) {
        watchdog('photobox', 'Photobox library is missing.', array(), WATCHDOG_ERROR);
        $library_path = FALSE;
      }
    }
  }
  return $library_path;
}
function photobox_library_path_check($library_path) {
  if (file_exists($library_path . '/jquery.photobox.js') || file_exists($library_path . '/photobox.js') || file_exists($library_path . '/photobox.min.js')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Returns the path to the Photobox library js file.
 */
function _photobox_library_path_js() {
  $library_path_js =& drupal_static(__FUNCTION__, NULL);
  if (is_null($library_path_js) && ($library_path = _photobox_library_path())) {
    if (file_exists($library_path . '/jquery.photobox.js')) {
      $library_path_js = $library_path . '/jquery.photobox.js';
    }
    elseif (file_exists($library_path . '/photobox.min.js')) {
      $library_path_js = $library_path . '/photobox.min.js';
    }
    elseif (file_exists($library_path . '/photobox.js')) {
      $library_path_js = $library_path . '/photobox.js';
    }
  }
  return $library_path_js;
}

/**
 * Returns version of the Photobox library.
 */
function _photobox_library_version() {
  $version =& drupal_static(__FUNCTION__, NULL);
  if (is_null($version) && ($library_path = _photobox_library_path())) {
    $pattern = '/photobox v([0-9\\.a-z]+)/';
    $photobox_js = file_get_contents(_photobox_library_path_js(), NULL, NULL, 0, 32);
    if (preg_match($pattern, $photobox_js, $matches)) {
      $version = $matches[1];
    }
    else {
      $version = 'Unknown';
    }
  }
  return $version;
}

/**
 * Helper function returning list of Photobox library settings.
 */
function _photobox_settings() {
  return array(
    'history' => variable_get('photobox_history', 1),
    'loop' => variable_get('photobox_loop', 1),
    'thumbs' => variable_get('photobox_thumbs', 1),
    'zoomable' => variable_get('photobox_zoomable', 1),
  );

  /*
  'photobox_counter' => 1,
  'photobox_hide_flash' => 1,
  'photobox_keys_close' => '27, 88, 67',
  'photobox_keys_prev' => '37, 80',
  'photobox_keys_next' => '39, 78',
  */
}

Functions

Namesort descending Description
photobox_attached_resources Utility function to get #attached resources.
photobox_field_formatter_info Implements hook_field_formatter_info().
photobox_field_formatter_settings_form Implements hook_field_formatter_settings_form().
photobox_field_formatter_settings_summary Implements hook_field_formatter_settings_summary().
photobox_field_formatter_view Implements hook_field_formatter_view().
photobox_gallery_custom_validate Validate function for photobox_gallery_custom.
photobox_library Implements hook_library().
photobox_library_path_check
photobox_menu Implements hook_menu().
photobox_theme Implements hook_theme().
_photobox_library_path Returns the path to the Photobox library.
_photobox_library_path_js Returns the path to the Photobox library js file.
_photobox_library_version Returns version of the Photobox library.
_photobox_settings Helper function returning list of Photobox library settings.