You are here

baguettebox.module in baguetteBox.js 7

Same filename and directory in other branches
  1. 8 baguettebox.module

Primary hooks for baguettebox module.

File

baguettebox.module
View source
<?php

/**
 * @file
 * Primary hooks for baguettebox module.
 */
define('BAGUETTEBOX_LIBRARY_DIR', 'sites/all/libraries/baguettebox');

/**
 * Implements hook_theme().
 */
function baguettebox_theme() {
  return array(
    'baguettebox_formatter' => array(
      'variables' => array(
        'item' => NULL,
        'path' => NULL,
        'image_style' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_library().
 */
function baguettebox_library() {
  $libraries['baguettebox'] = array(
    'title' => 'baguetteBox.js',
    'website' => 'https://github.com/feimosi/baguetteBox.js',
    'version' => '1.0.0',
    'js' => array(
      BAGUETTEBOX_LIBRARY_DIR . '/baguetteBox.min.js' => array(),
    ),
    'css' => array(
      BAGUETTEBOX_LIBRARY_DIR . '/baguetteBox.min.css' => array(),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_field_formatter_info().
 */
function baguettebox_field_formatter_info() {
  $formatters['baguettebox'] = array(
    'label' => t('baguetteBox'),
    'field types' => array(
      'image',
    ),
    'settings' => array(
      'initial_image_style' => '',
      'baguettebox_image_style' => '',
      'animation' => 'slideIn',
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function baguettebox_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, PASS_THROUGH);
  $element['initial_image_style'] = array(
    '#title' => t('Initial image style'),
    '#type' => 'select',
    '#default_value' => $settings['initial_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['baguettebox_image_style'] = array(
    '#title' => t('Baguettebox image_style'),
    '#type' => 'select',
    '#default_value' => $settings['baguettebox_image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['animation'] = array(
    '#title' => t('Animation'),
    '#type' => 'select',
    '#default_value' => $settings['animation'],
    '#options' => array(
      '0' => t('None'),
      'slideIn' => t('SlideIn'),
      'fadeIn' => t('FadeIn'),
    ),
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function baguettebox_field_formatter_settings_summary($field, $instance, $view_mode) {
  $settings = $instance['display'][$view_mode]['settings'];
  $image_styles = image_style_options(FALSE, PASS_THROUGH);
  $initial_image_style = isset($image_styles[$settings['initial_image_style']]) ? $image_styles[$settings['initial_image_style']] : t('Original image');
  $summary[] = t('Initial image style: %style', array(
    '%style' => $initial_image_style,
  ));
  $baguettebox_image_style = isset($image_styles[$settings['baguettebox_image_style']]) ? $image_styles[$settings['baguettebox_image_style']] : t('Original image');
  $summary[] = t('Baguettebox image style: %style', array(
    '%style' => $baguettebox_image_style,
  ));
  $summary[] = t('Animation: %animation', array(
    '%animation' => $settings['animation'],
  ));
  return implode('<br/>', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
function baguettebox_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array(
    '#theme_wrappers' => array(
      'container',
    ),
    '#attributes' => array(
      'class' => array(
        'baguettebox-container',
      ),
    ),
    '#attached' => array(
      'library' => array(
        array(
          'baguettebox',
          'baguettebox',
        ),
      ),
      'js' => array(
        drupal_get_path('module', 'baguettebox') . '/baguettebox.js',
        array(
          'data' => array(
            'baguettebox' => $settings,
          ),
          'type' => 'setting',
        ),
      ),
    ),
  );
  foreach ($items as $delta => $item) {
    $path = $settings['baguettebox_image_style'] ? image_style_url($settings['baguettebox_image_style'], $item['uri']) : file_create_url($item['uri']);
    $element[$delta] = array(
      '#theme' => 'baguettebox_formatter',
      '#item' => $item,
      '#image_style' => $display['settings']['initial_image_style'],
      '#path' => $path,
    );
  }
  return $element;
}

/**
 * Returns HTML for an baguettebox field formatter.
 */
function theme_baguettebox_formatter($vars) {
  $item = $vars['item'];
  $image = array(
    'path' => $item['uri'],
  );
  if (array_key_exists('alt', $item)) {
    $image['alt'] = $item['alt'];
  }
  if (isset($item['attributes'])) {
    $image['attributes'] = $item['attributes'];
  }
  if (isset($item['width']) && isset($item['height'])) {
    $image['width'] = $item['width'];
    $image['height'] = $item['height'];
  }

  // Do not output an empty 'title' attribute.
  if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
    $image['title'] = $item['title'];
  }
  if ($vars['image_style']) {
    $image['style_name'] = $vars['image_style'];
    $output = theme('image_style', $image);
  }
  else {
    $output = theme('image', $image);
  }
  $link_attributes = array();
  if (isset($image['title'])) {
    $link_attributes['data-caption'] = $item['title'];
  }
  elseif (isset($image['alt'])) {
    $link_attributes['data-caption'] = $item['alt'];
  }
  return l($output, $vars['path'], array(
    'html' => TRUE,
    'attributes' => $link_attributes,
  ));
}