View source
<?php
function hover_preview_library() {
$libraries['imgPreview'] = array(
'title' => 'Image Preview',
'website' => 'http://plugins.jquery.com/project/imgPreview',
'version' => '0.22',
'js' => array(
drupal_get_path('module', 'hover_preview') . '/imgpreview.min.jquery.js' => array(),
),
'css' => array(
drupal_get_path('module', 'hover_preview') . '/hover_preview.imgpreview.css' => array(),
),
);
$libraries['imghover'] = array(
'title' => 'ImgHover',
'website' => 'http://code.google.com/p/jquery-image-hover/',
'version' => '2011.05.11',
'js' => array(
drupal_get_path('module', 'hover_preview') . '/jquery.imghover.js' => array(),
),
);
return $libraries;
}
function hover_preview_field_formatter_info() {
$formatters['hover_preview'] = array(
'label' => t('Hover Preview'),
'field types' => array(
'image',
),
'settings' => array(
'image_style' => '',
'image_link' => '',
'hover_preview_style' => '',
'hover_preview_action' => '',
),
);
return $formatters;
}
function hover_preview_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = image_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
$element['hover_preview_action'] = array(
'#title' => t('Hover preview action'),
'#type' => 'select',
'#default_value' => $settings['hover_preview_action'],
'#required' => TRUE,
'#options' => array(
'imgpreview' => t('Image Preview'),
'replace' => t('Replace Image'),
),
);
$image_styles = image_style_options(FALSE);
$element['hover_preview_style'] = array(
'#title' => t('Hover preview style'),
'#type' => 'select',
'#default_value' => $settings['hover_preview_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
);
return $element;
}
function hover_preview_field_formatter_settings_summary($field, $instance, $view_mode) {
$summary = image_field_formatter_settings_summary($field, $instance, $view_mode);
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if (isset($settings['hover_preview_action']) && !empty($settings['hover_preview_action'])) {
$summary .= '<br />' . t('Hover preview action: @action', array(
'@action' => $settings['hover_preview_action'],
));
}
else {
$summary .= '<br />' . t('Hover preview action: Preview Image');
}
$image_styles = image_style_options(FALSE);
if (isset($image_styles[$settings['hover_preview_style']])) {
$summary .= '<br />' . t('Hover preview style: @style', array(
'@style' => $image_styles[$settings['hover_preview_style']],
));
}
else {
$summary .= '<br />' . t('Hover preview style: Original image');
}
return $summary;
}
function hover_preview_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$element[$delta]['#theme'] = 'hover_preview_image_formatter';
$element[$delta]['#item'] = $item;
if (isset($item['title']) && !empty($item['title'])) {
$element[$delta]['#image']['title'] = $item['title'];
}
if (isset($item['alt']) && !empty($item['alt'])) {
$element[$delta]['#image']['alt'] = $item['alt'];
}
if (isset($display['settings']['image_style']) && !empty($display['settings']['image_style'])) {
$element[$delta]['#image']['path'] = image_style_url($display['settings']['image_style'], $item['uri']);
}
else {
$element[$delta]['#image']['path'] = $item['uri'];
}
if (isset($display['settings']['hover_preview_style']) && !empty($display['settings']['hover_preview_style'])) {
$element[$delta]['#image']['attributes']['data-hover-preview'] = image_style_url($display['settings']['hover_preview_style'], $item['uri']);
}
else {
$element[$delta]['#image']['attributes']['data-hover-preview'] = file_create_url($item['uri']);
}
$action = isset($display['settings']['hover_preview_action']) && !empty($display['settings']['hover_preview_action']) ? $display['settings']['hover_preview_action'] : 'imgpreview';
$element[$delta]['#image']['attributes']['class'][] = 'hover-preview-' . $action;
$element[$delta]['#image']['attributes']['class'][] = 'hover-preview';
switch ($display['settings']['image_link']) {
case 'content':
$uri = entity_uri($entity_type, $entity);
$element[$delta]['#path'] = array(
'path' => $uri['path'],
'options' => array(
'html' => TRUE,
),
);
break;
case 'file':
$element[$delta]['#path'] = array(
'path' => file_create_url($item['uri']),
'options' => array(
'html' => TRUE,
),
);
break;
}
switch ($action) {
case 'imgpreview':
$element[$delta]['#attached']['library'][] = array(
'hover_preview',
'imgPreview',
);
break;
case 'replace':
$element[$delta]['#attached']['library'][] = array(
'hover_preview',
'imghover',
);
break;
break;
}
$element[$delta]['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'hover_preview') . '/hover_preview.js',
'type' => 'file',
);
}
return $element;
}
function hover_preview_theme() {
return array(
'hover_preview_image_formatter' => array(
'variables' => array(
'item' => NULL,
'image' => NULL,
'path' => NULL,
),
),
);
}
function theme_hover_preview_image_formatter($variables) {
$output = theme('image', $variables['image']);
if (!empty($variables['path'])) {
$path = $variables['path']['path'];
$options = $variables['path']['options'];
$output = l($output, $path, $options);
}
return $output;
}