hover_preview.module in Hover Preview for ImageCache 6
Same filename and directory in other branches
Hover Preview for Imagecache.
File
hover_preview.moduleView source
<?php
/**
* @file
* Hover Preview for Imagecache.
*/
/**
* Implementation of hook_theme().
*/
function hover_preview_theme() {
$presets_a = imagecache_presets();
$presets_b = $presets_a;
$items = array();
$items['hover_preview'] = array(
'arguments' => array(
'node' => NULL,
'field_name' => '',
'preset_a' => '',
'preset_b' => '',
),
);
foreach ($presets_a as $preset_a) {
foreach ($presets_b as $preset_b) {
// Hover only
$id = $preset_a['presetname'] . '_hover_preview_' . $preset_b['presetname'];
$items['hover_preview_formatter_' . $id] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_hover_preview_formatter',
);
// Linked to node
$id = $preset_a['presetname'] . '_hover_preview_' . $preset_b['presetname'] . '_linked';
$items['hover_preview_formatter_' . $id] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_hover_preview_formatter',
);
}
}
return $items;
}
/**
* Implementation of hook_field_formatter_info().
* Adds formatters to CCK image fields
*/
function hover_preview_field_formatter_info() {
$presets_a = imagecache_presets();
$presets_b = $presets_a;
$formatters = array();
foreach ($presets_a as $preset_a) {
foreach ($presets_b as $preset_b) {
// Hover only
$id = $preset_a['presetname'] . '_hover_preview_' . $preset_b['presetname'];
$formatters[$id] = array(
'label' => t('Hover: @preset_a TO: @preset_b', array(
'@preset_a' => $preset_a['presetname'],
'@preset_b' => $preset_b['presetname'],
)),
'field types' => array(
'image',
'filefield',
),
);
// Hover, linked to original node
$id = $preset_a['presetname'] . '_hover_preview_' . $preset_b['presetname'] . '_linked';
$formatters[$id] = array(
'label' => t('Hover: @preset_a TO: @preset_b linked to node', array(
'@preset_a' => $preset_a['presetname'],
'@preset_b' => $preset_b['presetname'],
)),
'field types' => array(
'image',
'filefield',
),
);
}
}
return $formatters;
}
/**
* Display a field like an imagecache formatter but with a wrapper to facilitate
* the hover preview.
*
* Some parts of this function are inspired from
* theme_imagecache_formatter_default() since hover preview is just extra stuff
* around an imagecache-generated img tag.
*/
function theme_hover_preview_formatter($element) {
// Generate unique id's for an image and it's corresponding preview url
$unique = $element['#item']['fid'] . '-' . $element['#item']['timestamp'];
// Inside a view $element may contain NULL data. In that case, just return.
if (empty($element['#item']['fid'])) {
return '';
}
drupal_add_js(drupal_get_path('module', 'hover_preview') . '/hover_preview.js');
drupal_add_css(drupal_get_path('module', 'hover_preview') . '/hover_preview.css');
// Extract the two constituent presets
if (substr($element['#formatter'], strlen($element['#formatter']) - 7, strlen($element['#formatter'])) == '_linked') {
$linked = TRUE;
list($preset_a, $preset_b) = explode('_hover_preview_', substr($element['#formatter'], 0, strlen($element['#formatter']) - 7));
}
else {
list($preset_a, $preset_b) = explode('_hover_preview_', $element['#formatter']);
}
// Generate the preview data
$preview_id = "hover-preview-" . $unique;
$preview_url = imagecache_create_url($preset_b, $element['#item']['filepath']);
$item = $element['#item'];
$item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : '';
$item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : '';
$preview_text = theme('imagecache', $preset_b, $element['#item']['filepath'], $item['data']['alt'], $item['data']['title'], array(
'class' => 'hover-preview-preview-url',
'style' => 'display: none',
'id' => $preview_id . '-url',
));
// Generate stuff needed for imagecache
$style = 'default';
$item = $element['#item'];
// Set the image alt and title if suitable values are available
if (!empty($item['data']['alt'])) {
$alt = $item['data']['alt'];
}
elseif (isset($element['#node']->node_title)) {
$alt = $element['#node']->node_title;
}
elseif (isset($element['#node']->title)) {
$alt = $element['#node']->title;
}
if (!empty($item['data']['title'])) {
$title = $item['data']['title'];
}
elseif (isset($element['#node']->node_title)) {
$title = $element['#node']->node_title;
}
else {
$title = $alt;
}
// Generate the imagecache html
$class = "imagecache imagecache-{$preset_a} imagecache-{$style} imagecache-{$element['#formatter']}";
$class .= " hover-preview";
$output = theme('imagecache', $preset_a, $item['filepath'], $alt, $title, array(
'class' => $class,
'id' => $preview_id,
));
if ($linked == TRUE && isset($element['#node']->path)) {
$return = l($output, $element['#node']->path, array(
'html' => array(
'html' => 'true',
),
)) . $preview_text;
}
else {
$return = $output . $preview_text;
}
return $return;
}
/**
* Convenience function to display an applicable field from a node using
* hover_preview
*
* Usage: theme('hover_preview', $node, 'field_photo', 'small_preset', 'large_preset', 'link to node (TRUE)');
*/
function theme_hover_preview($node, $field_name, $preset_a, $preset_b, $linked = NULL) {
$field = content_fields($field_name, $node->type);
if ($field) {
// If necessary, pass the node through node_view first to sanitise it
if (empty($node->content)) {
$node->build_mode = NODE_BUILD_NORMAL;
node_view($node);
}
// Poke CCK's display settings to use the formatter we want
if (isset($linked)) {
$format = $preset_a . '_hover_preview_' . $preset_b . '_linked';
}
else {
$format = $preset_a . '_hover_preview_' . $preset_b;
}
$field['display_settings']['full']['format'] = $format;
// Invoke CCK to assemble the data structure for us and then render it
$data = content_field('view', $node, $field, $node->{$field_name}, FALSE, FALSE);
$output = drupal_render($data);
return $output;
}
}
Functions
Name | Description |
---|---|
hover_preview_field_formatter_info | Implementation of hook_field_formatter_info(). Adds formatters to CCK image fields |
hover_preview_theme | Implementation of hook_theme(). |
theme_hover_preview | Convenience function to display an applicable field from a node using hover_preview |
theme_hover_preview_formatter | Display a field like an imagecache formatter but with a wrapper to facilitate the hover preview. |