View source
<?php
function zurb_twentytwenty_theme() {
return array(
'zurb_twentytwenty' => array(
'variables' => array(
'items' => NULL,
'images' => NULL,
'options' => NULL,
'entity' => NULL,
),
'template' => 'zurb-twentytwenty',
),
);
}
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;
}
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;
}
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();
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);
}
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;
}