You are here

zurb_twentytwenty.module in ZURB TwentyTwenty 7

Same filename and directory in other branches
  1. 8 zurb_twentytwenty.module
  2. 2.0.x zurb_twentytwenty.module

File

zurb_twentytwenty.module
View source
<?php

/**
 * Implementation of hook_theme().
 */
function zurb_twentytwenty_theme() {
  return array(
    'zurb_twentytwenty' => array(
      'variables' => array(
        'items' => NULL,
        'images' => NULL,
        'options' => NULL,
        'entity' => NULL,
      ),
      'template' => 'zurb-twentytwenty',
    ),
  );
}

/**
 * Implementation of hook_field_formatter_info().
 * @return array
 */
function zurb_twentytwenty_field_formatter_info() {
  $formatters = array(
    'twentytwenty' => array(
      'label' => t('Zurb TwentyTwenty'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'image_style' => '',
        'default_offset_pct' => '0.5',
      ),
    ),
  );
  return $formatters;
}

/**
 * Implementation of hook_field_formatter_settings_form().
 * @param $field
 * @param $instance
 * @param $view_mode
 * @param $form
 * @param $form_state
 * @return array
 */
function zurb_twentytwenty_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array(
    '#type' => 'fieldset',
    '#title' => t('Zurb TwentyTwenty settings'),
  );
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => image_style_options(FALSE),
  );
  $element['default_offset_pct'] = array(
    '#title' => t('Default Offset (Percent)'),
    '#description' => t('Select an offset between 0 and 1 to set how far the slider is from the lefthand side when it loads. The default is 0.5.'),
    '#type' => 'textfield',
    '#default_value' => $settings['default_offset_pct'],
  );
  return $element;
}

/**
 * Implementation of hook_field_formatter_settings_summary().
 * @param $field
 * @param $instance
 * @param $view_mode
 * @return string
 */
function zurb_twentytwenty_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $image_styles = image_style_options(FALSE);
  unset($image_styles['']);
  $summary = array();

  // Styles could be lost because of enabled/disabled modules that defines
  // their styles in code.
  if (isset($image_styles[$settings['image_style']])) {
    $summary[] = t('Image style: @value', array(
      '@value' => $settings['image_style'],
    ));
  }
  else {
    $summary[] = t('Original image');
  }
  if (isset($settings['default_offset_pct'])) {
    $summary[] = t('Default offset: @value', array(
      '@value' => $settings['default_offset_pct'],
    ));
  }
  if ($field['cardinality'] != 2) {
    $summary[] = t('<strong>This image field needs to accept two images to be able to run TwentyTwenty properly, it is currently set to @value.</strong>', array(
      '@value' => $field['cardinality'] > 0 ? $field['cardinality'] : 'unlimited',
    ));
  }
  return implode('<br />', $summary);
}

/**
 * Implementation of hook_field_formatter_view().
 * @param $entity_type
 * @param $entity
 * @param $field
 * @param $instance
 * @param $langcode
 * @param $items
 * @param $display
 * @return array
 */
function zurb_twentytwenty_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $images = array();
  $element = array();
  $twentytwenty_path = libraries_get_path('twentytwenty');
  drupal_add_css($twentytwenty_path . '/css/twentytwenty.css', array(
    'scope' => 'footer',
    'weight' => 110,
  ));
  drupal_add_js($twentytwenty_path . '/js/jquery.event.move.js', array(
    'scope' => 'footer',
    'weight' => 100,
  ));
  drupal_add_js($twentytwenty_path . '/js/jquery.twentytwenty.js', array(
    'scope' => 'footer',
    'weight' => 110,
  ));
  drupal_add_js('jQuery(window).load(function () { jQuery(".twentytwenty-container").twentytwenty({default_offset_pct: ' . $settings['default_offset_pct'] . '}); });', array(
    'type' => 'inline',
    'scope' => 'footer',
    'weight' => 120,
  ));
  if (count($items)) {
    foreach ($items as $item) {
      $item['path'] = $item['uri'];
      if (isset($settings['image_style']) && drupal_strlen($settings['image_style'])) {
        $item['style_name'] = $settings['image_style'];
        $images[] = theme('image_style', $item);
      }
      else {
        $images[] = theme('image', $item);
      }
    }
    $element[] = array(
      '#theme' => 'zurb_twentytwenty',
      '#items' => $items,
      '#images' => $images,
      '#options' => array(
        'image_style' => $settings['image_style'],
        'default_offset_pct' => $settings['default_offset_pct'],
      ),
      '#entity' => $entity,
    );
  }
  return $element;
}